Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > rollup data rather than numbers?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-04, 20:38
bilderbach bilderbach is offline
Registered User
 
Join Date: Aug 2004
Location: Riverside, CA
Posts: 21
rollup data rather than numbers?

I need to rollup a bunch of detail records to a single line report like a groupby sum() rollup using data rather than numbers and I would like to do it with sql rather than in my vb code.

Its basically a bunch of detail attributes of a product that I would like to generate a query returning one line per product based on a predefined set of attributes.


The data only has 3 columns:
codename = the product code
recordtype = the type of attribute
(for example price,weight,height,width,lastshipment)
data = the data value of the attribute


Sample data would be like this:
CODE1,PRICE,d1
CODE1,WEIGHT,d2
CODE1,HEIGHT,d3
CODE1,WIDTH,d4
CODE1,LASTSHIP,1/1/2004
CODE2,HEIGHT,d6
CODE3,PRICE,d7
CODE3,HEIGHT,d8
CODE3,WIDTH,d9

Output columns should look like this:
Code, Price, Weight, Height, Width

The output data from the sample above should look like this:CODE1,d1,d2,d3,d5
CODE2,null,null,d6,null
CODE3,d7,null,d8,d9

note in CODE1 I have a LASTSHIP type and I dont care about it, I just want the 4 types for this query.

I am using MSSQL 2000.


I am wide open to a better way to do this, I just need the ability to assign an unlimited number of attributes to a specific product and this was how I best figured out how to accomplish it giving the flexibility of any type of record.

Hope thats clearer...?

Thanks for your help!

Last edited by bilderbach : 10-13-04 at 11:40. Reason: unclear
Reply With Quote
  #2 (permalink)  
Old 10-13-04, 00:03
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,570
I'm not sure that I follow the rules for the rollup, or the exact format you want the results (I can't figure it out from your examples). It would also help a lot to know which database engine you are using, since this kind of rollup is much easier to do with engine specific tricks than doing it using pure SQL.

-PatP
Reply With Quote
  #3 (permalink)  
Old 10-13-04, 11:43
bilderbach bilderbach is offline
Registered User
 
Join Date: Aug 2004
Location: Riverside, CA
Posts: 21
see if that makes more sense.
Reply With Quote
  #4 (permalink)  
Old 10-13-04, 17:41
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Does SQL2000 support a case statement?
Reply With Quote
  #5 (permalink)  
Old 10-13-04, 18:15
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,570
I'd use:
Code:
SELECT a.CODENAME , Min(CASE WHEN 'PRICE' = a.recordtype THEN a.data END) AS Price , Min(CASE WHEN 'WEIGHT' = a.recordtype THEN a.data END) AS Weight , Min(CASE WHEN 'HEIGHT' = a.recordtype THEN a.data END) AS Height , Min(CASE WHEN 'WIDTH' = a.recordtype THEN a.data END) AS Width FROM myTable AS a GROUP BY a.CODENAME
-PatP
Reply With Quote
  #6 (permalink)  
Old 10-22-04, 21:56
bilderbach bilderbach is offline
Registered User
 
Join Date: Aug 2004
Location: Riverside, CA
Posts: 21
Thanks!

Thats juts what I was looking for!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On