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
 how to present the data comparison
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

249 Posts

Posted - 09/10/2013 :  11:50:08  Show Profile  Reply with Quote
Hi there,

A table with data like this:
Id | Type | val1 | val2 ...
12 | B3 | 21.9 | 32.5
12 | C2 | 12.4 | 21.3
...
13 | B3 | 19.2 | 11.4
13 | C2 | 32.5 |61.2

Now I need show the data this way:
| Type | val1 | val1 | val2 | val2 ...
| B3 | 21.9 | 19.2 | 32.5 | 11.4
| C2 | 12.4 | 32.5 | 21.3 | 61.2
The tricky part is the weekId 13 data must be arranged on the same line with weekId 12. Possible?
Thanks in advance.



James K
Flowing Fount of Yak Knowledge

3741 Posts

Posted - 09/10/2013 :  12:28:38  Show Profile  Reply with Quote
SELECT
	[Type],
	MAX(CASE WHEN Id = 12 THEN val1 END) AS val1,
	MAX(CASE WHEN id = 13 THEN val1 END) AS val1,
	MAX(CASE WHEN Id = 12 THEN val2 END) AS val2,
	MAX(CASE WHEN id = 13 THEN val2 END) AS val2
FROM
	Tbl
GROUP BY
	[Type]
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 09/10/2013 :  13:03:11  Show Profile  Reply with Quote
Beautiful!!! Thank you so much James!
But I don't understand what does MAX() here do.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 09/10/2013 :  13:08:18  Show Profile  Reply with Quote
Tried something and actually MAX() here does not need get maximum or minimum values but just make the case when statement work. So if you change it to MIN() you get same results. Is it right? Thanks.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3741 Posts

Posted - 09/10/2013 :  13:24:50  Show Profile  Reply with Quote
quote:
Originally posted by allan8964

Tried something and actually MAX() here does not need get maximum or minimum values but just make the case when statement work. So if you change it to MIN() you get same results. Is it right? Thanks.

Yes - the MAX or MIN (or any aggregate function that is applicable to the data type) can work. The assumption is that there will be only one value that satisfies the grouping condition for a given Id and Type, so you are not losing any data, but it allows you to group by the Type column.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 09/10/2013 :  15:29:03  Show Profile  Reply with Quote
Got it, thanks again!!!
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 09/16/2013 :  10:05:40  Show Profile  Reply with Quote
Another Way :
Select * from
(select Id,Type,val1,val2 from table1)K
PIVOT (MAX(TYPE) (ID IN ([val1],[val2],[val1],[val2]))P

P.V.P.MOhan
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.08 seconds. Powered By: Snitz Forums 2000