SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to populate new column in the result set


Need to populate new column in the result set

Author
Message
Mac1986
Mac1986
Mr or Mrs. 500
Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)

Group: General Forum Members
Points: 571 Visits: 791
Hi I have a table which has 3 unique values as below table.

SNo. Date Cat Value
1 11/01 A 100
2 11/02 A 101
3 11/01 B 102
4 11/02 B 100
5 11/01 C 1088
5 11/02 C 1026

I would want to see the result set as

SNo. Date Cat_A Value_A Cat_B Value_B Cat_C Value_C
1 11/01 A 100 B 102 C 1088
2 11/02 A 101 B 100 C 1026


I'm getting confused how to get it ... Please help
imex
imex
SSC-Enthusiastic
SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)

Group: General Forum Members
Points: 103 Visits: 199
Hi,

Try:

select
a.SNo,
a.Date,
a.Cat as Cat_A,
a.Value as Value_A,
b.Cat as Cat_B,
b.Value as Value_B,
c.Cat as Cat_C,
c.Value as Value_C
from MyTable as a
left join MyTable as b
on b.Date = a.Date and b.Cat = 'B'
left join MyTable as c
on c.Date = c.Date and c.Cat = 'C'
where a.Cat = 'A'



Hope this helps.

http://www.imoveisemexposicao.com.br
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7531 Visits: 6431
imex (11/4/2012)
Hi,

Try:

select
a.SNo,
a.Date,
a.Cat as Cat_A,
a.Value as Value_A,
b.Cat as Cat_B,
b.Value as Value_B,
c.Cat as Cat_C,
c.Value as Value_C
from MyTable as a
left join MyTable as b
on b.Date = a.Date and b.Cat = 'B'
left join MyTable as c
on c.Date = c.Date and c.Cat = 'C'
where a.Cat = 'A'



Hope this helps.


A double self-join? w00t

How about a single table scan instead:


DECLARE @T TABLE
(SNo INT, Date DATETIME, CAT CHAR(1), Value INT)

INSERT INTO @T (SNo, Date, Cat, Value)
SELECT 1, '2011-11-01','A',100
UNION ALL SELECT 2, '2011-11-02','A',101
UNION ALL SELECT 3, '2011-11-01','B',102
UNION ALL SELECT 4, '2011-11-02','B',100
UNION ALL SELECT 5, '2011-11-01','C',1088
UNION ALL SELECT 5, '2011-11-02','C',1026

SELECT SNo=ROW_NUMBER() OVER (ORDER BY Date)
,Date, Cat_A, Value_A, Cat_B, Value_B, Cat_C, Value_C
FROM (
SELECT Date
,Cat_A='A'
,Value_A=MAX(CASE Cat WHEN 'A' THEN Value END)
,Cat_B='B'
,Value_B=MAX(CASE Cat WHEN 'B' THEN Value END)
,Cat_C='C'
,Value_C=MAX(CASE Cat WHEN 'C' THEN Value END)
FROM @T
GROUP BY Date) a





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89018 Visits: 41138
Dwain,

Add 10 to each SNo and see if it still works.

You're on the right track though. Just a small change will do it.

 SELECT SNo = MIN(SNo)
, Date
, Cat_A = 'A'
, Value_A = MAX(CASE Cat WHEN 'A' THEN Value END)
, Cat_B = 'B'
, Value_B = MAX(CASE Cat WHEN 'B' THEN Value END)
, Cat_C = 'C'
, Value_C = MAX(CASE Cat WHEN 'C' THEN Value END)
FROM @T
GROUP BY Date




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7531 Visits: 6431
Jeff Moden (11/4/2012)
Dwain,

Add 10 to each SNo and see if it still works.

You're on the right track though. Just a small change will do it.

 SELECT SNo = MIN(SNo)
, Date
, Cat_A = 'A'
, Value_A = MAX(CASE Cat WHEN 'A' THEN Value END)
, Cat_B = 'B'
, Value_B = MAX(CASE Cat WHEN 'B' THEN Value END)
, Cat_C = 'C'
, Value_C = MAX(CASE Cat WHEN 'C' THEN Value END)
FROM @T
GROUP BY Date




Jeff,

I see what you did, but if I "add 10 to each SNo" in the sample data like this:


INSERT INTO @T (SNo, Date, Cat, Value)
SELECT 11, '2011-11-01','A',100
UNION ALL SELECT 12, '2011-11-02','A',101
UNION ALL SELECT 13, '2011-11-01','B',102
UNION ALL SELECT 14, '2011-11-02','B',100
UNION ALL SELECT 15, '2011-11-01','C',1088
UNION ALL SELECT 15, '2011-11-02','C',1026




Mine still gets the same results. Not intentionally being obtuse here. :-D


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
vinu512
vinu512
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1713 Visits: 1625
That was a really nice catch Mr. Moden..........I came up with the exact same query as Dwain......never thought of the scenario that you mentioned. I guess this foresight comes with experience.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search