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 > select statement w/case statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-02-03, 12: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, 13:03
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: select statement w/case statement

What is the output that you want to see?
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 06-02-03, 13: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, 13:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
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://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 06-02-03, 13: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, 13: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, 13:31
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
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://tonyandrews.blogspot.com
Reply With Quote
  #8 (permalink)  
Old 06-02-03, 13: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, 13:48
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
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://tonyandrews.blogspot.com
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