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 |
mutlyp
Starting Member
20 Posts |
Posted - 2012-05-03 : 12:58:37
|
I have a table that holds ItemIds and LocationIDs. It's a many to many table because multiple locations can use the same Item. But there are also "Default" items that are offered to the user. So they can choose a "Deault" item or add a custom item o thier loaction. So I marked all the "Default" items in the table with a Zero for the LocationID. Beacuse there is no location with a Zero for the ID.If a user decides to use one of the "Defualt" items the ItemID for that item is added to the table with their LocationID for that particular Location. So the table would look like this:ItemID LocationID1 02 03 04 05 02 213 21So this shows that Location 21 has chosen item 2 and 3 to be used at thier location (21).But now I need a query that will return all the LocationID of 21 and all the "Default" items that have not been chosen by location 21.So the results should look like this:ItemID LocaionID1 02 213 214 05 0Hope this makes since. Please help it's driving me crazy.Thank you |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-03 : 13:21:21
|
Assuming you have no locations with a negative number for the location id, you can do the following:SELECT ItemId, MAX(LocationId)FROM YourTableGROUP BY ItemIdORDER BY ItemId; |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-03 : 14:54:25
|
quote: Originally posted by sunitabeck Assuming you have no locations with a negative number for the location id, you can do the following:SELECT ItemId, MAX(LocationId)FROM YourTableWHERE LocationId=@LocationIDGROUP BY ItemIdORDER BY ItemId;
If you've records for locationids other than 21 also presentYou should pass values for @LocationID based on your requirement------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
mutlyp
Starting Member
20 Posts |
Posted - 2012-05-03 : 15:20:32
|
Thank you for the response. Unfortuately this is not working. If I don't add a where clause I get back all the results. But as soon as I add the where clause for the locationid it only brings back the ones for that particular location id. It does not bring back any of the defaults that were not used by the location Id.Please helpThank youquote: Originally posted by visakh16
quote: Originally posted by sunitabeck Assuming you have no locations with a negative number for the location id, you can do the following:SELECT ItemId, MAX(LocationId)FROM YourTableWHERE LocationId=@LocationIDGROUP BY ItemIdORDER BY ItemId;
If you've records for locationids other than 21 also presentYou should pass values for @LocationID based on your requirement------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-03 : 15:33:35
|
ah sorry . it should be thisSELECT ItemID,MAX(LocationID)FROM table WHERE t.LocationID IN (0,@LocationID)GROUP BY ItemID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
mutlyp
Starting Member
20 Posts |
Posted - 2012-05-03 : 17:12:05
|
Thank you that worked. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-03 : 17:12:58
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|