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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 different representation

Author  Topic 

nbs
Starting Member

22 Posts

Posted - 2007-04-20 : 14:49:13
Hi all,
I have this information but i need to put it in a different format as stated below.
For a specific nbr and year, when PIN and ON I would like to add up all the counts and put them in InsON; likewise for CMT--> CmtON/OFF
The third category MedProv consists of P1, MCP, HOS. So MedProvON/OFF for a specific nbr and its year should sum up all the counts respectively.

below is an example how things should add up.

nbr year role status count

90-04424-01; 2006; CMT; OFF; 20;
90-04424-01; 2006; CMT; ON; 47;
90-04424-01; 2006; HOS; OFF; 3;
90-04424-01; 2006; HOS; ON; 10;
90-04424-01; 2006; MCP; OFF; 1;
90-04424-01; 2006; P1; OFF; 1;
90-04424-01; 2006; P1; ON; 5;
90-04424-01; 2006; PIN; OFF; 8;
90-04424-01; 2006; PIN; ON; 58;
90-04424-04; 2005; CMT; ON; 1;
90-04424-04; 2005; PIN; ON; 1;
90-04424-04; 2006; CMT; ON; 8;
90-04424-04; 2006; PIN; ON; 9;
90-04424-08; 2006; CMT; ON; 1;
90-04424-08; 2006; PIN; ON; 1;

InsON InsOFF CmtON CmtOFF MedProvON MEdProvOFF
90-04424-01; 2006; 58; 8; 47; 20; 15; 5;
90-04424-04; 2005; 1; 0; 1; 0; 0; 0;
90-04424-04; 2006; 9; 0; 8; 0; 0; 0;
90-04424-08; 2006; 1; 0; 1; 0; 1; 0;

Can anyone help me on this one?

Thanks

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-04-20 : 16:25:58
Try this:
DECLARE @Table TABLE(nbr VARCHAR(12), [year] INT, [role] VARCHAR(3), status VARCHAR(3), [count] INT)

INSERT @Table
SELECT '90-04424-01', 2006, 'CMT', 'OFF', 20
UNION SELECT '90-04424-01', 2006, 'CMT', 'ON', 47
UNION SELECT '90-04424-01', 2006, 'HOS', 'OFF', 3
UNION SELECT '90-04424-01', 2006, 'HOS', 'ON', 10
UNION SELECT '90-04424-01', 2006, 'MCP', 'OFF', 1
UNION SELECT '90-04424-01', 2006, 'P1', 'OFF', 1
UNION SELECT '90-04424-01', 2006, 'P1', 'ON', 5
UNION SELECT '90-04424-01', 2006, 'PIN', 'OFF', 8
UNION SELECT '90-04424-01', 2006, 'PIN', 'ON', 58
UNION SELECT '90-04424-04', 2005, 'CMT', 'ON', 1
UNION SELECT '90-04424-04', 2005, 'PIN', 'ON', 1
UNION SELECT '90-04424-04', 2006, 'CMT', 'ON', 8
UNION SELECT '90-04424-04', 2006, 'PIN', 'ON', 9
UNION SELECT '90-04424-08', 2006, 'CMT', 'ON', 1
UNION SELECT '90-04424-08', 2006, 'PIN', 'ON', 1
;

SELECT
nbr,
[year],
SUM(
CASE
WHEN [role] = 'PIN' AND status = 'ON' THEN [count]
ELSE 0
END
) AS IsOn,
SUM(
CASE
WHEN [role] = 'PIN' AND status = 'OFF' THEN [count]
ELSE 0
END
) AS IsOff,
SUM(
CASE
WHEN [role] = 'CMT' AND status = 'ON' THEN [count]
ELSE 0
END
) AS CmtOn,
SUM(
CASE
WHEN [role] = 'CMT' AND status = 'OFF' THEN [count]
ELSE 0
END
) AS CmtOff,
SUM(
CASE
WHEN ([role] = 'P1' OR [role] = 'MCP' OR [role] = 'HOS') AND status = 'ON' THEN [count]
ELSE 0
END
) AS MedProvOn,
SUM(
CASE
WHEN ([role] = 'P1' OR [role] = 'MCP' OR [role] = 'HOS') AND status = 'OFF' THEN [count]
ELSE 0
END
) AS MedProvOff
FROM
@Table
GROUP BY
nbr,
[year]
ORDER BY
nbr,
[year]

EDIT: Forgot the year GROUP BY
EDIT2: Typo :)
Go to Top of Page

nbs
Starting Member

22 Posts

Posted - 2007-04-20 : 17:28:20
thanks a lot.. worked with a lil tweaking!
Go to Top of Page
   

- Advertisement -