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 |
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-23 : 10:21:35
|
| HelloI am getting the Incorrect syntax near the keyword 'Pivot for the following Query.Please adviseSELECT X,Y, Z, A, B, C,D, E, Agent, Daily1Day, DAILYATTN, '5/21/2012', '05/22/2012', '05/23/12'(SELECT ReportDate, RptFunction, Center, AMATTUID, AM, TMATTUID,TM, ATTUID, Agent, Daily1Day, [1DayDailyAttain] AS DAILYATTN, (SELECT MAX([F]) FROM dbo.t_Month) AS OnedayResFROM dbo.q_AgentWHERE (INT_EXT = 'i'))PIVOT ( MAX(OnedayRes) FOR OnedayRes IN ('5/21/2012', '05/22/2012','05/23/12') ) AS pvt |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-05-23 : 10:41:25
|
[code](SELECT a.ReportDate AS X, a.RptFunction AS Y, a.Center AS Z, a.AMATTUID AS A, a.AM AS B, a.TMATTUID AS C, a.TM AS D, a.ATTUID AS E, a.Agent, a.Daily1Day, a.[1DayDailyAttain] AS DailyAttn, f.a AS [5/21/2012], f.b AS [05/22/2012], f.c AS [05/23/12]FROM dbo.q_Agent AS aOUTER APPLY ( SELECT MAX(CASE WHEN F = '20120521' THEN CAST('20120521' AS DATE) ELSE NULL END) AS a, MAX(CASE WHEN F = '20120522' THEN CAST('20120522' AS DATE) ELSE NULL END) AS b, MAX(CASE WHEN F = '20120523' THEN CAST('20120523' AS DATE) ELSE NULL END) AS c FROM dbo.t_Month WHERE F >= '20120521' AND F < '20120524' ) AS f(a, b, c)WHERE a.INT_EXT = 'i'[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-23 : 11:10:45
|
| Thanks for the above queryThis is my actual query SELECT ReportDate, RptFunction, Center, Agent, Daily1Day, [1DayDailyAttain],(SELECT [24 Hr Res]FROM dbo.t_Month) AS OnedayRes,(SELECT ReportDateFROM dbo.t_Month WHERE (ReportDate = '2012-05-22')) AS RptDateFROM dbo.q_AgentWHERE (INT_EXT = 'i') AND (ReportDate = '2012-05-22')which currently pulls the data for the report date mentioned and everyday the data gets appended to the last of the rowBut my requirement is that the data output should have the dates coming from [24 Hour res] and the results for each day from Daily1 day and[1DayDailyAttain] should be under the corresponding the [24 hour res]date Columnthe output data in 1daydaily attain should be conditionally formatted like if (1DayDailyAttain0> 1+ the data should be bold green,0.98-0.99 should be bold yellow and <0.95 should be bold redBut My users wants to see the data daily so they can compare to previous day as the data changes everyday which helps them calculate their performanceRight nowit is Rptfunction..........Agent Daily1day 24Hour Res ABC 1.1 5/22.......... XYZ 1.2 5/23some thing likeRpt function ....... agent 5/22 5/23 5/24......... .... XYZ 1.1 1.2 1.6....................... ABC 0.99 0.98 1.00Above dates come from the [24 Hr Res] column name and 1.1,1.2... come from daily1 day columnI hope this makes senseI used the report date in my query just to make sure I am getting the correct data from both the tables.Your help is greatly appreciated.ThanksJ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-23 : 15:27:22
|
| Why not do this pivoting in reports? If you're using sql reporting services,you can very easily achieve it using matrix container in ssrs------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-23 : 15:41:24
|
| I am using SQL server as we do not have access to reporting servicesCan you help me with the query using pivot? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-23 : 15:44:00
|
| so are you asking to pivot lutiple columns based on condition?like Daily1Day, 1DayDailyAttain], etc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-23 : 15:53:19
|
| to make it simler to understandI would need my data like this:RptFunction, Center, Agent 5/22 5/23 5/24Data data data 1.1/1.45 1.2/0.99 1.3/0.98Since he data changes everyday ,agents need to compare their data to previous date and so on... 5/22 comes from [24 Hour res] in the t_Month table and 1.1/1.45 ,1.2...... comes from Daily1day and 1 day daily attain in the agents tableThe common column between these two tables is teh reportdate which is set to get the sysdate |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-23 : 16:33:36
|
| Agent TableCREATE TABLE [dbo].[q_Agent]( [RptFunction] [nvarchar](255) NULL, [Center] [nvarchar](255) NULL, [AMATTUID] [nvarchar](255) NULL, [AM] [nvarchar](255) NULL, [TMATTUID] [nvarchar](255) NULL, [TM] [nvarchar](255) NULL, [ATTUID] [nvarchar](255) NULL, [Agent] [nvarchar](255) NULL, [ReportDate] [date] NOT NULLMonth tableCREATE TABLE [dbo].[t_Month]([Month] [datetime] NULL, [Daily] [datetime] NULL, [24 Hour Res] [datetime] NULL, [ReportDate] [date] NOT NULLThis is what i have so far SELECT ReportDate, RptFunction, Center, AMATTUID, AM, TMATTUID,TM, ATTUID, Agent, Daily1Day, DAILYATTN, '5/21/2012', '05/22/2012', '05/23/12',(SELECT ReportDate, RptFunction, Center, AMATTUID, AM, TMATTUID,TM, ATTUID, Agent, Daily1Day, [1DayDailyAttain] AS DAILYATTN, (SELECT [24 Hour Res] FROM dbo.t_Month) AS OnedayResFROM dbo.q_AgentWHERE (INT_EXT = 'i')) PIVOT ((Daily1Day) FOR Daily1Day IN OnedayRes(Case when Reportdate='5/22/2012 then [24 Hr Res] ='5/20/2012'Case when Reportdate='5/23/2012 then [24 Hr Res]= '05/21/2012' ) )AS pvtWith this query I am getting an incorrect syntax at PivotMy out put should beFunction Center Rep ID Representative. ------ 5/20 5/21 5/22 5/23 XYZ ABC ag194u GILE ------ 0.98 0.98 0.98 0.98 XYZ ABC as263s SWANSON ------ 1.01 1.01 1.01 1.01 XYZ ABC cj680t JOHNSON0 ------- .97 0.97 0.97 0.97 The dates comes from the [24 hr res] column in Month table and the results for the dates comes from daily1day and 1 daydailyattain in agent table |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-24 : 08:47:22
|
| Please help with the above query |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-24 : 15:35:35
|
You're still not giving us required information. I cant even see columns daily1day and 1 daydailyattain in posted info so dont know how you want it to be pivoted. Anyways it should be something like below (i'm just showing for one of columns)SELECT *FROM(SELECT a.RptFunction,a.Center,a.[Rep ID],a.Representative,m.[24 Hour Res],a.daily1dayFROM [dbo].[q_Agent] aINNER JOIN [dbo].[t_Month] mON m.ReportDate = a.ReportDate)mPIVOT (MAX(daily1day) FOR [24 Hour Res] IN ([5/20],[5/21],...,[5/23]))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-24 : 16:59:05
|
| Sorry about that.Here it is from the agent tableCREATE TABLE [dbo].[q_Agent]( [RptFunction] [nvarchar](255) NULL, [Center] [nvarchar](255) NULL, [AMATTUID] [nvarchar](255) NULL, [AM] [nvarchar](255) NULL, [TMATTUID] [nvarchar](255) NULL, [TM] [nvarchar](255) NULL, [ATTUID] [nvarchar](255) NULL, [Agent] [nvarchar](255) NULL, [Daily1Day] [float] NULL, [1DayDailyAttain] [float] NULL, and the month table[dbo].[t_Month]( [24 Hour Res] [datetime] NULL, [ReportDate] [date] NOT NULL |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-24 : 23:47:15
|
| Please help with this requirement |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-25 : 09:52:33
|
quote: Originally posted by Julie19 Please help with this requirement
extend the last suggestion to include the extra columns also. Dont wait for someone to give you spoonfed solution!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-25 : 11:53:57
|
| This query workedBut I also want to include the 1DaydailyAttain similar to Daily1day in the pivot for the same datesand also filter the query to use where Int_Ext='i' from the agents table |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-25 : 15:29:45
|
quote: Originally posted by Julie19 This query workedBut I also want to include the 1DaydailyAttain similar to Daily1day in the pivot for the same datesand also filter the query to use where Int_Ext='i' from the agents table
so how would output be?for each date you want both columns side by side?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Julie19
Starting Member
32 Posts |
Posted - 2012-05-25 : 16:34:07
|
| Yes,for each date I want both columns |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|
|
|