Here is my problem...I have this query already giving me the results I am looking for except that I need to display rows that have zero values... in the column "station" I should be displaying numbers 4 and 7 too with zero values:
select
sum("Ch1") AS "Ch1",
sum("Ch2") AS "Ch2",
sum("Ch3") AS "Ch3",
sum("Ch4") AS "Ch4",
CASE WHEN "FailCode" >= 10 and "FailCode" <= 13 THEN 1
WHEN "FailCode" >= 14 and "FailCode" <= 17 THEN 2
WHEN "FailCode" >= 18 and "FailCode" <= 21 THEN 3
WHEN "FailCode" >= 23 and "FailCode" <= 26 THEN 4
WHEN "FailCode" >= 27 and "FailCode" <= 30 THEN 5
WHEN "FailCode" >= 35 and "FailCode" <= 38 THEN 6
WHEN "FailCode" >= 40 and "FailCode" <= 43 THEN 7
END AS station
from (
SELECT "FailCode",
count (*) FILTER (WHERE "FailCode" =10 or "FailCode"=14 or "FailCode"=18 or "FailCode"=23 or "FailCode"=27 or "FailCode"=35 or "FailCode"=40) as "Ch1",
count (*) FILTER (WHERE "FailCode" =11 or "FailCode"=15 or "FailCode"=19 or "FailCode"=24 or "FailCode"=28 or "FailCode"=36 or "FailCode"=41) as "Ch2",
count (*) FILTER (WHERE "FailCode" =12 or "FailCode"=16 or "FailCode"=20 or "FailCode"=25 or "FailCode"=29 or "FailCode"=37 or "FailCode"=42) as "Ch3",
count (*) FILTER (WHERE "FailCode" =13 or "FailCode"=17 or "FailCode"=21 or "FailCode"=26 or "FailCode"=30 or "FailCode"=38 or "FailCode"=43) as "Ch4"
FROM minical01
where ("FailCode" between 10 and 43) and not ("FailCode" between 31 and 34)
group by 1
) t
where t is not null
group by 5
order by 5
This is what I am getting:
Ch1
Ch2
Ch3
Ch4
Station
61
487
117
57
1
6
13
5
7
2
0
29
17
21
3
283
807
113
510
5
2
0
0
0
6
This is what I am trying to get:
Ch1
Ch2
Ch3
Ch4
Station
61
487
117
57
1
6
13
5
7
2
0
29
17
21
3
0
0
0
0
4
283
807
113
510
5
2
0
0
0
6
0
0
0
0
7
The following is what I tried so far, but it's not changing my results. I still don't get any change:
select
sum("Ch1") AS "Ch1",
sum("Ch2") AS "Ch2",
sum("Ch3") AS "Ch3",
sum("Ch4") AS "Ch4",
CASE WHEN "FailCode" >= 10 and "FailCode" <= 13 THEN 1
WHEN "FailCode" >= 14 and "FailCode" <= 17 THEN 2
WHEN "FailCode" >= 18 and "FailCode" <= 21 THEN 3
WHEN "FailCode" >= 23 and "FailCode" <= 26 THEN 4
WHEN "FailCode" >= 27 and "FailCode" <= 30 THEN 5
WHEN "FailCode" >= 35 and "FailCode" <= 38 THEN 6
WHEN "FailCode" >= 40 and "FailCode" <= 43 THEN 7
END AS station
from (
SELECT "FailCode",
count (*) FILTER (WHERE "FailCode" =10 or "FailCode"=14 or "FailCode"=18 or "FailCode"=23 or "FailCode"=27 or "FailCode"=35 or "FailCode"=40) as "Ch1",
count (*) FILTER (WHERE "FailCode" =11 or "FailCode"=15 or "FailCode"=19 or "FailCode"=24 or "FailCode"=28 or "FailCode"=36 or "FailCode"=41) as "Ch2",
count (*) FILTER (WHERE "FailCode" =12 or "FailCode"=16 or "FailCode"=20 or "FailCode"=25 or "FailCode"=29 or "FailCode"=37 or "FailCode"=42) as "Ch3",
count (*) FILTER (WHERE "FailCode" =13 or "FailCode"=17 or "FailCode"=21 or "FailCode"=26 or "FailCode"=30 or "FailCode"=38 or "FailCode"=43) as "Ch4"
FROM minical01
where ("FailCode" between 10 and 43) and not ("FailCode" between 31 and 34)
group by 1
) t
LEFT JOIN generate_series(1, 7, 1) AS g(id)
ON g.id = t."FailCode"
where t is not null
group by 5
order by 5
Related
I have data like in a dataframe
CommsId
Id
Amount
Date
85
1
10
07/10/2020
72
1
15
09/09/2021
85
1
25
09/09/2021
70
1
30
09/09/2021
72
1
-15
05/11/2020
70
1
-30
05/11/2020
For each date, I want to find the sum of amounts of the latest CommsId as of the date.
Expected output is as below
Date
Sum_Amount
Id
07/10/2020
10
1
09/09/2021
70
1
05/11/2021
25
1
I created a view which is presently giving the data like this:
practice_name message_type message_count
CHC ALOG_SYNC 1
CHC BULKNT 0
CHC PIE_SYNC 1
CHC PPRV_SYNC 1
CHC SYNC_PRACT 3
CHC SYNC_PROV 9
CHC SYNC_WTXT 3
CHC SYNC_XYZ 0
Midtown ALOG_SYNC 0
Midtown BULKNT 0
Midtown PIE_SYNC 0
Midtown PPRV_SYNC 0
Midtown SYNC_PRACT 3
Midtown SYNC_PROV 0
Midtown SYNC_WTXT 3
Midtown SYNC_XYZ 0
NextGen MedicalPractice ALOG_SYNC 0
NextGen MedicalPractice BULKNT 1
NextGen MedicalPractice PIE_SYNC 0
NextGen MedicalPractice PPRV_SYNC 0
NextGen MedicalPractice SYNC_PRACT 3
NextGen MedicalPractice SYNC_PROV 591
NextGen MedicalPractice SYNC_WTXT 3
NextGen MedicalPractice SYNC_XYZ 0
My View:
CREATE OR REPLACE VIEW sha.sha_export_queue_view AS
SELECT q3.practice_name,
q3.message_type,
q3.share_site_org_key,
COALESCE(q2.message_count, '0'::text) AS message_count
FROM ( SELECT q1.practice_name,
mt.message_type,
q1.share_site_org_key
FROM sha.message_types mt,
( SELECT DISTINCT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
ai.share_site_org_key
FROM sha.sha_share_site_view ssv
LEFT JOIN ( SELECT mytable2.assessment_id,
mytable2.result_json,
mytable2.share_site_org_key,
mytable2.rnk
FROM ( SELECT assessment_info.assessment_id,
assessment_info.result_json,
assessment_info.share_site_org_key,
dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
FROM sha.assessment_info
WHERE assessment_info.assessment_id = 8::numeric) mytable2
WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q1) q3
LEFT JOIN ( SELECT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Type'::text AS message_type,
jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Count'::text AS message_count
FROM sha.sha_share_site_view ssv
LEFT JOIN ( SELECT mytable2.assessment_id,
mytable2.result_json,
mytable2.share_site_org_key,
mytable2.rnk
FROM ( SELECT assessment_info.assessment_id,
assessment_info.result_json,
assessment_info.share_site_org_key,
dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
FROM sha.assessment_info
WHERE assessment_info.assessment_id = 8::numeric) mytable2
WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q2 ON q3.message_type::text = q2.message_type AND q3.practice_name = q2.practice_name
ORDER BY q3.practice_name;
I want the second column to be flattened:
Practice Time Stamp <<message type 1>> <<message type 2>> <<message type 3>> <<message type 4 >> <<message type 5>> <<message type 6>> <<message type 7>> <<message type 8>>
Practice Name 1 21-12-2016 10:00 23 25 27 29 31 33 35 37
Practice Name 2 21-12-2016 10:00 24 26 28 30 32 34 36 38
Practice Name 3 21-12-2016 13:00 25 27 29 31 33 35 37 39
Practice Name 4 21-12-2016 13:00 26 28 30 32 34 36 38 40
Practice Name 5 24-12-2016 13:00 27 29 31 33 35 37 39 41
Practice Name 6 27-12-2016 13:00 28 30 32 34 36 38 40 42
Practice Name 7 30-12-2016 13:00 29 31 33 35 37 39 41 43
Practice Name 8 02-01-2017 13:00 30 32 34 36 38 40 42 44
Practice Name 1 05-01-2017 13:00 31 33 35 37 39 41 43 45
Practice Name 2 08-01-2017 13:00 32 34 36 38 40 42 44 46
Practice Name 3 11-01-2017 13:00 33 35 37 39 41 43 45 47
Is there any way I can achieve that?
Sorry for the little alignment issue.
The values are corresponding message type values
Sample for query (idea in comments to question):
SELECT
practice_name,
sum("ALOG_SYNC") AS "ALOG_SYNC",
sum("BULKNT") AS "BULKNT",
...
FROM (
SELECT
practice_name,
CASE WHEN q3.message_type = 'ALOG_SYNC' THEN sum(message_count) END AS "ALOG_SYNC",
CASE WHEN q3.message_type = 'BULKNT' THEN sum(message_count) END AS "BULKNT"
FROM
<your from + where clause>
) AS A
GROUP BY 1
Probably your query might be optimised.
Or you can use crosstab function (https://www.postgresql.org/docs/current/static/tablefunc.html)
I'm trying to get the Running total from the values from the previous months. And if the month is january I would like to get the total from the previous year and so forth.
I hope someone can help.
WITH CTE AS (SELECT COUNT(LZP.pv_zaaknummer) AS [Aantal LZP]
, YEAR(LZP.lzp_actief_vanaf) AS Jaar
, MONTH(LZP.lzp_actief_vanaf) AS Maand
FROM dm.crm_LZP_Vn_zaaktype_leefzorgplan_registrerenExtensionBase_hist AS LZP
WHERE LZP.LZP_actief_tot_LDTS > GETDATE()
GROUP BY YEAR(LZP.lzp_actief_vanaf)
, MONTH(LZP.lzp_actief_vanaf)
)
SELECT a.Jaar
, a.Maand
, a.[Aantal LZP]
, (
SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar
) AS [Running Total 1]
, (
SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar
AND b.Maand <= a.Maand
) AS [Running Total 2]
FROM CTE AS a
ORDER BY a.Jaar, a.Maand;
Results as of now, I'm getting the Totals per year and then the running totals per year:
Jaar Maand Aantal LZP Running Total 1 Running Total 2
2014 4 11 661 11
2014 5 52 661 63
2014 6 70 661 133
2014 7 76 661 209
2014 8 39 661 248
2014 9 86 661 334
2014 10 112 661 446
2014 11 120 661 566
2014 12 95 661 661
2015 1 57 3327 57
2015 2 109 3327 166
2015 3 196 3327 362
2015 4 200 3327 573
2015 5 169 3327 794
2015 6 233 3327 1097
2015 7 276 3327 1449
2015 8 224 3327 1712
2015 9 203 3327 2001
2015 10 291 3327 2404
2015 11 296 3327 2820
2015 12 412 3327 3327
2016 1 311 6062 368
2016 2 341 6062 818
2016 3 476 6062 1490
2016 4 440 6062 2141
2016 5 418 6062 2780
2016 6 500 6062 3583
2016 7 249 6062 4184
I would like it to be:
Running Total 3
11
63
133
209
248
334
446
566
661
718
827
1023
1223
1392
1625
1901
2125
2328
2619
2915
3327
3638
3979
4455
4895
5313
5813
6062
you could do it without subquery try
....................)
SELECT a.Jaar
, a.Maand
, a.[Aantal LZP],SUM([Aantal LZP]) over (order by jaar) [Running Total 1]
, SUM([Aantal LZP]) over (partition by jaar order by maand) [Running Total 2],
SUM([Aantal LZP]) over ( order by jaar,maand) [Running Total 3]
FROM CTE AS a
ORDER BY a.Jaar, a.Maand;
old version
SELECT a.Jaar
, a.Maand
, a.[Aantal LZP],(SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar
) [Running Total 1],
(SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar and Maand<=a.Maand
) [Running Total 2]
, (SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE dateadd(year,jaar-1900,dateadd(month,maand-1,0)) <= dateadd(year,a.jaar-1900,dateadd(month,a.maand-1,0))
) [Running Total 3]
FROM CTE AS a
ORDER BY a.Jaar, a.Maand;
Found the Solution but now I must find to integrate it so it responds to my reportparameters Jaar and Maand
DECLARE #SalesTbl TABLE (Jaar int, Maand int, Aantal int, RunningTotal int)
DECLARE #Jaar int,
#Maand int,
#Aantal int,
#RunningTotal int
SET #RunningTotal = 0
DECLARE rt_cursor CURSOR
FOR
WITH CTE AS
(SELECT COUNT(LZP.pv_zaaknummer) AS [Aantal LZP]
, YEAR(LZP.lzp_actief_vanaf) AS Jaar
, MONTH(LZP.lzp_actief_vanaf) AS Maand
FROM dm.crm_LZP_Vn_zaaktype_leefzorgplan_registrerenExtensionBase_hist AS LZP
WHERE LZP.LZP_actief_tot_LDTS > GETDATE()
GROUP BY LZP.lzp_actief_vanaf
)
SELECT A.Jaar, A.Maand, SUM(A.[Aantal LZP])
FROM CTE AS A
GROUP BY Jaar, Maand
ORDER BY A.Jaar, A.Maand
OPEN rt_cursor
FETCH NEXT FROM rt_cursor INTO #Jaar, #Maand, #Aantal
WHILE ##FETCH_STATUS = 0
BEGIN
SET #RunningTotal = #RunningTotal + #Aantal
INSERT #SalesTbl VALUES (#Jaar, #Maand,#Aantal,#RunningTotal)
FETCH NEXT FROM rt_cursor INTO #Jaar, #Maand, #Aantal
END
CLOSE rt_cursor
DEALLOCATE rt_cursor
SELECT * FROM #SalesTbl
I've been trying to adapt other answers here for ages without success, so here goes... I have a basic query:
SELECT
*
FROM
#tbl_counts
ORDER BY
sgId,
CategoryCount DESC,
qccId;
This shows the following results:
sgId qccId CategoryCount
------- -------- -------------
4668 18 8
4668 77 7
4668 2 6
4669 43 2
4669 46 2
4670 25 3
4670 27 3
4670 74 2
4671 56 4
4671 60 3
4671 74 3
4671 54 3
4671 55 3
4671 78 2
4671 88 1
4671 89 1
4671 90 3
I need to amend this query to show the following:
For each unique sgId value, show the top 3 CategoryCount values (with ties if they exist), and the appropriate qccId value. Therefore, the results should be:
sgId qccId CategoryCount
------- ----- -------------
4668 18 8
4668 77 7
4668 2 6 -- top 3 4668
4669 43 2
4669 46 2 -- top 2 4669 because only 2 existed
4670 25 3
4670 27 3
4670 74 2 -- top 3 4670
4671 56 4
4671 60 3
4671 74 3
4671 54 3
4671 55 3 -- top 5 4671 caused by TIES, but discards others
Usually I would ROW_NUMBER here, but am struggling because it doesn't present TIES (I don't think). Therefore on adapting other answers I've found, I've got this far but it doesn't work properly...
SELECT
cnt.*
FROM
(
SELECT DISTINCT
sgId
FROM
#tbl_counts) sg INNER JOIN
(
SELECT TOP 3 WITH TIES
*
FROM
#tbl_counts
ORDER BY
CategoryCount DESC) cnt ON cnt.sgId = sg.sgId
Look like the perfect job for the window function DENSE_RANK:
;WITH
cte AS
(
SELECT *,
DENSE_RANK() OVER (PARTITION BY sgId ORDER BY CategoryCount DESC) As RowRank
FROM #tbl_counts
)
SELECT *
FROM cte
WHERE RowRank <= 3
ORDER BY sgId, CategoryCount DESC, qccId
You could use TOP 1 WITH TIES as you proposed in title:
SELECT TOP 1 WITH TIES *
FROM #tbl_counts
ORDER BY
IIF(DENSE_RANK() OVER(PARTITION BY sgId ORDER BY CategoryCount DESC)<=3,0,1);
LiveDemo
Alright, I've been trying to conceptualize this for a better part of the afternoon and still cannot figure out how to structure this subselect.
The data that I need to report are ages for a given student major grouped by the past 3 fiscal years. Each fiscal year has 3 semesters (summer, fall, spring). I need to have my query grouped on the fiscalyear and agerange fields and then count the distinct student id's.
I currently have this for my SQL statement:
Select COUNT(distinct StuID), AgeRange, FiscalYear
from tblStatic
where Campus like 'World%' and (enrl_act like 'REG%' or enrl_act like 'SCH%')
and StuMaj = 'LAWSC' and FiscalYear IN ('09/10', '10/11', '11/12')
group by FiscalYear, AgeRange
order by FiscalYear, AgeRange
So this is all fine and dandy except it doesn't match my headcount of students for the fiscalyear. The reason being, that people may cross over in the age ranges during the fiscal year and is adding them to my count twice.
How can I use a subselect to resolve this duplicate entry? The field I have been trying to get working is my semester field and using a max to find the max semester during a fiscalyear for a given student.
Data Sample:
Count AgeRange FiscalYear
3 1 to 19 09/10
20 20 to 23 09/10
60 24 to 29 09/10
96 30 to 39 09/10
34 40 to 49 09/10
14 50 to 59 09/10
3 60+ 09/10
2 1 to 19 10/11
24 20 to 23 10/11
73 24 to 29 10/11
109 30 to 39 10/11
43 40 to 49 10/11
11 50 to 59 10/11
2 60+ 10/11
1 1 to 19 11/12
17 20 to 23 11/12
75 24 to 29 11/12
123 30 to 39 11/12
44 40 to 49 11/12
14 50 to 59 11/12
2 60+ 11/12
Solution: (Just got this working and produced my headcounts that match what they are suppose to be)
Select COUNT(distinct S.StuID), AR.AgeRange, S.FiscalYear
from tblStatic S
INNER JOIN
( Select S.StuID, MIN(AgeRange) as AgeRange
From tblStatic S
Group By S.StuID) AR on S.StuID=AR.StuID
where Campus like 'World%' and (enrl_act like 'REG%' or
enrl_act like 'SCH%')
and StuMaj = 'LAWSC' and FiscalYear IN ('09/10', '10/11', '11/12')
group by S.FiscalYear, AR.AgeRange
order by S.FiscalYear, AR.AgeRange
Replace each student's age range with its maximum (or minimum, if you like) age range that fiscal year, then count them:
;
WITH sourceData AS (
SELECT
StudID,
MaxAgeRangeThisFiscalYear = MAX(AgeRange) OVER
(PARTITION BY StudID, FiscalYear),
FiscalYear
FROM tblStatic
WHERE Campus LIKE 'World%'
AND (enrl_act LIKE 'REG%' OR enrl_act LIKE 'SCH%')
AND StuMaj = 'LAWSC'
AND FiscalYear IN ('09/10', '10/11', '11/12')
)
SELECT
FiscalYear,
AgeRange = MaxAgeRangeThisFiscalYear,
Count = COUNT(DISTINCT StudID)
FROM sourceData
GROUP BY
FiscalYear,
MaxAgeRangeThisFiscalYear
ORDER BY
FiscalYear,
MaxAgeRangeThisFiscalYear