Spliting GROUP BY results into different columns - sql

I have a column containing date ranges and the number of days passed associated to a specific ID (one to many), based on the number of records associated to it, I want those results split into columns instead of individual rows, so from this:
id_hr dd beg end
----------------------------------------
1 10 05/01/2019 15/01/2019
1 5 03/02/2019 08/02/2019
2 8 07/03/2019 15/03/2019
Could become this:
id_hr dd beg end dd beg end
--------------------------------- ---------------------
1 10 05/01/2019 15/01/2019 5 03/02/2019 08/02/2019
2 8 07/03/2019 15/03/2019
I did the same in a worksheet (pivot table) but the table became as slow as it could get, so I'm looking for a more friendly approach in SQL, I did a CTE which number the associated rows and then select each one and display them in new columns.
;WITH CTE AS(
SELECT PER_PRO, ID_HR, NOM_INC, rut_dv, dias_dur, INI, FIN,
ROW_NUMBER()OVER(PARTITION BY ID_HR ORDER BY SUBIDO) AS RN
FROM dbo.inf_vac WHERE PER_PRO = 201902
)
SELECT ID_HR, NOM_INC, rut_dv,
(case when rn = 1 then DIAS_DUR end) as DIAS_DUR1,
(case when rn = 1 then INI end) as INI1,
(case when rn = 1 then FIN end) as FIN1,
(case when rn = 2 then DIAS_DUR end) as DIAS_DUR2,
(case when rn = 2 then INI end) as INI2,
(case when rn = 2 then FIN end) as FIN2,
(case when rn = 3 then DIAS_DUR end) as DIAS_DUR3,
(case when rn = 3 then INI end) as INI3,
(case when rn = 3 then FIN end) as FIN3
FROM CTE
Which gets me each column on where it should be but not grouped. Using GROUP BY displays an error on the CTE select.
rn id_hr dd beg end dd beg end
----------------------------------- ------------------------
1 1 10 05/01/2019 15/01/2019 NULL NULL NULL
2 1 NULL NULL NULL 5 03/02/2019 08/02/2019
1 2 8 07/03/2019 15/03/2019 NULL NULL NULL
Is there any way to group them on the second select?

You have additional columns in the result set that are not in the query. However, this should work:
SELECT ID_HR,
max(case when rn = 1 then DIAS_DUR end) as DIAS_DUR1,
max(case when rn = 1 then INI end) as INI1,
max(case when rn = 1 then FIN end) as FIN1,
max(case when rn = 2 then DIAS_DUR end) as DIAS_DUR2,
max(case when rn = 2 then INI end) as INI2,
max(case when rn = 2 then FIN end) as FIN2,
max(case when rn = 3 then DIAS_DUR end) as DIAS_DUR3,
max(case when rn = 3 then INI end) as INI3,
max(case when rn = 3 then FIN end) as FIN3
FROM CTE
GROUP BY ID_HR;

Yes, you can GROUP BY all the non-CASE columns, and apply MAX to each of the CASE-expression columns.

Related

employment data - Pivot table, placing all employment data in one row

Hi so I have table as following
employee id
job
start_dt
1
abc
1/1/2021
1
def
5/1/2021
2
xyz
6/1/2021
2
rfd
8/1/2021
2
hgf
7/1/2021
2
esd
1/1/1999
I was wonder if there's a way I could pivot the table and layup all job and date at the same row,
employee id
job_a
start_dt_ a
job_b
start_dt_b
job_c
start_dt_c
job_d
start_dt_d
job_e
start_dt_e
job_f
start_dt_f
1
abc
1/1/2021
def
5/1/2021
2
xyz
6/1/2021
rfd
8/1/2021
hgf
7/1/2021
esd
1/1/1999
(table name 'JOB')
You can use Conditional Aggregation along with ROW_NUMBER() Analytic function such as
WITH j AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY employee_id ORDER BY start_dt) AS rn,
j.*
FROM job j
)
SELECT employee_id,
MAX(CASE WHEN rn = 1 THEN job END) AS job_a,
MAX(CASE WHEN rn = 1 THEN start_dt END) AS start_dt_a,
MAX(CASE WHEN rn = 2 THEN job END) AS job_b,
MAX(CASE WHEN rn = 2 THEN start_dt END) AS start_dt_b,
MAX(CASE WHEN rn = 3 THEN job END) AS job_c,
MAX(CASE WHEN rn = 3 THEN start_dt END) AS start_dt_c,
MAX(CASE WHEN rn = 4 THEN job END) AS job_d,
MAX(CASE WHEN rn = 4 THEN start_dt END) AS start_dt_d
FROM j
GROUP BY employee_id
ORDER BY employee_id
Demo
P.S. the logic for the sorting for current result set is not clear enough

SQL PIVOT table with MIN() and on Multiple columns

Here is the table structure
ID TypeX TypeXDesc XDate TypeCodeY
040001 3669 Unspecified Cat 2005-08-08 1
040001 3669 Unspecified Cat 2006-08-29 2
040001 37515 Tear Film 2005-08-08 1
040001 37999 Disor 2004-07-22 1
Transform above table INTO below USING PIVOT
ID TypeX_1 TypeXDesc_1 XDate_1 TypeCodeY_1 TypeX_2 TypeXDesc_2 XDate_2 TypeCodeY_2 TypeX_3 TypeXDesc_3 XDate_3 TypeCodeY_3
040001 3669 Unspecified Cat 2005-08-08 1 37515 Tear Film 2005-08-08 1 37999 Disor 2004-07-22 1
Look at the same TypeX code but XDate is different and we need to get Min(XDate) so first row is qualified not the second row.
You can accomplish this using conditional aggregation. In this case, you can enumerate the rows with within typex/id groups using row_number(). You can enumerate the groups with with typex/id using dense_rank().
Then, use conditional aggregation:
select t.id,
max(case when grpnum = 1 and seqnum = 1 then typex end) as typex_1,
max(case when grpnum = 1 and seqnum = 1 then TypeXDesc end) as TypeXDesc_1,
max(case when grpnum = 1 and seqnum = 1 then XDate end) as XDate_1,
max(case when grpnum = 1 and seqnum = 1 then TypeCodeY end) as TypeCodeY_1,
max(case when grpnum = 2 and seqnum = 1 then typex end) as typex_12,
max(case when grpnum = 2 and seqnum = 1 then TypeXDesc end) as TypeXDesc_2,
max(case when grpnum = 2 and seqnum = 1 then XDate end) as XDate_2,
max(case when grpnum = 2 and seqnum = 1 then TypeCodeY end) as TypeCodeY_3,
max(case when grpnum = 3 and seqnum = 1 then typex end) as typex_1,
max(case when grpnum = 3 and seqnum = 1 then TypeXDesc end) as TypeXDesc_3,
max(case when grpnum = 3 and seqnum = 1 then XDate end) as XDate_3,
max(case when grpnum = 3 and seqnum = 1 then TypeCodeY end) as TypeCodeY_3
from (select t.*,
row_number() over (partition by id, typex order by xdate as seqnum,
dense_rank() over (partition by id order by typex) as grpnum
from t
) t
group by id;

SQL Pivot View for Single Table

Have a table:
Nomber Sce SceValue
10 A a1b2c3
20 C d2v3b4
10 B 42b2c3
10 B 5978c3
20 A edr432
I need to create the following listing using a view where there all possible "Sce" and "SceValue" pairs are shown for each individual "Nomber" (9 pairs maximum):
Nomber Sce1 SceValue1 Sce2 SceValue2 ... Sce9 SceValue9
10 A a1b2c3 B 42b2c3 B 5978c3
20 C d2v3b4 A edr432
I would like to achieve this using a View. Is this possible?
You can use conditional aggregation:
select number,
max(case when seqnum = 1 then Sce end) as sce_1,
max(case when seqnum = 1 then SceValue end) as SceValue_1,
max(case when seqnum = 2 then Sce end) as sce_2,
max(case when seqnum = 2 then SceValue end) as SceValue_2,
. . .
max(case when seqnum = 9 then Sce end) as sce_9,
max(case when seqnum = 9 then SceValue end) as SceValue_9
from (select t.*,
row_number() over (partition by nomber order by sce) as seqnum
from t
) t
group by nomber;

group by and select max with value null

I have a next problem with query
SELECT
T.DETALLE_BECA_ANIO anio,
T.DETALLE_BECA_MES mes,
T.DETALLE_BECA_NIVEL_EDU_ID edu_id,
T.DETALLE_BECA_TRAMO_ID tr_id,
MAX(
CASE
WHEN T.DETALLE_BECA_TIPO_BENE_ID IS NULL
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) mant ,
MAX(
CASE
WHEN T.DETALLE_BECA_TIPO_BENE_ID = 1
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) tras
FROM
(SELECT DETALLE_BECA_NIVEL_EDU_ID,
DETALLE_BECA_BECA_ID,
DETALLE_BECA_TIPO_BENE_ID,
DETALLE_BECA_VALOR,
DETALLE_BECA_MES,
DETALLE_BECA_REGION_ID,
DETALLE_BECA_PROVINCIA_ID,
DETALLE_BECA_ANIO,
DETALLE_BECA_TRAMO_ID,
DETALLE_BECA_COMUNA_ID
FROM TBL_DETALLE_BECAS
WHERE (DETALLE_BECA_TIPO_BENE_ID = 1
OR DETALLE_BECA_TIPO_BENE_ID IS NULL)
and DETALLE_BECA_BECA_ID = 1
and detalle_beca_mes = 3
) T
GROUP BY T.DETALLE_BECA_BECA_ID,
T.DETALLE_BECA_TRAMO_ID,
T.DETALLE_BECA_REGION_ID,
T.DETALLE_BECA_PROVINCIA_ID,
T.DETALLE_BECA_ANIO,
T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID,
T.DETALLE_BECA_COMUNA_ID
ORDER BY T.DETALLE_BECA_BECA_ID,
T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID
output:
"ANIO" "MES" "EDU_ID" "TR_ID" "MANT" "TRAS"
2017 3 2 0.62 0 NULL
2017 3 3 1.24 6 NULL
2017 3 NULL 1.0 NULL 1
I need that sum value where EDU_ID is null with value 2,3 in TR_ID and replace value null in "tras" with value from EDU is null
"ANIO" "MES" "EDU_ID" "TR_ID" "MANT" "TRAS"
2017 3 2 1.62 0 1
2017 3 3 2.24 6 1
I writed query with min(edu_id) or max(edu_id ) but could not solve my problem.
The other thing that occurred to me is to make a join with the same table
First, this makes more sense as your query:
SELECT T.DETALLE_BECA_ANIO as anio, T.DETALLE_BECA_MES as mes,
T.DETALLE_BECA_NIVEL_EDU_ID as edu_id, T.DETALLE_BECA_TRAMO_ID as tr_id,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID IS NULL
THEN NVL(DETALLE_BECA_VALOR, 0)
ELSE 0
END) as mant ,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID = 1
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) tras
FROM TBL_DETALLE_BECAS
WHERE (DETALLE_BECA_TIPO_BENE_ID = 1 OR DETALLE_BECA_TIPO_BENE_ID IS NULL) AND
DETALLE_BECA_BECA_ID = 1 AND
detalle_beca_mes = 3
GROUP BY T.DETALLE_BECA_ANIO, T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID, T.DETALLE_BECA_TRAMO_ID
ORDER BY T.DETALLE_BECA_BECA_ID, T.DETALLE_BECA_MES, T.DETALLE_BECA_NIVEL_EDU_ID;
This eliminates the subquery (unnecessary) and only aggregates by the columns being returned. A proper query might fix your problem.
But, you seem to want to use NULL to be "all" for the other columns. If so, something like this will work:
WITH t as (
SELECT T.DETALLE_BECA_ANIO as anio, T.DETALLE_BECA_MES as mes,
T.DETALLE_BECA_NIVEL_EDU_ID as edu_id, T.DETALLE_BECA_TRAMO_ID as tr_id,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID IS NULL
THEN NVL(DETALLE_BECA_VALOR, 0)
ELSE 0
END) as mant ,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID = 1
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) tras
FROM TBL_DETALLE_BECAS
WHERE (DETALLE_BECA_TIPO_BENE_ID = 1 OR DETALLE_BECA_TIPO_BENE_ID IS NULL) AND
DETALLE_BECA_BECA_ID = 1 AND
detalle_beca_mes = 3
GROUP BY T.DETALLE_BECA_ANIO, T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID, T.DETALLE_BECA_TRAMO_ID
)
SELECT t.ANIO, t.MES, t.EDU_ID,
COALESCE(t.TR_ID, 0) + COALESCE(tnull.TR_ID, 0) as TR_ID,
t.MANT,
COALESCE(t.TRAS, 0) + COALESCE(tnull.TRAS, 0) as TRAS
FROM t LEFT JOIN
(SELECT t.*
FROM t
WHERE t.edu_id IS NULL
) tnull
ON tnull.ANIO = t.ANIO AND tnull.MES = t.MES
WHERE t.edu_id IS NOT NULL
ORDER BY T.DETALLE_BECA_BECA_ID, T.DETALLE_BECA_MES, T.DETALLE_BECA_NIVEL_EDU_ID;

How to make multiple rows into columns

I've tried MAX CASE WHEN and CTE but for some reason can't exactly figure this out.
My data looks like this:
SELECT RC, isMHy, eligible
FROM test
RC isMHY eligible
190B05 0 1
190K00 1 0
There can be up to 4 rows in the table, I want to the results to look like this (12 columns in case there are 4 rows)
RC1 isMHY1 eligible1 RC2 isMHY2 eligible2
190B05 0 1 190K00 1 0
Any suggestions would be appreciated
You can use conditional aggregation with ROW_NUMBER() :
SELECT MAX(CASE WHEN s.rnk = 1 THEN s.rc END) as rc1,
MAX(CASE WHEN s.rnk = 1 THEN s.ismhy END) as ismhy1,
MAX(CASE WHEN s.rnk = 1 THEN s.eligible END) as eligible1,
MAX(CASE WHEN s.rnk = 2 THEN s.rc END) as rc2,
MAX(CASE WHEN s.rnk = 2 THEN s.ismhy END) as ismhy2,
MAX(CASE WHEN s.rnk = 2 THEN s.eligible END) as eligible2,
..........
FROM(
SELECT t.*,
ROW_NUMBER() OVER(ORDER BY SELECT 1) as rnk
FROM test t) s