sql server - SQL - Get Sum of Values with same Date -
i'm sure i've done type of operation 1000 times before reason not working me. i'm doing report determine if patient receive medication on day. regardless if 1 or 5 doses in day value should 1. staff corrections on system, come in negative values. need sum of dose value each day, if + value 1, otherwise 0.
all want accomplish @ point have 1 row each date either 1 or 0.
here sql query sum values:
select dim_drug_name_short.drug_name_short 'med_name_short' , sum(baseline.doses) 'dot' , day(baseline.dispense_date) 'd_date' fact_ams_baseline_report baseline inner join dim_drug_name_short on baseline.med_name_id = dim_drug_name_short.drug_name_long inner join dim_date tdate on baseline.dispense_date = tdate.date baseline.encounter = '00000001/01' group dim_drug_name_short.drug_name_short , baseline.dispense_date , doses order drug_name_short
for time being i'm pulling 1 encounter out of data set test with. output i'm getting. included day in select show same day coming through twice , not getting summed.
here sample of output get:
med_name_short dot day of month ceftriaxone 1 15 ceftriaxone 1 16 ceftriaxone 4 16 ceftriaxone 1 17 ceftriaxone 1 18 ceftriaxone 1 20 ceftriaxone -3 21 ceftriaxone 1 21 ceftriaxone -1 23 propranolol -1 24 propranolol 3 24 propranolol 1 25 propranolol 2 26 propranolol 2 27
what hoping see in day 16 5, day 21 -2 , day 24 -2.
any assistance appreciated. thanks
i don't think should grouping doses. without seeing data, can guess that, example, there 2 doses of quantity 2 on 16th.
so try:
select dim_drug_name_short.drug_name_short 'med_name_short' , sum(baseline.doses) 'dot' , day(baseline.dispense_date) 'd_date' fact_ams_baseline_report baseline inner join dim_drug_name_short on baseline.med_name_id = dim_drug_name_short.drug_name_long inner join dim_date tdate on baseline.dispense_date = tdate.date baseline.encounter = '00000001/01' group dim_drug_name_short.drug_name_short , baseline.dispense_date order drug_name_short
Comments
Post a Comment