Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

changing data from vertical to horizontal

 
   Database Forums (Home) -> Datamining RSS
Next:  Trying to predict sales from demographics  
Author Message
cb

External


Since: May 25, 2005
Posts: 3



(Msg. 1) Posted: Thu Dec 13, 2007 6:15 pm
Post subject: changing data from vertical to horizontal
Archived from groups: microsoft>public>sqlserver>datamining (more info?)

I have data in a vertical format, that I want to un-normalize into a
Horizontal (single-row-per-person) format to make it easier for users to
query on. (data/queries abbreviated for the sake of efficiency and not
boring the heckout of anyone reading):



The metadata table:

attribute_id description import_key select_mask
sort_order

-11 Not applying noessay -1 -14

-8 P essay received Precd -1 -8

-7 P essay q sent Psent -1 -7

-6 P Invite Pinvite -1 -6

-5 P Deny Pdeny -1 -5

-4 P Recipient Precipient -1 -4

-3 P Offer Poffer -1 -3

-2 P Waitlist Pwaitlist -1 -2

-1 P Hold Phold -1 -1



The applicant Data table

applicant_user_id attribute_id

182182 -11

182387 -11

182388 -8

182388 -7

182463 -10

182463 -8

182463 -7

182645 -12

182645 -8

182645 -7



Desired output



applicant_user_id Not applying P essay received P
essay q sent P Invite P Deny P Recipient P Offer P
Waitlist P Hold

182182 1 0 0 0 0 0
0 0 0

182387 1 0 0 0 0 0
0 0 0

182388 0 1 0 0 0 0
0 0 0

182388 0 0 1 0 0 0
0 0 0

182463 0 0 0 0 0 0
0 0 0

182463 0 1 0 0 0 0
0 0 0

182463 0 0 1 0 0 0
0 0 0

182645 0 0 0 0 0 0
0 0 0

182645 0 1 0 0 0 0
0 0 0

182645 0 0 1 0 0 0
0 0 0





I can do this using this query style (abbreviated for sake efficiency):

Select applicant_user_id,

'Not applying' =

CASE

WHEN [Attribute_id]='-11' THEN 1

ELSE 0

END,

'P essay received' =

CASE

WHEN [Attribute_id]='-8' THEN 1

ELSE 0

END,

'P essay q sent' =

...

FROM Applicant_Attributes

WHERE applicant_user_id in
('182182','182387','182388','182388','182463','182463','182463','182645','182645','182645')





But, ideally, I'd like an automated way to do this so I can tuck it into a
sp. I can do this in ColdFusion or ASP (select * from General_Attributes,
then loop over to build the query string). I'd like to do this so when the
values change in the table, I don't have to rewrite the query each time.



So. any ideas or resources? Is it even feasible in just T-SQL?



TIA,



Chris

 >> Stay informed about: changing data from vertical to horizontal 
Back to top
Login to vote
Dejan Sarka

External


Since: Mar 18, 2004
Posts: 317



(Msg. 2) Posted: Fri Dec 14, 2007 9:58 am
Post subject: Re: changing data from vertical to horizontal [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

if you are using SQL Server 2005, you can use the PIVOT operator. Check it
in Books OnLine, you have examples there as well.

--
Dejan Sarka
http://blogs.solidq.com/EN/dsarka/default.aspx

"cb" wrote in message

>
>
> I have data in a vertical format, that I want to un-normalize into a
> Horizontal (single-row-per-person) format to make it easier for users to
> query on. (data/queries abbreviated for the sake of efficiency and not
> boring the heckout of anyone reading):
>
>
>
> The metadata table:
>
> attribute_id description import_key select_mask
> sort_order
>
> -11 Not applying noessay -1 -14
>
> -8 P essay received Precd -1 -8
>
> -7 P essay q sent Psent -1 -7
>
> -6 P Invite Pinvite -1 -6
>
> -5 P Deny Pdeny -1 -5
>
> -4 P Recipient Precipient -1 -4
>
> -3 P Offer Poffer -1 -3
>
> -2 P Waitlist Pwaitlist -1 -2
>
> -1 P Hold Phold -1 -1
>
>
>
> The applicant Data table
>
> applicant_user_id attribute_id
>
> 182182 -11
>
> 182387 -11
>
> 182388 -8
>
> 182388 -7
>
> 182463 -10
>
> 182463 -8
>
> 182463 -7
>
> 182645 -12
>
> 182645 -8
>
> 182645 -7
>
>
>
> Desired output
>
>
>
> applicant_user_id Not applying P essay received P
> essay q sent P Invite P Deny P Recipient P Offer P
> Waitlist P Hold
>
> 182182 1 0 0 0 0 0
> 0 0 0
>
> 182387 1 0 0 0 0 0
> 0 0 0
>
> 182388 0 1 0 0 0 0
> 0 0 0
>
> 182388 0 0 1 0 0 0
> 0 0 0
>
> 182463 0 0 0 0 0 0
> 0 0 0
>
> 182463 0 1 0 0 0 0
> 0 0 0
>
> 182463 0 0 1 0 0 0
> 0 0 0
>
> 182645 0 0 0 0 0 0
> 0 0 0
>
> 182645 0 1 0 0 0 0
> 0 0 0
>
> 182645 0 0 1 0 0 0
> 0 0 0
>
>
>
>
>
> I can do this using this query style (abbreviated for sake efficiency):
>
> Select applicant_user_id,
>
> 'Not applying' =
>
> CASE
>
> WHEN [Attribute_id]='-11' THEN 1
>
> ELSE 0
>
> END,
>
> 'P essay received' =
>
> CASE
>
> WHEN [Attribute_id]='-8' THEN 1
>
> ELSE 0
>
> END,
>
> 'P essay q sent' =
>
> ..
>
> FROM Applicant_Attributes
>
> WHERE applicant_user_id in
> ('182182','182387','182388','182388','182463','182463','182463','182645','182645','182645')
>
>
>
>
>
> But, ideally, I'd like an automated way to do this so I can tuck it into a
> sp. I can do this in ColdFusion or ASP (select * from General_Attributes,
> then loop over to build the query string). I'd like to do this so when the
> values change in the table, I don't have to rewrite the query each time.
>
>
>
> So. any ideas or resources? Is it even feasible in just T-SQL?
>
>
>
> TIA,
>
>
>
> Chris
>
>

 >> Stay informed about: changing data from vertical to horizontal 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Changing training data table in mining structure - In DMX it's easy to train a mining model or structure on another data basis, using the INSERT INTO statement. But how can I do this in an Analysis Services Project in the BI-Studio? I didn't find a reasonable way to change the table which the mining..

Data - Hi! I'm actually looking for "usefull" test data, perhaps sth business related. Does somebody know if there is a site offering that?! Thanks a lot! Cheers alex

refreshing my data set - Hello I have created a desicion tree model via Visual Studio 2005. I started with 10 000 records and now want to refresh my model with 30 000 additional records. Though I have inserted these new recrds in my source table, the model keeps using only the..

warranty data - have you processed warranty information to track down product options that have a higher warranty cost or/and find emerging issues?

Data mining - I am trying to find more about data mining description. Something like definitions of it. I heard about things like predictive models, classifications, regressions, clasters, sumarisation, etc. but still I couldn't find real meaning in data minig of thos...
   Database Forums (Home) -> Datamining All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]