I wrote a SQL - Statement to export the results to an excel sheet for analytics. At the moment I have the problem:
Some data which are split in 2 rows but need it as 1 row. Reason for that is one column, with different values.
I explain the structure: The sql create this table (headers):
ROOT|INSERT_TS|COUNT_ALL|Order_Type|Input_Obj|Input_SUPP|IS_SIM|Last_TS|Status_Type|Count_Open
Example data with my problem (this is the result what give my actual SQL - Statement):
R |In_TS|C_AL|O|I_Obj|I_Supp|IS_Sim|La_TS|Status|C_Opn|
--+-----+----+-+-----+------+------+-----+------+-----+
76|date1|1451|a|file1|mass |1 |date2|work |1451 | <-- 1st part
76|date1|25 |a|file1|mass |1 |date2|final |0 | <-- 2nd part
76|date1|1 |b|file1|man |0 |date2|final |0 |
76|date1|1 |c|file1|mass |1 |date2|work |1 |
What I want at the end is this (My target after fixing the sql query):
R |In_TS|C_AL|O|I_Obj|I_Supp|IS_Sim|La_TS|Status|C_Opn|
--+-----+----+-+-----+------+------+-----+------+-----+
76|date1|1476|a|file1|mass |1 |date2|work |1451 | <-- 1 row
76|date1|1 |b|file1|man |0 |date2|final |0 |
76|date1|1 |c|file1|mass |1 |date2|work |1 |
Thats my actual sql query/statement:
SELECT
distinct t1.ROOT r,
min(to_char(t1.INSERT_TS, 'YYYY.MM.DD HH24:MI')) in_ts,
count(distinct t1.tool_ID) c_al, -- this count give the number of modul steps in this "business tool" - no sum task is here needed
t1.ORDERTYPE o,
t1.I_OBJ,
t1.I_Supp,
t1.IS_SIM,
max(to_char(t1.LASTCHANGE_TS, 'DD.MM.YYYY HH24:MI')) la_ts,
t2.STATUS status,
sum(case when t2.STATUS != 'final' then 1 else 0 end) c_opn
FROM TOOL_DATA.FW_MAIN t1
left join TOOL_DATA.CONF_STATUS t2 on t2.tab = 'FW_MAIN' and t2.status_val = t1.status
GROUP BY
t1.ROOT, t1.ORDERTYPE, t1.I_OBJ, t1.I_SUPP, t1.I_SIM, t2.STATUS
ORDER BY 2 desc, 1 desc
;
What I do and search to solve this problem on my own:
I look here in this community and in google, found some task about own define functions and listagg, but both doesn't help, because the col "Status" grew up, but the row does not combine.
Have someone here any idea(s) for me to solve this?
The important thing is:
- Col "O" is identical
- Col "R" is identical
- Col "IS_SIM" is identical
- Col "C_AL" is cumulative
- Col "C_Opn" sum only the not "final" data sets (better: both values are cumulativ, cuz xx + 0 = xx)
- Col "Status" show only the "work" value, if the rows are combined, the "final" value is removed
The solution (thx to ruudvan)
SELECT
distinct t1.ROOT r,
min(to_char(t1.INSERT_TS, 'YYYY.MM.DD HH24:MI')) in_ts,
count(distinct t1.tool_ID) c_al, -- this count give the number of modul steps in this "business tool" - no sum task is here needed
t1.ORDERTYPE o,
t1.I_OBJ,
t1.I_Supp,
t1.IS_SIM,
max(to_char(t1.LASTCHANGE_TS, 'DD.MM.YYYY HH24:MI')) la_ts,
NVL(MAX(case when t2.STATUS != 'final' then t2.STATUS else null end), 'final'), -- i set it to != 'final', because status have more as 1 work value, but i wrote it here for a simple view
sum(case when t2.STATUS != 'final' then 1 else 0 end) c_opn
FROM TOOL_DATA.FW_MAIN t1
left join TOOL_DATA.CONF_STATUS t2 on t2.tab = 'FW_MAIN' and t2.status_val = t1.status
GROUP BY
t1.ROOT, t1.ORDERTYPE, t1.I_OBJ, t1.I_SUPP, t1.I_SIM,
ORDER BY 2 desc, 1 desc
;