I need some help.
I have a dataset with distinct values, I want to insert that query into temp table
SELECT DISTINCT
T1.C01_CMM_S, T2.C05_CMM_DESC_S Prodname ,T1.C07_CUS_NET_UNTS_D as NetUnit ,T1.c01_GROSS_DATE_T_AS_A_DATE,
T1.NO1_NAM_DESC_1FIELD
INTO #TEMP1 FROM dbo.trns_Agris_shipment T1, dbo.C05_COMMODITY_CODE T2
WHERE T1.C01_CMM_S = T2.C05_COMM_CD_S and T1.c01_tkt_type_s = 'R' and T1.c01_weight_base_s in ( 'F' )
ORDER BY T1.NO1_NAM_DESC_1FIELD asc
Now with this set of rows I want to further do more calculations..
select prodname,NO1_NAM_DESC_1FIELD ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 1 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) JANUARY,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 2 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) FEB ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 3 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) MARCH ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 4 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) APRIL,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 5 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) MAY ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 6 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) JUNE ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 7 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) JULY ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 8 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) AUGUST ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 9 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) SEPT ,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 10 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) OCT,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 11 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) NOV,
sum(case when {fn MONTH( "c01_GROSS_DATE_T_AS_A_DATE")} = 12 then case when "C01_CMM_S" in ('A5', 'B2', 'B5', 'B6', 'D2')
then netunit / 2204.6 when "C01_CMM_S" in ('G1', 'G3', 'L1', 'N1', 'O1') then netunit * 1.1023
when "C01_CMM_S" = 'O2' then netunit / 2000 else netunit end else 0 end) DECE ,
sum(case when {fn MONTH( c01_GROSS_DATE_T_AS_A_DATE)} >= 1 and {fn MONTH( c01_GROSS_DATE_T_AS_A_DATE)} <= 12
then case when C01_CMM_S in ('A5', 'B2', 'B5', 'B6', 'D2') then NETUNIT / 2204.6
when C01_CMM_S in ('G1', 'G3', 'L1', 'N1', 'O1') then NETUNIT * 1.1023
when C01_CMM_S = 'O2' then NETUNIT / 2000 else NETUNIT end else 0 end) TotalYEAR,
sum( case when C01_CMM_S in ('A5', 'B2', 'B5', 'B6', 'D2') then NETUNIT / 2204.6
when C01_CMM_S in ('G1', 'G3', 'L1', 'N1', 'O1') then NETUNIT * 1.1023 when C01_CMM_S = 'O2'
then NETUNIT / 2000 else NETUNIT end) CONVERTEDUNITS
from #TEMP1
WHERE C01_CMM_S in ('A1')
group by prodname,NO1_NAM_DESC_1FIELD
I want all that in one stored proc, its going to be my dataset for my report... Can someone help me ?????????????
FM