If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > select statement w/case statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-02-03, 11:32
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
select statement w/case statement

hello,

i have a view:

SELECT
products.product_id,
productImageTypes.ImageType,
productImages.Image
FROM
products INNER JOIN
productImages ON products.product_id = productImages.product_id INNER JOIN
productBrands ON products.productBrand_id = productBrands.productBrand_id INNER JOIN
productImageTypes ON productImages.productImageType_id = productImageTypes.productImageType_id
WHERE (productBrands.productBrand_value = 'ThisBrand')

which returns:
product_id ImageType Image
113042 Large 26227.jpg
113042 Small 26228.jpg

i now need to make this into one record. i am trying to use a case statement like:

SELECT
products.product_id,
CASE
WHEN productImageTypes.ImageType = 'Large'
THEN productImages.Image
ELSE
NULL
END as largeImage
CASE
WHEN productImageTypes.ImageType = 'Small'
THEN productImages.Image
ELSE
NULL
END as smallImage
FROM
products INNER JOIN
productImages ON products.product_id = productImages.product_id INNER JOIN
productBrands ON products.productBrand_id = productBrands.productBrand_id INNER JOIN
productImageTypes ON productImages.productImageType_id = productImageTypes.productImageType_id
WHERE (productBrands.productBrand_value = 'ThisBrand')


but I can only get this type of output:
product_id largeImage smallImage
113042 26227.jpg NULL
113042 NULL 26228.jpg

how can i change my query so i get what i need? thanks for any help.
Reply With Quote
  #2 (permalink)  
Old 06-02-03, 12:03
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: select statement w/case statement

What is the output that you want to see?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 06-02-03, 12:05
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
product_id largeImage smallImage
113042 26227.jpg 26228.jpg
Reply With Quote
  #4 (permalink)  
Old 06-02-03, 12:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally posted by kfenstad
product_id largeImage smallImage
113042 26227.jpg 26228.jpg
Try this:

SELECT
products.product_id,
MAX(CASE WHEN productImageTypes.ImageType = 'Large' THEN productImages.Image END) largeImage,
MAX(CASE WHEN productImageTypes.ImageType = 'Small' THEN productImages.Image END) smallImage,
FROM
products INNER JOIN
productImages ON products.product_id = productImages.product_id INNER JOIN
productBrands ON products.productBrand_id = productBrands.productBrand_id INNER JOIN
productImageTypes ON productImages.productImageType_id = productImageTypes.productImageType_id
WHERE (productBrands.productBrand_value = 'ThisBrand')
GROUP BY products.product_id
ORDER BY products.product_id;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 06-02-03, 12:19
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
Smile

thank you, it works great.
Reply With Quote
  #6 (permalink)  
Old 06-02-03, 12:27
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
another question.

maybe you could help me with something else too. in the productImages table, there is a field called primary. there are multiple images for each product_id in the table, but each product_id should only have one primary image. right now all of the images are set to 0 (nonprimary) but i need to set one image for each product_id to 1 (primary). it doesn't matter which, just the top 1 image for that product_id. is this possible with an update statement, and if so, do you have any ideas for me?

thanks again for all your help.
Reply With Quote
  #7 (permalink)  
Old 06-02-03, 12:31
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: another question.

Quote:
Originally posted by kfenstad
maybe you could help me with something else too. in the productImages table, there is a field called primary. there are multiple images for each product_id in the table, but each product_id should only have one primary image. right now all of the images are set to 0 (nonprimary) but i need to set one image for each product_id to 1 (primary). it doesn't matter which, just the top 1 image for that product_id. is this possible with an update statement, and if so, do you have any ideas for me?

thanks again for all your help.
If it doesn't matter which one:

UPDATE productImages
SET primary=1
WHERE product_id, image IN
( SELECT product_id, MAX(image)
FROM productImages
GROUP BY product_id
);

That will pick the image with the "max" image name.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #8 (permalink)  
Old 06-02-03, 12:45
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
Re: another question.

Quote:
Originally posted by andrewst
If it doesn't matter which one:

UPDATE productImages
SET primary=1
WHERE product_id, image IN
( SELECT product_id, MAX(image)
FROM productImages
GROUP BY product_id
);

That will pick the image with the "max" image name.
i am getting an error, Incorrect syntax near ','.

what am i doing wrong?

thanks.
Reply With Quote
  #9 (permalink)  
Old 06-02-03, 12:48
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: another question.

Quote:
Originally posted by kfenstad
i am getting an error, Incorrect syntax near ','.

what am i doing wrong?

thanks.
Sorry, that should have read:

UPDATE productImages
SET primary=1
WHERE (product_id, image) IN
( SELECT product_id, MAX(image)
FROM productImages
GROUP BY product_id
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
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

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