question

somdeepgniyogi avatar image
somdeepgniyogi asked

SQL Query Help

![alt text][1] [1]: /storage/temp/2619-capture.png
sql-server-2008homework
capture.png (26.2 KiB)
2 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Show us the query that you have tried. And please don't post it as a screenshot - you are more likely to get help if you post actual code that we can copy and paste.
0 Likes 0 ·
I tried the below code SELECT [company],[Divisin],[Key],[Cluster],[Type],[Value] as [Value Y] into #extendedTableY from #ExtendedTempTable Where [Type]='Y' SELECT [company],[Divisin],[Key],[Cluster],[Type],[Value] as [Value N] into #extendedTableN from #ExtendedTempTable Where [Type]='N' select A.[company], A.[Divisin], A.[Key], A.[Value Y], B.[Value N] from #extendedTableY A Left Outer Join #extendedTableN B on A.[company]=B.[company] and A.[Divisin]=B.[Divisin] and A.[Key]=B.[Key] Order By A.[Key]
0 Likes 0 ·

1 Answer

· Write an Answer
Sule avatar image
Sule answered
CREATE TABLE #TABLE ( [Company] int, [Division] int, [Key] int, [Cluster] int, [Type] char(1), [Value] bigint ) INSERT INTO #TABLE SELECT 741,998,93510,180,'Y',-192659111 UNION ALL SELECT 741,999,93510,180,'N',648623657 UNION ALL SELECT 741,999,93510,180,'Y',224602713 UNION ALL SELECT 741,1,93515,110,'N',-7031789871 UNION ALL SELECT 741,1,93515,110,'Y',-33054093 UNION ALL SELECT 741,202,93515,120,'Y',44287176 UNION ALL SELECT 741,500,93515,120,'Y',-131376958 SELECT [Company], [Division], [Key], [Cluster], SUM(CASE WHEN [Type] = 'Y' THEN [Value] ELSE 0 END) AS [Value Y], SUM(CASE WHEN [Type] = 'N' THEN [Value] ELSE 0 END) AS [Value N] FROM #TABLE GROUP BY [Company], [Division], [Key], [Cluster]
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks a lot...
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.