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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get ROWs as COLUMNs
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aluna98
Starting Member

Ecuador
1 Posts

Posted - 08/22/2012 :  10:09:34  Show Profile  Reply with Quote
Hello

I'm using MS SQL 2008 R2, have three tables with following schema:

Table 1: Contains workshift info for each worker

CREATE TABLE workshift (
[ws_id] [bigint] NOT NULL,
[start_date] [datetime] NOT NULL,
[end_date] [datetime] NOT NULL,
[worker_id] [bigint] NOT NULL
)

INSERT INTO workshift VALUES (1, '2012-08-20 08:30:00', '2012-08-20 14:30:00', 1)
INSERT INTO workshift VALUES (2, '2012-08-20 14:30:00', '2012-08-20 22:30:00', 2)


Table 2: Contains monetary denominations

CREATE TABLE currency_denom (
[cd_id] [decimal](7, 2) NOT NULL,
[name] [nchar](100) NOT NULL
)

INSERT INTO currency_denom VALUES (1, '100.00')
INSERT INTO currency_denom VALUES (2, '50.00')
INSERT INTO currency_denom VALUES (3, '20.00')
INSERT INTO currency_denom VALUES (4, '10.00')
INSERT INTO currency_denom VALUES (5, '5.00')
INSERT INTO currency_denom VALUES (6, '1.00')


Table 3: Contains the quantity of each denomination the worker has received in every workshift

CREATE TABLE currency_by_workshift (
[cd_id] [decimal](7, 2) NOT NULL,
[ws_id] [bigint] NOT NULL,
[qty] [int] NOT NULL
)

INSERT INTO currency_by_workshift VALUES (1, 1, 1)
INSERT INTO currency_by_workshift VALUES (2, 1, 2)
INSERT INTO currency_by_workshift VALUES (3, 1, 2)
INSERT INTO currency_by_workshift VALUES (2, 2, 3)
INSERT INTO currency_by_workshift VALUES (4, 2, 4)
INSERT INTO currency_by_workshift VALUES (5, 2, 2)


I need to get the currency_by_workshift values in columns instead of rows, along with the workshift values, that is:

workshift |     workshift       |     workshift       | 100.00 | 50.00 | 20.00 | 10.00 | 5.00 | 1.00 
  ws_id   |     start_date      |     end_date        |        |       |       |       |      | 

    1     | 2012-08-20 08:30:00 | 2012-08-20 14:30:00 |    1   |   2   |   2   |   0   |   0  |   0
    2     | 2012-08-20 14:30:00 | 2012-08-20 22:30:00 |    0   |   2   |   0   |   4   |   2  |   0


I'm not able to use a case to count quantities for each currency denomination because they are configurable, if a new denomination is added, the query should be modified. Same applies if using PIVOT function, or I'm wrong?

How can I get the info that way?

Thank you

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 08/22/2012 :  10:50:38  Show Profile  Reply with Quote
see

http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
643 Posts

Posted - 08/25/2012 :  21:21:22  Show Profile  Reply with Quote
quote:
I'm not able to use a case to count quantities for each currency denomination because they are configurable, if a new denomination is added, the query should be modified.


I'm curious... what demoninations do you think you might add?


--Jeff Moden
Go to Top of Page

Andy Hyslop
Starting Member

United Kingdom
13 Posts

Posted - 08/28/2012 :  06:02:23  Show Profile  Reply with Quote

DECLARE 
@SQL VARCHAR(MAX)
,@COLUMNS VARCHAR(MAX)

SET @COLUMNS =
STUFF(( SELECT ',' + QUOTENAME([name]) AS [text()] FROM ( SELECT DISTINCT [name] FROM currency_denom) AS Code
FOR XML PATH('')),1,1,'' )



SET @SQL = 
'SELECT *
FROM 
(	SELECT 
	W.[ws_id]
	, W.[start_date]
	, W.[end_date] 
	, CD.[Name]
	, CBW.[Qty]

	
	FROM workshift AS W 
	
	INNER JOIN currency_by_workshift AS CBW
	ON W.[ws_id] = CBW.[ws_id] 
	
	INNER JOIN currency_denom AS CD
	ON CBW.[cd_id] = CD.[cd_id]
	
	) AS A
PIVOT
(
SUM([Qty]) FOR [name] IN (' + @COLUMNS + ')
) AS PIV'

EXEC (@SQL)



Andy
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.09 seconds. Powered By: Snitz Forums 2000