Re: CrossTabulate names
From: Lance Wynn (lance_wynn_at_N.O.S.P.A.M.hotmail.com)
Date: 08/23/04
- Previous message: Adam Machanic: "Re: CrossTabulate names"
- In reply to: Adam Machanic: "Re: CrossTabulate names"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 22 Aug 2004 18:44:29 -0600
Excellent! I learn something new every day on these NG's!
Thanks
Lance
"Adam Machanic" <amachanic@hotmail._removetoemail_.com> wrote in message
news:eEue7YJiEHA.4092@TK2MSFTNGP10.phx.gbl...
Lance,
A very nice and compact way to do it is with the following pattern:
SELECT PKey, --Primary key
MAX(CASE YourDecisionColumn
WHEN 'A' THEN YourValueColumn
ELSE NULL
END) AS A,
MAX(CASE YourDecisionColumn
WHEN 'B' THEN YourValueColumn
ELSE NULL
END) AS B,
...
FROM YourTable
GROUP BY PKey
This would tabulate the following table:
PKey YourDecisionColumn YourValueColumn
---------------------------------------------------------
1 A AValCol1
1 B BValCol1
2 A AValCol1
2 B BValCol1
into:
PKey A B
----------------------------------
1 AValCol1 BValCol1
2 AValCol2 BValCol2
Depending on your indexes, this method can perform quite a bit better than
other methods that use temp tables or correlated subqueries.
"Lance Wynn" <lance_wynn@N.O.S.P.A.M.hotmail.com> wrote in message
news:OLzdfGBiEHA.1972@TK2MSFTNGP09.phx.gbl...
> Hello, I've done something like this before, you can create a temp table
for
> your output (If you know how many possible name_type values there are),
> and then do an update using a:
>
> Create #tmpOut with all required fields (Each possible name_type field)
- Previous message: Adam Machanic: "Re: CrossTabulate names"
- In reply to: Adam Machanic: "Re: CrossTabulate names"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|