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 |
|
mike1973
Starting Member
46 Posts |
Posted - 2010-05-07 : 03:52:09
|
| hey guys,i have this query that contains 2 subs, is it possible to change it to display the types as columns and not in rows? I mean using the following structureCategoryID,CategoryAR,CategoryEN,TotalSalesWeight,TotalSalesPrice,TotalSalesPriceUSD,TotalPurchaseWeight,TotalPurchasePrice,TotalPurchasePriceUSDSelect Distinct(SP.CategoryAutoID),Categories.CategoryAR, Categories.CategoryEN, SUM(SP.TotalWeight) As TotalWeight,SUM(SP.TotalWeight * SP.UnitPrice) AS SoldAmount, CONVERT(Decimal(18,2),SUM(SP.TotalWeight * SP.UnitPrice/ex1.value)*1000) AS SoldAmountUSD, 'Sales' AS TypeFrom SalesProducts AS SPJOIN Categories ON Categories.CategoryAutoID=SP.CategoryAutoIDLEFT JOIN Exchange As ex1 ON ex1.DateExchange=SP.DateSoldWhere DateSold BETWEEN CONVERT(DATETIME,@StartDate,101) + ' 12:00:00 AM' AND CONVERT(DATETIME,@EndDate,101) + '11:59:59 PM'Group By SP.CategoryAutoID, Categories.CategoryAR,Categories.CategoryENUNIONSelect Distinct(DO.CategoryID),Categories.CategoryAR, Categories.CategoryEN, SUM(DO.Weight) As TotalWeight,SUM(DO.Weight * DO.UnitPrice) AS SoldAmount, CONVERT(Decimal(18,2),SUM(DO.Weight * DO.UnitPrice/ex1.value)*1000) AS SoldAmountUSD, 'Purchases' AS TypeFrom DailyOperations AS DOJOIN Categories ON Categories.CategoryAutoID=DO.CategoryIDLEFT JOIN Exchange As ex1 ON ex1.DateExchange=DO.DateOperatedWhere DateOperated BETWEEN CONVERT(DATETIME,@StartDate,101) + ' 12:00:00 AM' AND CONVERT(DATETIME,@EndDate,101) + '11:59:59 PM'Group By DO.CategoryID, Categories.CategoryAR,Categories.CategoryENORDER BY Categories.CategoryENThanks a lot for your helpMike |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-05-07 : 06:57:02
|
| You must do this from you front end application because backend is concern with the data not the display.Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2010-05-07 : 07:03:40
|
| How i can do this? i tried but not able to bind it to a grid |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-05-07 : 07:17:26
|
| But what sholud be the data under the columns.From what I see there is just 'Sales' and 'Purchases' under the column Type.PBUH |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2010-05-07 : 09:17:28
|
| here is my target format:Catergory SalesTotalWeight SalesTotalPrice SalesTotalPriceUSD PurchaseTotalWeight SalesTotalPrice SalesTotalPriceUSD Category1 - - - - - -Category2 - - - - - -Category3 - - - - - -Category4 - - - - - -Category5 - - - - - -Category6 - - - - - -Category7 - - - - - -Category8 - - - - - - |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-05-07 : 09:32:32
|
Well you will have to use something like thisselect sum(case when Type='Sales' then TotalWeight end) as PurchaseTotalWeight, sum(case when Type='Purchase' then TotalWeight end) as PurchaseTotalWeight and so onfrom(Your original query)T PBUH |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2010-05-08 : 03:50:51
|
Using the following query selectcase when Type='Purchases' then CATID end as CATID,case when Type='Purchases' then CAT end as CAT,case when Type='Purchases' then ORDERCAT end as ORDERCAT,case when Type='Purchases' then TotalWeight end as PurchaseTotalWeight, case when Type='Purchases' then SoldAmountUSD end as PurchaseAmountUSD, case when Type='Purchases' then SoldAmount end as PurchaseAmount, case when Type='Sales' then CATID end as CATID,case when Type='Sales' then CAT end as CAT, case when Type='Sales' then ORDERCAT end as ORDERCAT,case when Type='Sales' then TotalWeight end as SalesTotalWeight, case when Type='Sales' then SoldAmount end as SalesAmount, case when Type='Sales' then SoldAmountUSD end as SalesAmountUSD--case when Type='Sales' then CAT end as CAT,from( Select Distinct(SP.CategoryAutoID) AS CATID,Categories.CategoryAR AS CAT, Categories.CategoryEN AS ORDERCAT, SUM(SP.TotalWeight) As TotalWeight,SUM(SP.TotalWeight * SP.UnitPrice) AS SoldAmount, CONVERT(Decimal(18,2),SUM(SP.TotalWeight * SP.UnitPrice/ex1.value)*1000) AS SoldAmountUSD, 'Sales' AS TypeFrom SalesProducts AS SPJOIN Categories ON Categories.CategoryAutoID=SP.CategoryAutoIDLEFT JOIN Exchange As ex1 ON ex1.DateExchange=SP.DateSoldWhere DateSold BETWEEN CONVERT(DATETIME,@StartDate,101) + ' 12:00:00 AM' AND CONVERT(DATETIME,@EndDate,101) + '11:59:59 PM'Group By SP.CategoryAutoID, Categories.CategoryAR,Categories.CategoryEN UNIONSelect Distinct(DO.CategoryID) AS CATID,Categories.CategoryAR AS CAT, Categories.CategoryEN AS ORDERCAT, SUM(DO.Weight) As TotalWeight,SUM(DO.Weight * DO.UnitPrice) AS SoldAmount, CONVERT(Decimal(18,2),SUM(DO.Weight * DO.UnitPrice/ex1.value)*1000) AS SoldAmountUSD, 'Purchases' AS TypeFrom DailyOperations AS DOJOIN Categories ON Categories.CategoryAutoID=DO.CategoryIDLEFT JOIN Exchange As ex1 ON ex1.DateExchange=DO.DateOperatedWhere DateOperated BETWEEN CONVERT(DATETIME,@StartDate,101) + ' 12:00:00 AM' AND CONVERT(DATETIME,@EndDate,101) + '11:59:59 PM'Group By DO.CategoryID, Categories.CategoryAR,Categories.CategoryEN)T i got the following results |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2010-05-09 : 06:06:39
|
| Anyone can help with this one please? |
 |
|
|
|
|
|
|
|