SQL Server to show data for each month from table - sql

How to populate or display the result for each month?
I have a table with four columns
Plan Status Creation date Triggers
I need to get the results for each plan each month how many triggers count are there. my report should look like this.
Plan Jan feb march Apr may jun jul aug sep oct nov dec Totaol
001 2 0 1 1 0 1 1 1 2 3 1 7 21
002 2 0 1 1 0 1 1 1 2 3 1 7 21
003 2 0 1 1 0 1 1 1 2 3 1 7 21
Could you please help me out how to achieve this results?
Thanks in advance.

select
plan,
sum(case when datepart(mm,[Creation date]) = 1 then 1 else 0 end) as Jan,
sum(case when datepart(mm,[Creation date]) = 2 then 1 else 0 end) as Feb,
...
sum(triggers) as Total
from table
where Status = 'SomeStatus'
group by Plan

Related

SQL show UserCode from the highest value in column

I am making a select from a table that has alums, but those alums can have a "level" being 01, 02, 03, 04, 05 or 99 and if I select a distinct(code) there will be 2 or 3 files of the same "code" with different "level". I want to be able to ONLY get the "code" with the higher "level" anytime.
Example of my code:
select
Level,
count(Code) as Atendee,
count(CASE WHEN CodPlace = 01 THEN (Atendee) ELSE null END ) as AtendeeCho,
count(CASE WHEN CodPlace = 02 THEN (Atendee) ELSE null END ) as AtendeeSB,
count(CASE WHEN CodPlace = 14 THEN (Atendee) ELSE null END ) as AtendeeIca
from #TempoPar
group by Level
order by Level
And here is the result:
Level Atendee AtendeeCho AtendeeSB AtendeeIca
1 3 2 0 1
2 0 0 0 0
3 2 2 0 0
99 1 1 0 0
Problem is that the one who is in AtendeeCho from level 1, 2 and 99 is the same person and he should only be on 99 or 3 if he wasn't on 99.
Thanks in advance!
Data example:
Code ( user) Level Career CodPlace
12345 1 9 01
12345 3 15 01
12346 1 10 14
12347 1 10 01
12345 3 15 01
12347 99 15 01
Now, this is what I require:
From the data sample:
Code ( user) Level Career CodPlace
12345 3 15 01
12346 1 10 14
12347 99 15 01
In the final output with the corrected data it should be:
Level Atendee AtendeeCho AtendeeSB AtendeeIca
1 1 0 0 1
2 0 0 0 0
3 1 1 0 0
99 1 1 0 0

healthcare enrollment verification of claims

All:
I'm trying to verify if a hospital stay should be covered based on enrollment data using SAS.
There are two tables:
"enr" : 2 years of a binary (0/1) if the patient was enrolled on that month.
data enr;
input id year jan feb mar apr may jun jul aug sep oct nov dec;
cards;
1 2018 0 0 0 1 1 0 0 0 0 0 0 1
1 2019 1 1 1 1 1 1 1 1 1 1 1 1
2 2018 1 1 0 0 1 1 1 1 1 1 1 1
2 2019 1 1 1 1 1 1 0 0 0 0 0 0
;
"clms" : id, admit_dt, discharge_dt, dr_id, cost
data clms;
input id admit_dt mmddyy10. #14dischrg_dt mmddyy10. code dr_id cost;
format admit_dt dischrg_dt mmddyy10.;
cards;
1 02/01/2019 02/06/2019 470 2 800
1 06/01/2019 06/15/2019 251 30 400
2 10/18/2018 10/22/2018 871 5 250
2 05/18/2019 05/22/2019 999 3 1000
;
For each "id" on the claims table, I wish to identify claims to be paid under the rule: for a paid claim, the patient must be enrolled in the 4 months prior and 4 months after the date of admission(admit_dt).
One of the tricky issues is the format of the months on the enrollment table(character, e.g."feb") differs from that of the admission date mmddyyy10 on the claims table.
Should I use an sql solution?
Transpose the enr data, compute which months are banded together, then perform a SQL join.
Example:
data enr_tall;
set enr;
array months jan feb mar apr may jun jul aug sep oct nov dec;
do i = 1 to 12;
month = mdy (i,1,year);
enrolled = months[i];
output;
end;
keep id month enrolled;
run;
data enr_tall_bands;
set enr_tall;
by id month;
retain count4;
if first.id then do;
count4 = 0;
band + 1;
end;
if ^first.id and not enrolled and count4 > 0 then do;
count4 = 0;
band + 1;
end;
if enrolled then do;
if count4 = 0 then band + 1;
count4 + 1;
end;
run;
proc sql;
create table news as
select
clms.*
, ( select count(distinct band) = 1 and min(enrolled) = 1
from enr_tall_bands as bands
where
bands.id = clms.id
& admit_dt between
intnx('month', month, -4)
and
intnx('month', month, 4)
) as
payable
from clms;

Postgres query for annual sales report by rep. grouped by month

I would like to create an annual sales report table by sales rep, showing all the twelve month. The data I have is more or less like in this example:
id | rep | date | price
----------------------------
1 1 2017-01-01 20
2 1 2017-01-20 44
3 2 2017-02-18 13
4 2 2017-03-08 12
5 2 2017-04-01 88
6 2 2017-09-05 67
7 3 2017-01-31 10
8 3 2017-06-01 74
The result I need would be like this:
Rep Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
----------------------------------------------------
1 64 0 0 0 0 0 0 0 0 0 0 0
2 0 13 12 88 0 0 0 0 67 0 0 0
3 10 0 0 0 0 74 0 0 0 0 0 0
What would be the most efficient way to write this query?
One way:
select rep,
sum(case when extract('month' from date) = 1 then price else 0 end ) as Jan,
sum(case when extract('month' from date) = 2 then price else 0 end ) as Feb
-- another months here
from t
group by rep
One way is to use windowed functions with FILTER:
SELECT DISTINCT
"rep",
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 1) OVER(PARTITION BY "rep"),0) AS Jan,
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 2) OVER(PARTITION BY "rep"),0) AS Feb
--....
FROM ta;
Rextester Demo
Warning!
You probably want to partition by YEAR too to avoid summing JAN 2017 and JAN 2018.

I have been trying to write a below SQL query ande getting result as given below the query

select
TRN_ADP_Items_Tracker.request_id,
TRN_ADP_Stationary_Request.requester_name,
item_id,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=6 then sum(item_qty_traded) else 0 end as JUNE,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=7 then sum(item_qty_traded) else 0 end as JULY,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=8 then sum(item_qty_traded) else 0 end as AUG
from TRN_ADP_Items_Tracker
join TRN_ADP_Stationary_Request
on TRN_ADP_Items_Tracker.request_id = TRN_ADP_Stationary_Request.stationary_request_id
group by TRN_ADP_Stationary_Request.requester_name,item_id,
MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE);
request requester item
_id _name _id JUNE JULY AUG
3 Prasad 150 2 0 0
3 Prasad 154 2 0 0
1 Sneha 150 15 0 0
1 Sneha 150 0 15 0
1 Sneha 150 0 0 6
1 Sneha 154 4 0 0
But here I want Result like:-
request_id requester_name item_id JUNE JULY AUG
1 Sneha 150 15 15 6
if 1st 3 columns data is same then then months data should be come in 1 row, as given above.
you need to correct your GROUP_ID to have only three columns from your select
select
TRN_ADP_Items_Tracker.request_id,
TRN_ADP_Stationary_Request.requester_name,
item_id
from
group by TRN_ADP_Stationary_Request.requester_name,item_id,
MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)
Need to take out request_id column from select list.
select
--TRN_ADP_Items_Tracker.request_id,
TRN_ADP_Stationary_Request.requester_name,
item_id,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=6 then sum(item_qty_traded) else 0 end as JUNE,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=7 then sum(item_qty_traded) else 0 end as JULY,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=8 then sum(item_qty_traded) else 0 end as AUG
from TRN_ADP_Items_Tracker
join TRN_ADP_Stationary_Request
on TRN_ADP_Items_Tracker.request_id = TRN_ADP_Stationary_Request.stationary_request_id
group by TRN_ADP_Stationary_Request.requester_name,item_id,
MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE);

Aggregate values with multiple conditions

I've searched the forum but can't quite find what I'm looking for. Apologies if this has already been answered.
I have a table with the following example values:
FY Period Version Value
2013 3 1 9954
2013 3 2 9954
2013 4 1 11498
2013 4 2 11498
2013 4 3 11498
2014 1 1 448
2014 1 2 448
2014 1 3 0
2014 2 1 3150
2014 2 2 3150
2014 3 1 0
2014 3 2 0
2014 3 3 5059
2014 4 1 11118
2014 4 2 0
2014 4 3 11118
I'm looking to sum the values for the highest version number, within each period and each FY, so the expected result for this particular data set would be:
(9954 + 11498 + 0 + 3150 + 5059 + 11118) = 40,779
I've done something similar previously with the over partition approach but i can't get it to work on this data set. Any pointers would be greatly appreciated.
A simple approach is to use row_number():
select sum(value)
from (select t.*,
row_number() over (partition by fy, period order by version desc) as seqnum
from table t
) t
where seqnum = 1;