Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ianbowyer
Starting Member
2 Posts |
Posted - 2005-02-27 : 10:40:01
|
| Hi I am doing a personal online gallery and have an SQL problem which should be so simple but is proving challenging. I may be looking at the problem from the wrong angle.The proble is as follows... I have two tables "Album" and "picture" (along with other ;o) )The create statements are as follows:CREATE TABLE [dbo].[Album] ( [id] [int] IDENTITY (1, 1) NOT NULL , [Name] [varchar] (50) COLLATE Latin1_General_CI_AS NULL , [Parent] [int] NULL , [sort] [int] NULL , [comments] [varchar] (50) COLLATE Latin1_General_CI_AS NULL , [timestamp] [datetime] NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[Picture] ( [id] [int] IDENTITY (1, 1) NOT NULL , [Title] [varchar] (100) COLLATE Latin1_General_CI_AS NULL , [Description] [varchar] (8000) COLLATE Latin1_General_CI_AS NULL , [FileName] [varchar] (50) COLLATE Latin1_General_CI_AS NULL , [Sort] [int] NOT NULL , [AlbumID] [int] NOT NULL , [CreatedBY] [int] NULL , [TimeStamp] [datetime] NOT NULL ) ON [PRIMARY]GOWhat I have done is to have a procedure which only shows albums which have pictures in using the following SQL Statement:SELECT distinct(A.ID), A.Name, A.Parent, A.Sort, A.CommentsFROM Album AJOIN Picture P ON P.AlbumID = A.IDorder by A.SortWhich will for example show<code>ID Name Parent Sort Comments1 Album1 NULL 10 Comment 12 Album2 NULL 20 Comment 23 Album3 NULL 30 Comment 3</code>What I want to do is to have a filename column which will be the top Picture from the related table. The top Picture for each album is the defined from picture->sort field (the lowest number being the top).I tried a group to no avail:SELECT A.ID, A.Name, A.Parent, A.Sort, A.Comments,P.filenameFROM Album A JOIN Picture P ON P.AlbumID = A.IDGROUP BY A.ID,P.filename, A.Name, A.Parent, A.Sort, A.CommentsWhich returned something like ID Name Parent Sort Comments FileName1 Album1 NULL 10 Comment 1 FileName1.JPG1 Album1 NULL 10 Comment 1 FileName2.JPG1 Album1 NULL 10 Comment 1 FileName3.JPG2 Album2 NULL 20 Comment 2 FileName4.JPG3 Album3 NULL 30 Comment 3 FileName5.JPGAm I missing something obvious... I know I can probably curse through origional SQL and do a select on picture to pick out the top 1 where album = cursor->albumID but really dont want to do that unless I have to.Any help is much appreciated |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-02-27 : 11:41:39
|
This code snippet should get you where you want to go. Let us know if you need anymore help.You need to do something like this:USE NorthwindGODROP TABLE ItemGOCREATE TABLE Item( PeriodID INT, SupplierID INT, ProductName VARCHAR(255), PricePerKg MONEY)GOINSERT Item(PeriodID, SupplierID, ProductName, PricePerKg) SELECT 1, 1, 'Alfafa', 9.2857 UNION ALL SELECT 1, 2, 'Alfafa', 8.9394 UNION ALL SELECT 1, 3, 'Alfafa', 6.6667 UNION ALL SELECT 1, 1, 'Apricots 40-45mm', 2.1000 UNION ALL SELECT 1, 2, 'Apricots 40-45mm', 3.0000 UNION ALL SELECT 1, 3, 'Apricots 40-45mm', 2.1500SELECT i1.SupplierID, i2.ProductName, i2.PricePerKgFROM Item i1 INNER JOIN ( SELECT ProductName, MIN(PricePerKg) AS PricePerKg FROM Item WHERE PeriodID = 1 AND PricePerKg > 0 GROUP BY ProductName) i2 ON i1.ProductName = i2.ProductName AND i1.PricePerKg = i2.PricePerKg MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
ianbowyer
Starting Member
2 Posts |
Posted - 2005-02-27 : 15:09:56
|
| Hi derrickleggett thanks for your help but still having problems :o(I like the idea of showing the min sort of the table grouped by AlbumIDI have adopted you query to fit my example and looks like belowSELECT A.ID, A.Name, A.Parent, A.Sort, A.Comments, P.FileName1FROM Album A INNER JOIN ( SELECT AlbumID, Min(sort) AS SortMin, filename as filename1FROM Picture GROUP BY AlbumID) P ON A.ID = P.AlbumIDThe problem is selected in bold. It returns:Column 'Picture.FileName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Can you shed any more light to the subject anyone |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-02-27 : 15:46:50
|
| You need to INNER JOIN to Picture again. Remember, you have to have all columns in the group by not aggregated, so in the derived table, just have AlbumID and SortMin. Inner join the derived table to Picture again, joining on AlbumID. That should give you the result you want.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|
|
|
|
|