SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL pivots - two columns?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 12/01/2013 :  14:35:29  Show Profile  Reply with Quote
I've had a look at similar questions asked but none of the answers seems to fit what I'm trying to achieve.

My existing code (with sample tables) is:

CREATE TABLE [dbo].[CashDetail](
[MappingMenu] [nchar](10) NULL,
[CreatedOn] [datetime] NULL,
[Value] [money] NULL,
[Narrative] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

CREATE TABLE [dbo].[MappingMenu](
[urn] [nchar](10) NULL
) ON [PRIMARY]

GO

INSERT CashDetail VALUES
(1, '2013-11-11 00:00:00', 22.50, 'TEST'),
(2, '2013-11-12 01:22:22', 46.57, 'TEST TWO')


INSERT MappingMenu VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7)

DECLARE @sql NVARCHAR(MAX)
DECLARE @pivotsql VARCHAR(MAX)
DECLARE @columns VARCHAR(MAX)
DECLARE @selectlist VARCHAR(MAX)

SET @columns = N'';
SELECT @columns += N', c.' + QUOTENAME(urn)
FROM (SELECT c.urn FROM Cashbook.MappingMenu AS c
LEFT JOIN Cashbook.CashDetail AS m ON m.MappingMenu = c.urn
GROUP BY c.urn, m.MappingMenu) AS x;

SET @selectlist = N'';
SELECT @selectlist += N', SUM(ISNULL(' + QUOTENAME(urn) + ', 0)) AS ' +
QUOTENAME(urn) FROM (SELECT c.urn FROM Cashbook.MappingMenu AS c
LEFT JOIN Cashbook.CashDetail AS m ON m.MappingMenu = c.urn
GROUP BY c.urn, m.MappingMenu) AS x;

SET @selectlist = STUFF(@selectlist, 1, 1, '');
SET @pivotsql = N'SELECT CONVERT (VARCHAR, CreatedOn, 103) AS Date, ' + @selectlist + '
FROM (SELECT CreatedOn, MappingMenu, Value FROM Cashbook.CashDetail AS m) AS j

PIVOT

(SUM(Value) FOR MappingMenu IN ('+ STUFF(REPLACE(@columns, ', c.[', ',['), 1, 1, '') + ')) AS c
GROUP BY c.CreatedON With ROLLUP
ORDER BY Grouping(c.CreatedON)';

EXEC (@pivotsql)

As you can see, it works perfectly. However (Grrrrr) someone has now asked if it is possible to bring in the narrative for each record.

My SQL pivot skills aren't sharp enough to work out if it's possible.

Thanks in advance for any pointers.

Edited by - Rasta Pickles on 12/01/2013 14:35:49

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/02/2013 :  04:34:24  Show Profile  Reply with Quote
for crosstabbing with multiple pivot columns you need to use a method like this

http://beyondrelational.com/modules/2/blogs/70/posts/10791/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000