Group concatenation in Hive sql - hive

I am finding bit challenging to achieve the below task.
Please find the attachment for sample data set and output.
Basically i want to concatenate in c6 column into 1 with pipe separated for each group

Use group by and then collect_list with concat_ws function.
select c1,c2,c3,c4,c5,concat_ws("|",collect_list(c6)) c6 from
<table_name>
group by c1,c2,c3,c4,c5;

You can use collect function
using collect()
select c1,c2,c3,c4,c5,concat_ws("|",collect(c6)) c6 from <table> group by c1,c2,c3,c4,c5
Using collect_set()
select c1,c2,c3,c4,c5,concat_ws("|",collect_set(c6)) c6 from <table> group by c1,c2,c3,c4,c5

Related

OracleSQL refer to columns just created

Is there a way to refer to columns created in the same SELECT
SELECT 2*a as a2,
3*a2 as a6
FROM ...
I am aware that I could use a nested query, but with many variables created after each other this seems tedious.
You can not refer to the alias in the same SELECT clause. It is SQL standard behavior, and it prevents ambiguities.
There are two options as I have mentioned in the comment also.
-- Use expression of the a2 in a6
SELECT 2*a as a2,
3*(2*a) as a6
FROM ...
-- OR use the sub-query
SELECT a2, 3*a2 as a6 FROM
(SELECT 2*a as a2
--3*a2 as a6
FROM ...)

Build multi columns as pivot in Oracle

I am trying pivot the data based on existing data. I have details as below.
based on strt_dt, I need to do pivot up to 36 months. I cannot add 36 cols as pivoting by using min or max functions. Please advice me what is the better way. I need to pivot 36 cols for 36 months.
Like strt_dt value is 202003 then 202004,202005,202006....202305.
Table sample data:
Thanks for your help in advance.
Why don't you build a dynamic query.
e.g. first build the columns :
select listagg('SUM(CASE WHEN to_char(start_dt,''YYYYMM'')='
||sdt||' THEN PRC_AMT END) as prc_amt'
||sdt,',') WITHIN GROUP (ORDER BY sdt)
into colList
from (select distinct to_char(start_dt,'YYYYMM') sdt
from price_details)
then generate the actual query :
lv_sql := 'select '||colList||'
from price_details
group by to_char(start_dt,''YYYYMM'')';
and execute it any way you want :
execute immediate lv_sql into your_variables;

I need help for a specific sql query

I have the following sql query:
select Judge, ResultIndex, count(*) as CasesForJudge
from SRSIndexes
group by Judge, ResultIndex
The columns "Judge" and "ResultIndex" are nvarchar type. I recieve output like this:
Adelina Andreeva 2a 24
Adelina Andreeva 5b 33
....
Georgy Ivanov 3b 44
Georgy Ivanov 5a 5
I want to find the sums (from "CasesForJudge" column) for each judge (for example: Adelina Andreeva -> 57, Georgy Ivanov -> 49). How should i modify my query?
You just need to GROUP BY your Judge column
SELECT Judge, count(*) AS CasesForJudge
FROM SRSIndexes
GROUP BY Judge
In case you want both groupings in one query you can try grouping sets:
select Judge,
ResultIndex,
count(*) as CasesForJudge
from SRSIndexes
group by grouping sets ((Judge, ResultIndex), -- Initial grouping
(Judge)) -- Added one

how to get the comma separated values of the column stored in the Sql server

how to get the comma separated values stored in the Sql Db into a individual values
e.g in sql DB the column is stored with comma values as shown below,
EligibleGroup
A11,A12,A13
B11,B12,B13
I need to get
EligibleGroup
A11
A12
A13
B11
B12
...
I have written a query that will fetch me some list of employees with employee name and eligible group
XXX A11
YYY B11
ZZZ C11
I need to check that the employees(XXX,YYY,ZZZ) eligiblegroup falls within this
EligibleGroup
A11,A12,A13
B11,B12,B13
and return me only that rows.
use a "user defined function" like the one shown here (including source code) - it returns the splitted values as a "table" (one row per value) you can select from like
select txt_value from dbo.fn_ParseText2Table('A11,A12,A13')
returns
A11
A12
A13
You could use a subquery:
SELECT employee_name, eligible_group
FROM YourTable
WHERE eligible_group IN
(SELECT SPLIT(EligibleGroup)
FROM tblEligibleGroup
WHERE <some conditions here>)
I don't believe the "SPLIT" function exists in SQL Server so you'll have to either create a user defined function to handle that, or you could use the nifty workaround suggested here: How do I split a string so I can access item x?
I think you can do it this way,
select left('A11,A12,A13',3) + SUBSTRING('A11,A12,A13',charindex(',','A11,A12,A13'),10)
I think you may not have to split EligibleGroup. You can do another way by just:
select empId
from yourTempEmpTable t1, EligibleGroup t2
where t2.elibigle like '%'+t1.elibigle+'%'
I think it should work.
Assuming that EligibleGroup has a fixed length data, you can try using SUBSTRING As follows:
select substring(EligibleGroup,1,3) from #test union all
select substring(EligibleGroup,5,3) from #test union all
select substring(EligibleGroup,9,3) from #test
This will return:
A11
A12
A13
B11
B12
...
You can try it in Data Explorer
And If you need to check if an employee fall into which EligibleGroup try this:
Select EligibleGroup from test where eligibleGroup like '%A11'

SQL SELECT: value that occur > 1

I'm trying to select duplicates from this table:
snr zip
01 83
02 82
03 43
04 28
Expected result is just empty table. Cuz it got no duplicates.
I've tried with this query:
SELECT snr, zip
FROM student
GRUOP BY snr
HAVING (COUNT(zip) > 1)
But it says that syntax is error, and that I'm trying to query an aggregate functions, etc..
It looks like you need to either remove zip from the SELECT columns, or else wrap it in an aggregate function, such as COUNT(zip):
SELECT snr, COUNT(zip)
FROM student
GROUP BY snr
HAVING (COUNT(zip) > 1)
Also check out #OMG Ponies's answer for further suggestions.
Use:
SELECT snr, zip
FROM student
GROUP BY snr, zip
HAVING COUNT(DISTINCT zip) > 1
Standard SQL requires that columns in the SELECT clause that are not wrapped in aggregate functions (COUNT, MIN, MAX, etc) need to be defined in the GROUP BY. However, MySQL and SQLite allow for columns to be omitted.
Additionally, use COUNT(DISTINCT or you'll risk false positives.