MotoCrazy
asked on
Select only one matching record from each category
Been working with SQL for about a week now, and n00b needs help! =P
I have a table with tons of images, and I am trying to create an ASPX (VB.Net) page that will display only the first image from each category. From here, the user can click on that image to be taken to a "category.aspx" page with all images in that category. Ie...
dbo.IMGS Table Sample:
ImgName ImgCategory
IMG1.jpg Cat1
IMG2.jpg Cat1
IMG3.jpg Cat1
IMG1.jpg Cat2
IMG2.jpg Cat2
How would I retrieve *ONLY* the two "IMG1.jpg from Cat1" and "IMG1.jpg from Cat2" from SQL?
SELECT TOP 1 *
FROM dbo.IMGS
WHERE ImgCategory *is different?*
Appreciate any help.
Eric
I have a table with tons of images, and I am trying to create an ASPX (VB.Net) page that will display only the first image from each category. From here, the user can click on that image to be taken to a "category.aspx" page with all images in that category. Ie...
dbo.IMGS Table Sample:
ImgName ImgCategory
IMG1.jpg Cat1
IMG2.jpg Cat1
IMG3.jpg Cat1
IMG1.jpg Cat2
IMG2.jpg Cat2
How would I retrieve *ONLY* the two "IMG1.jpg from Cat1" and "IMG1.jpg from Cat2" from SQL?
SELECT TOP 1 *
FROM dbo.IMGS
WHERE ImgCategory *is different?*
Appreciate any help.
Eric
Are you using SQL Server 2005 or 2008?
look at this
use adventureworks
go
create table img
(
ImgName varchar(20),
ImgCategory varchar(10)
)
insert into img
SELECT 'IMG1.jpg', 'Cat1' UNION ALL
SELECT 'IMG2.jpg', 'Cat1' UNION ALL
SELECT 'IMG3.jpg', 'Cat1' UNION ALL
SELECT 'IMG1.jpg', 'Cat2' UNION ALL
SELECT 'IMG2.jpg', 'Cat2'
SELECT max(imgname),imgcategory from img group by imgcategory
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or you can use this one as well for first image
SELECT min(imgname),imgcategory from img group by imgcategory
SELECT min(imgname),imgcategory from img group by imgcategory
Unless you have and index or something to rank the images in each category, the 'first' image is a bit of an ambiguous description.
That said, here's some SQL that will do what you want to do. You can change the 'min' to 'max' to change which image is selected from each category. If you select the image name carefully, you should be able to control which is selected
select min(ImgName), ImgCategory from dbo.IMGS
group by ImgCategory
That said, here's some SQL that will do what you want to do. You can change the 'min' to 'max' to change which image is selected from each category. If you select the image name carefully, you should be able to control which is selected
select min(ImgName), ImgCategory from dbo.IMGS
group by ImgCategory
You can try the MIN or MAX as suggested by other experts here. Or you can also try like this if you are interested in TOP 1 value irrespective of whether it its MIN or MAX value for that Category.
select * from IMGS t1
where ImgName in (select top 1 ImgName from IMGS t2 where t1.ImgCategory = t2.ImgCategory)
ASKER
Thank you all for the quick replies!
acperkins, it is 2005 and you nailed it perfectly!
SELECT ImgCategory, MIN(ImgName) ImgName
FROM dbo.pictures
GROUP BY Category
My DataSet is now exactly how I wanted it. Thank you!
Eric
acperkins, it is 2005 and you nailed it perfectly!
SELECT ImgCategory, MIN(ImgName) ImgName
FROM dbo.pictures
GROUP BY Category
My DataSet is now exactly how I wanted it. Thank you!
Eric