August 26, 2010 at 1:05 pm
Hi,
my results looks in this format
Sorce YearMonth CountEntery
A 2003-04 43
B 2003-04 196
A 2003-05 135
B 2003-05 507
i want to the results like below format Using pivot here for yearmonth column will be keep on changing for every month
Source 2003-04 2003-05
A 43 135
B 1964 5079
Please help.. can anyone provide the query for the above scenario.
Thanks
August 26, 2010 at 1:16 pm
Please have a look at the CrossTab article referenced in my signature for an alternative way.
The CrossTab method will also make it a lot easier to return dynamic yearmonth columns if needed. This concept is described in detail in the DynamicCrossTab article also referenced in my signature.
August 26, 2010 at 1:51 pm
The Link you provided is good, im trying it using pivot function in SQL 2008. can you help me with that?
August 26, 2010 at 1:54 pm
sqlbi (8/26/2010)
The Link you provided is good, im trying it using pivot function in SQL 2008. can you help me with that?
That article does an excellent job of walking you through how to do it. Just substitute your columns in the appropriate places.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 26, 2010 at 3:43 pm
i tired in so many ways and im getting error, in my table there are 277 records with different dates like and in nset month i may have 300 records with additional dates... can any one write simple query for my scenario.. thanks
2003-04
2003-04
2003-05
August 26, 2010 at 4:06 pm
Step 1: remove those dozens (or even hudreds) of rather useless lines in your previous post.
Step 2: read the first article referenced in my signature
Step 3: provide ready to use sample data following the guideline described in step 2
Step 4: add your expected output based on your sample data together with what you've tried so far and where you got stuck (I'd expect to see some code based on Jeff's CrossTab article).
We're here to help you but you should also help us help you.
August 26, 2010 at 4:34 pm
sqlbi (8/26/2010)
i tired in so many ways and im getting error...
Cool... let's see what you've tried. Also, take a peek at the first link in my signature line below. It'll help you get better help a lot more quickly.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 26, 2010 at 5:48 pm
sqlbi - You might want to take a look at this post - this poster did everything right for how to get helpful answers. And the answer is right in line with what you need to be looking at for your solution.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 27, 2010 at 4:19 am
I hope this would work for you:
SELECT * FROM (SELECT Source, YearMonth, CountEntery FROM tbl) AS a
PIVOT(SUM(CountEntery) FOR YearMonth IN (2003-04,2003-05)) AS p
August 27, 2010 at 7:38 am
i tried with below query i getting syntax error as
Msg 1033, Level 15, State 1, Line 19
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
Msg 102, Level 15, State 1, Line 21
Incorrect syntax near '2003'.
The script i Used is
with CTETemp
as(
Select CASE
When SourceCode = 'WENR' Then 'A'
When SourceCode = 'WCHG' Then 'B'
When SourceCode = 'EVLV' Then 'C'
Else 'Other'
End as Sorce,
Substring(CONVERT(varchar(19),RegisterTimestamp,120),1,7) as YearMonth,
COUNT(EmailAddrID) As CountEntery
From Warehouse.dbo.tblCustomerEmailAddress
Group By Substring(CONVERT(varchar(19),RegisterTimestamp,120),1,7),
Case When SourceCode = 'WENR' Then 'A'
When SourceCode = 'WCHG' Then 'B'
When SourceCode = 'EVLV' Then 'C'
Else 'Other'
End
Order By Substring(CONVERT(varchar(19),RegisterTimestamp,120),1,7) ,Sorce
)
SELECT * FROM (SELECT Sorce, YearMonth, CountEntery FROM CTETemp) AS a
PIVOT(COUNT(Emailaddrid) FOR YearMonth IN (2003-04,2003-05)) AS p
August 27, 2010 at 7:54 am
take out this line of code...
Order By Substring(CONVERT(varchar(19),RegisterTimestamp,120),1,7) ,Sorce
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 27, 2010 at 8:59 am
still getting Syntax error as
Msg 102, Level 15, State 1, Line 20
Incorrect syntax near '2003'.
August 27, 2010 at 9:19 am
So, where all do you have '2003'?
Oh, it's here:
(2003-04,2003-05))
how about putting those in quotes, like:
('2003-04','2003-05'))
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 27, 2010 at 9:48 am
It doesn't help basically i want it to be in.. wether im doing mistake some where in the script?
2008-022008-032008-042008-052008-06
A 123 234 132 245 300
B 1235 2546 541 1234 457
C 1457 1456 545 145 2546
August 27, 2010 at 10:02 am
sqlbi (8/27/2010)
It doesn't help basically i want it to be in.. wether im doing mistake some where in the script?2008-022008-032008-042008-052008-06
A 123 234 132 245 300
B 1235 2546 541 1234 457
C 1457 1456 545 145 2546
It seems like you really don't want to get help.
You ignore the advice Wayne gave you (even a link showing you the effect of ready to use sample data), Jeffs advice and the stuff I recommended. And I don't think it's a language barrier...
@Wayne: the OP is all yours!
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply