Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Pivot Select?

I need to build a select based on two columns

team     disqReason
1           Bad
1           Good
1           No Money
2           Good

With the results

Team      Bad        Good     No Money
1              1               1            1
2              0               1            0
0
lrbrister
Asked:
lrbrister
1 Solution
 
ThomasianCommented:
DECLARE @t table (team int, disqReason varchar(100))

INSERT @t 
SELECT 1, 'Bad'
UNION ALL SELECT 1, 'Good'
UNION ALL SELECT 1, 'No Money'
UNION ALL SELECT 2, 'Good'


SELECT *
FROM @t T
pivot (COUNT(disqReason) FOR disqReason IN ([Bad], [Good], [No Money])) pvt

Open in new window

0
 
tim_csCommented:
Try this.
SELECT
	Team, [Bad], [Good], [No Money]
FROM
	(SELECT
		Team, disqReason
	FROM
		YourTable) p
	PIVOT (
	COUNT(team) FOR disqReason IN ([Bad], [Good], [No Money])
	) pvt

Open in new window

0
 
lrbristerAuthor Commented:
Perfect and what I asked for
Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now