| Author |
Topic |
|
spnz
Starting Member
12 Posts |
Posted - 2007-03-30 : 22:31:05
|
| Hi there,I am having problems with a SQL statement and can't seem to get it to work the way I wish.I have 2 tables tblItemDetails & tblItemTypes.A person can have many items and each of those items can be different ItemType e.g. a person may own a tv and dvd that fall into the appliances type, they also may have a fridge and a frezzer that fall into the kitchen wear type.My current SQL statement returns all the information where I would like for the statement to return only 1 record for appliances and 1 record for kitchen wear.This is my statement nowSELECT tblItemDetails.ItemTypeID, tblItemDetails.UserID, tblItemDetails.ItemID, tblItemTypes.ItemTypeFROM tblItemTypes INNER JOIN tblItemDetails ON tblItemTypes.ItemTypeID = tblItemDetails.ItemTypeIDCan anyone help out? |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-30 : 22:57:15
|
| SELECT top 1 tblItemDetails.ItemTypeID, tblItemDetails.UserID, tblItemDetails.ItemID, tblItemTypes.ItemTypeFROM tblItemTypes INNER JOINtblItemDetails ON tblItemTypes.ItemTypeID = tblItemDetails.ItemTypeIDTCC |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-30 : 22:59:06
|
| select * from @stageSELECT tblItemDetails.ItemTypeID, tblItemDetails.UserID, tblItemDetails.ItemID, it.ItemTypeFROM (select top 1 * tblItemTypes) it INNER JOINtblItemDetails ON it.ItemTypeID = tblItemDetails.ItemTypeIDTCC |
 |
|
|
spnz
Starting Member
12 Posts |
Posted - 2007-03-30 : 23:18:59
|
| Hi Tishri,I have tried both your examples but they didn't work :(The 1st example runs ok but only returns 1 result where I know there should be 2.The 2nd example fails giving me an error Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'tblItemTypes'.What should I change? |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-30 : 23:23:47
|
| SELECT tblItemDetails.ItemTypeID, tblItemDetails.UserID, tblItemDetails.ItemID, it.ItemTypeFROM (select top 1 * from tblItemTypes) it INNER JOINtblItemDetails ON it.ItemTypeID = tblItemDetails.ItemTypeIDTCC |
 |
|
|
spnz
Starting Member
12 Posts |
Posted - 2007-03-30 : 23:30:26
|
That one runs ok but returns 1 d196a58d-4733-491c-b860-8aabe0711d2f Kitchenware1 d196a58d-4733-491c-b860-8aabe0711d2f KitchenwareWhere it should return1 d196a58d-4733-491c-b860-8aabe0711d2f Kitchenware2 d196a58d-4733-491c-b860-8aabe0711d2f ApplicancesThanks for your help |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-30 : 23:35:11
|
| You mean you want to select all ITEMTYPES and return only 1 record for each item type?TCC |
 |
|
|
spnz
Starting Member
12 Posts |
Posted - 2007-03-30 : 23:41:49
|
| yes sorry if I didnt explain myself very clearly |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-30 : 23:44:43
|
| is this what you mean?you can change the data of the temporary table for make things clear.declare @itemdetails table (itemtypeid int, userid varchar(200), itemid varchar(100))declare @itemtypes table (itemtypeid int, itemtype varchar(12))insert into @itemdetails select 1, 'd196a58d-4733-491c-b860-8aabe07', '11d2f' union all select 2, 'd196a58d-4733-491c-b860-8aabe07', '11d2f'insert into @itemtypes select 1, 'appliances' union all select 2 , 'kitchenware'select * from @itemdetails d inner join @itemtypes t on d.itemtypeid = t.itemtypeidTCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-30 : 23:54:14
|
spnz,you better post your table structure with sample data and the result that you want. KH |
 |
|
|
spnz
Starting Member
12 Posts |
Posted - 2007-03-31 : 00:24:55
|
| Ok the 2 tablestblItemTypesItemTypeID ItemType1, abc2, def3, xyztblItemDetailsItemDetailsID ItemTypeID UserID1, 1, 12, 1, 13, 2, 1I would like the results to look likeUserID ItemTypeID ItemType1, 1 , abc1 , 2 , defHope that makes sense |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-31 : 01:21:57
|
| --run the statement belowdeclare @tblItemTypes table (ItemTypeID int, ItemType varchar(10))declare @tblItemDetails table(ItemDetailsID int , ItemTypeID int, UserID int)insert into @tblItemTypes select 1, 'abc' union all select 2, 'def' union all select 3, 'xyz' insert into @tblItemDetails select 1,1,1 union all select 2,1,1 union all select 3,2,1select distinct userid, itemtypeid , (select itemtype from @tblItemTypes where itemtypeid = d.itemtypeid) as itemtype from @tblItemdetails dTCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-31 : 02:26:53
|
"I would like for the statement to return only 1 record for appliances and 1 record for kitchen wear"Which one would you like to return ?Here i just return the smallest value of UserID.select UserID = min(d.UserID), t.ItemTypeID , t.ItemTypefrom tblItemDetails d inner join tblItemTypes t on d.ItemTypeID = t.ItemTypeIDgroup by t.ItemTypeID , t.ItemType KH |
 |
|
|
spnz
Starting Member
12 Posts |
Posted - 2007-03-31 : 03:09:47
|
| How would I write this as a stored procedure as I want to be able to select this information based on a users ID.I would have added @UserID intAS....... but UserID has already been added into the select |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-31 : 03:11:24
|
[code]select UserID = @UserID, t.ItemTypeID , t.ItemTypefrom tblItemDetails d inner join tblItemTypes t on d.ItemTypeID = t.ItemTypeIDwhere d.UserID = @UserIDgroup by t.ItemTypeID , t.ItemType[/code] KH |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-03-31 : 22:36:17
|
| select distinct userid, itemtypeid , (select itemtype from tblItemTypes where itemtypeid = d.itemtypeid) as itemtype from tblItemdetails dwhere userid = @useridTCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-01 : 00:34:40
|
quote: Originally posted by tishri select distinct userid, itemtypeid , (select itemtype from tblItemTypes where itemtypeid = d.itemtypeid) as itemtype from tblItemdetails dwhere userid = @useridTCC
Sub-query is not as efficient as INNER JOIN. If the itemtypeid is not the primary key in tblItemTypes table, the sub-query will failed as it will return more than 1 records. KH |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2007-04-01 : 21:16:38
|
| ahh,okey thanks for the informationTCC |
 |
|
|
|