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 |
|
Halim1973
Starting Member
2 Posts |
Posted - 2011-07-11 : 19:31:58
|
| I have a table myTable with the following fields: categoryID,Description, SiteID, QtySold, TotalPrice.I use this query to get a report that shows totalQtySold and Total TotalPrice per category per site. SELECT category, SUM(CASE WHEN T.Site = 1 THEN QtySold END) AS [Site 1 Qty Sold],SUM(CASE WHEN T.Site = 1 THEN TotalPrice END) AS [Site 1 total price],SUM(CASE WHEN T.Site = 2 THEN QtySold END) AS [Site 2 Qty Sold],SUM(CASE WHEN T.Site = 2 THEN TotalPrice END) AS [Site 2 total price]FROM myTableGROUP BY category The result of this query looks like:. Site 1 Site 2Category Description Qty Sold Total price Qty Sold Total price 1 General Items 10 25.00 30 34.00 2 Food 20 17.00 45 52.00 3 Etc… 4This query works fine when I know how many sites I have ahead of time but it doesn't help me because The number of sites change all the time. it varies from 1 to 10 or more. is there way I can get a dynamic query that give me similar result set even if I have a variable number of sites.my sites are in a table called Sites which has a SiteID and SiteName as fields.Any help would be greatly appreciated.Thank you,-Halim |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Halim1973
Starting Member
2 Posts |
Posted - 2011-07-12 : 23:54:19
|
| I started building a stored procedure to get the following string based on the number of sites:SUM(CASE WHEN T.Site = 1 THEN QtySold END) AS [Site 1 Qty Sold],SUM(CASE WHEN T.Site = 1 THEN TotalPrice END) AS [Site 1 total price],SUM(CASE WHEN T.Site = 2 THEN QtySold END) AS [Site 2 Qty Sold],SUM(CASE WHEN T.Site = 2 THEN TotalPrice END) AS [Site 2 total price]I built the variable using the following stored procedure:Lets assume it's called: SP1 Declare SITECursor CURSOR FOR Select SiteID,SiteName from Site Declare @SiteID VarChar(10) Declare @SiteName VarChar(50) SELECT @SQL= '''' Open SiteCursor Fetch SiteCursor into @SiteID, @SiteNamewhile @@FETCH_STATUS = 0 begin SELECT @SQL=@SQL+ ''SUM(CASE WHEN I.SiteID =''+ @SiteID +'' THEN U.[Quantity Sold] END) AS [''+ @SiteName +'' Qty Sold],''+ ''SUM(CASE WHEN I.SiteID =''+ @SiteID +'' THEN U.[Non-Taxable Income] +U.[Taxable Income] + U.[TaxFreeSales] END) AS [''+ @SiteName +''total Direct Sale],'' Fetch SiteCursor into @SiteID, @SiteNameendSET @SQL = LEFT(@SQL, LEN(@SQL) - 1)After all that I used another stored procedure to get the result set I want in a crosstab form.the query is:Exec SP1 @SQL OutputSelect Category, @SQL From mytableGroup by CategoryThe result set was not what I was looking for. it hadCategory, (No Column name)1, SUM(CASE WHEN I.SiteID =1 THEN U.[Quantity Sold] END) ...etc,...2, SUM(CASE WHEN I.SiteID =2 THEN U.[Quantity Sold] END) ...etc,...etc..Basically, It was not passing the @SQL as variable, it was using it as a quoted string.I'm sure I'm missing something simple. Any help would be appreciated.Thank you, |
 |
|
|
|
|
|
|
|