Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
papershop
Starting Member
27 Posts |
Posted - 2011-08-10 : 21:53:15
|
| Dear All i want to ask about this :if i have table sales and it's fill with amount sales and month saleslike this :product amountsales month a 100 1a 150 1a 200 2a 250 2a 150 2a 75 3a 175 3a 150 4a 200 4a 105 5and i would like to have report that sum amount sales by product and month like thisproduct month1 month2 month3 month4 month5 a 250 600 250 350 105please help me@papershop |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-08-10 : 21:57:23
|
[code]select *from sales pivot ( sum(amountsales) for month in ([1], [2], [3], [4], [5]) ) p[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
papershop
Starting Member
27 Posts |
Posted - 2011-08-10 : 22:41:45
|
| my data_month product amountsales1 ACTHIB 6376361 TRIOVAX 2545441 EAX B 3178171 EAX B 1191811 PIACEL 6772721 PIACEL 13545441 PIACEL 15238621 TIM 1554551 TACT HIB 2430001 AHIB 9086312 NCORT AQ -1722002 NGIN 317392 NGIN -317392 FYL 786702 FLAGYL -786702 AML -2204122 AML 2204122 AML M 1450142 AML M 1321713 PICEL 32593713 OKAX 2681253 ACIB 15881253 VARIP 753413 TECT HIB 4158003 ACIB 15881253 VARIP 1126563 EUX B 114000and then i use pivotselect familyname, 1,2,3from (select _month, familyname, valuenet from dbo.v_fact_salesWHERE (DATEPART(m, inv_date) >=1) AND (DATEPART(m, inv_date) <=3) and (_Year = YEAR(GETDATE())))uppivot (sum(valuenet) for _month in ("1","2","3") )as pvtorder by familynamebut the result isfamilyname noname noname nonameACTHIB 1 2 3ACTONEL 1 2 3AMARYL 1 2 3AMARYL M 1 2 3AMLOGRIX 1 2 3can anyone fix my syntax so the result will be like thisfamilyname 1noname nonameACTHIB 1 2 3ACTONEL 1 2 3AMARYL 1 2 3AMARYL M 1 2 3AMLOGRIX 1 2 3@papershop |
 |
|
|
papershop
Starting Member
27 Posts |
Posted - 2011-08-10 : 22:45:07
|
| my data_month product amountsales1 ACTHIB 6376361 TRIOVAX 2545441 EAX B 3178171 EAX B 1191811 PIACEL 6772721 PIACEL 13545441 PIACEL 15238621 TIM 1554551 TACT HIB 2430001 AHIB 9086312 NCORT AQ -1722002 NGIN 317392 NGIN -317392 FYL 786702 FLAGYL -786702 AML -2204122 AML 2204122 AML M 1450142 AML M 1321713 PICEL 32593713 OKAX 2681253 ACIB 15881253 VARIP 753413 TECT HIB 4158003 ACIB 15881253 VARIP 1126563 EUX B 114000and then i use pivotselect familyname, 1,2,3from (select _month, familyname, valuenet from dbo.v_fact_salesWHERE (DATEPART(m, inv_date) >=1) AND (DATEPART(m, inv_date) <=3) and (_Year = YEAR(GETDATE())))uppivot (sum(valuenet) for _month in ("1","2","3") )as pvtorder by familynamebut the result isfamilyname noname noname nonameACTHIB 1 2 3ACTONEL 1 2 3AMARYL 1 2 3AMARYL M 1 2 3AMLOGRIX 1 2 3can anyone fix my syntax so the result will be like thisfamilyname 1 2 3ACTHIB 100 200 300ACTONEL . AMARYL AMARYL M AMLOGRIX @papershop |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-08-10 : 22:52:30
|
[code]select familyname, [1], [2], [3]from (select _month, familyname, valuenet from dbo.v_fact_salesWHERE (DATEPART(m, inv_date) >=1) AND (DATEPART(m, inv_date) <=3) and (_Year = YEAR(GETDATE())))uppivot (sum(valuenet) for _month in ("1","2","3") )as pvtorder by familyname[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|