![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
I am really not sure what are you trying to filter. Do you want to selectonly a specific column from the pivoted output, or to filter on a date value? If you want to filter a date value, then this will do: N'FROM * * (SELECT sort,doc, dist, area, reg,total, weekdat * * *FROM Totals WHERE weekdat = ''20090626'') as O ' + * * * * * N' PIVOT ' + N'(SUM(total) FOR weekdat IN (' + @listCol + N')) AS P;'; If you need to pick a column, then you need to filter the column list: SELECT *@listCol = STUFF(( SELECT DISTINCT * * * * *'],[' + convert(varchar(10),(Weekdat),1) * * * * *FROM DiaryPct WHERE weekdat = ''20090626'' *... -- Plamen Ratchevhttp://www.SQLStudio.com |
#4
| |||
| |||
|
|
On Dec 1, 4:50*pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote: I am really not sure what are you trying to filter. Do you want to select only a specific column from the pivoted output, or to filter on a date value? If you want to filter a date value, then this will do: N'FROM * * (SELECT sort,doc, dist, area, reg,total, weekdat * * *FROM Totals WHERE weekdat = ''20090626'') as O ' + * * * * * N' PIVOT ' + N'(SUM(total) FOR weekdat IN (' + @listCol + N')) AS P;'; If you need to pick a column, then you need to filter the column list: SELECT *@listCol = STUFF(( SELECT DISTINCT * * * * *'],[' + convert(varchar(10),(Weekdat),1) * * * * *FROM DiaryPct WHERE weekdat = ''20090626'' *... -- Plamen Ratchevhttp://www.SQLStudio.com So sorry for the confusion. *I realized I can't use that stored procedure (the pivot one) because I need the column name (weekdat) to get what I want out of the database then pivot the table. I have a website where a user can select several weekdat's Here's what I want the results to look like if the user selected 6/26/2009 and 7/31/2009 Sort * * * * *Reg * * *6/26/2009 * * * * *7/31/2009 1 * * * PHI * * 13.0 * * * * * *11.5 x * * * 01 * * *14.9 * * * * * *11.5 x * * * 02 * * *24.6 * * * * * *19.8 x * * * 03 * * *6.7 * * * * * * * 5.6 x * * * 04 * * *7.4 * * * * * * * 7.7 x * * * 05 * * *5.4 * * * * * * * 9.2 x * * * 06 * * *10.3 * * * * * *11.6 y * * * 199 * * 23.7 * * * * * * *9.0 y * * * 200 * * 32.6 * * * * * *17.6 Below is the table that I need to write a stored procedure to get the above results. Here's an example: I want to do this to the columns totovr and totpnd: SELECT sort, doc, dist, area, reg, CASE * WHEN SUM(totovr)= 0 THEN 0 * WHEN SUM(totpnd) = 0 THEN 0 * ELSE (convert(decimal(6,1),(((((sum(totovr))) * 1.00) */ (sum(totpnd))) * 100))) * END AS total, weekdat Sort * * * * * Reg * * * * Area Weekdat * * * * * * * * * * * * * * * *totovr * * * totpnd 1 * * * PHI * * PH * * *6/26/2009 12:00:00 AM * * **403 * * * * * * 3092 1 * * * PHI * * PH * * *7/31/2009 12:00:00 AM * * **379 * * * * * * 3297 1 * * * PHI * * PH * * *8/28/2009 12:00:00 AM * * **303 * * * * * * 2896 1 * * * PHI * * PH * * *9/25/2009 12:00:00 AM * * **322 * * * * * * 2909 1 * * * PHI * * PH * * *10/30/2009 12:00:00 AM * * * 282 * * * * * * 2971 x * * * 01 * * *01 * * *6/26/2009 12:00:00 AM * * * * *94 * * * * * * *631 x * * * 02 * * *02 * * *6/26/2009 12:00:00 AM * * * *176 * * * * * * * 716 x * * * 03 * * *03 * * *6/26/2009 12:00:00 AM * * * * *15 * * * * * * *225 x * * * 04 * * *04 * * *6/26/2009 12:00:00 AM * * * * *47 * * * * * * *635 x * * * 05 * * *05 * * *6/26/2009 12:00:00 AM * * * * *22 * * * * * * *411 x * * * 06 * * *06 * * *6/26/2009 12:00:00 AM * * * * *49 * * * * * * *474- Hide quoted text - - Show quoted text - |
#5
| |||
| |||
|
|
On Dec 2, 8:28*am, JJ297 <nc... (AT) yahoo (DOT) com> wrote: On Dec 1, 4:50*pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote: I am really not sure what are you trying to filter. Do you want to select only a specific column from the pivoted output, or to filter on a date value? If you want to filter a date value, then this will do: N'FROM * * (SELECT sort,doc, dist, area, reg,total, weekdat * * *FROM Totals WHERE weekdat = ''20090626'') as O ' + * * * * * N' PIVOT ' + N'(SUM(total) FOR weekdat IN (' + @listCol + N')) AS P;'; If you need to pick a column, then you need to filter the column list: SELECT *@listCol = STUFF(( SELECT DISTINCT * * * * *'],[' + convert(varchar(10),(Weekdat),1) * * * * *FROM DiaryPct WHERE weekdat = ''20090626'' *.... -- Plamen Ratchevhttp://www.SQLStudio.com So sorry for the confusion. *I realized I can't use that stored procedure (the pivot one) because I need the column name (weekdat) to get what I want out of the database then pivot the table. I have a website where a user can select several weekdat's Here's what I want the results to look like if the user selected 6/26/2009 and 7/31/2009 Sort * * * * *Reg * * *6/26/2009 * * * * *7/31/2009 1 * * * PHI * * 13.0 * * * * * *11.5 x * * * 01 * * *14.9 * * * * * *11.5 x * * * 02 * * *24.6 * * * * * *19.8 x * * * 03 * * *6.7 * * * * * * * 5.6 x * * * 04 * * *7.4 * * * * * * * 7.7 x * * * 05 * * *5.4 * * * * * * * 9.2 x * * * 06 * * *10.3 * * * * * *11.6 y * * * 199 * * 23.7 * * * * * * *9.0 y * * * 200 * * 32.6 * * * * * *17.6 Below is the table that I need to write a stored procedure to get the above results. Here's an example: I want to do this to the columns totovr and totpnd: SELECT sort, doc, dist, area, reg, CASE * WHEN SUM(totovr)= 0 THEN 0 * WHEN SUM(totpnd) = 0 THEN 0 * ELSE (convert(decimal(6,1),(((((sum(totovr))) * 1.00) */ (sum(totpnd))) * 100))) * END AS total, weekdat Sort * * * * * Reg * * * * Area Weekdat * * * * * * * * * * * * * * * *totovr * * * totpnd 1 * * * PHI * * PH * * *6/26/2009 12:00:00 AM * * * *403 * * * * * * 3092 1 * * * PHI * * PH * * *7/31/2009 12:00:00 AM * * * *379 * * * * * * 3297 1 * * * PHI * * PH * * *8/28/2009 12:00:00 AM * * * *303 * * * * * * 2896 1 * * * PHI * * PH * * *9/25/2009 12:00:00 AM * * * *322 * * * * * * 2909 1 * * * PHI * * PH * * *10/30/2009 12:00:00 AM * * * 282 * * * * * * 2971 x * * * 01 * * *01 * * *6/26/2009 12:00:00 AM * ** * *94 * * * * * * *631 x * * * 02 * * *02 * * *6/26/2009 12:00:00 AM * ** *176 * * * * * * * 716 x * * * 03 * * *03 * * *6/26/2009 12:00:00 AM * ** * *15 * * * * * * *225 x * * * 04 * * *04 * * *6/26/2009 12:00:00 AM * ** * *47 * * * * * * *635 x * * * 05 * * *05 * * *6/26/2009 12:00:00 AM * ** * *22 * * * * * * *411 x * * * 06 * * *06 * * *6/26/2009 12:00:00 AM * ** * *49 * * * * * * *474- Hide quoted text - - Show quoted text - Sorry here's the table it got messed up. Sort * Reg *Area * * Weekdat * * * * * * * * * * * * totovr totpnd 1 * * * PHI * * PH * *6/26/2009 12:00:00 AM * * *403 * * * * 3092 1 * * * PHI * * PH * *7/31/2009 12:00:00 AM * * *379 * * * * 3297 1 * * * PHI * * PH * *8/28/2009 12:00:00 AM * * *303 * * * * 2896 1 * * * PHI * * PH * *9/25/2009 12:00:00 AM * * *322 * * * * 2909 1 * * * PHI * * PH * *10/30/2009 12:00:00 AM * *282 ** * * 2971 x * * * 01 * * *01 * * *6/26/2009 12:00:00 AM * * * 94 * * * * * 631 x * * * 02 * * *02 * * *6/26/2009 12:00:00 AM * * 176 * * * * * 716 x * * * 03 * * *03 * * *6/26/2009 12:00:00 AM * * *15 * * * * * *225 x * * * 04 * * *04 * * *6/26/2009 12:00:00 AM * * *47 * * * * * *635 x * * * 05 * * *05 * * *6/26/2009 12:00:00 AM * * *22 * * * * * *411 x * * * 06 * * *06 * * *6/26/2009 12:00:00 AM * * *49 * * * * * *474- Hide quoted text - - Show quoted text - |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
You need to use the exact same pivoting as before. Since it is dynamic and you already filtered the data for the two dates it will automatically generate pivoted data with two columns for each date. -- Plamen Ratchevhttp://www.SQLStudio.com |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
You simply need to change the WHERE clause of the query to filter on onlyone date: WHERE weekdat = '20090626' Alternatively, as I noted in my first post you can do the filtering when you build the dynamic column list: SELECT *@listCol = STUFF(( SELECT DISTINCT * * * * *'],[' + convert(varchar(10),(Weekdat),1) * * * * *FROM DiaryPct WHERE weekdat = ''20090626'' *... -- Plamen Ratchevhttp://www.SQLStudio.com |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |