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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query that Excludes Related Rows.

Author  Topic 

davidshq
Posting Yak Master

119 Posts

Posted - 2006-01-13 : 17:35:44
Lets say one has a database of widgets with a popularity rating and type:
WidgetName - WidgetPopularity - WidgetType
RedWidget - 8 - Useful
BlueWidget - 7 - Useful
MyWidget - 8 - Useless
YourWidget - 8 - Useless
AnotherWidget - 7 - Useless
WickedWidget - 7 - Helpful
Now, lets say you want to grab a list of the top one Useful widgets, top two Useless widgets, and top one Helpful widget, the result set would be:
Red Widget - 8 - Useful
MyWidget - 8 - Useless
YourWidget - 8 - Useless
WickedWidget - 7 - Helpful
Now, lets take it one step further. There is one additional column that notes a relationship between widgets. In this instance MyWidget and YourWidget are related. Now, people don't want to see two very similar widgets, they want to see the top two best widgets that aren't similar. So the result set for Useless widgets should instead look like:
MyWidget - 8 - Useless
AnitherWidget - 7 - Useless
Because YourWidget is filtered out on the basis of having a relationship with MyWidget.
How can I accomplish this in SQL? I've already accomplished the first step, but now I am planning on adding a relationship column but am unsure how to put it into practice.
David.

- http://www.gamesecretary.com/
- http://www.thehungersite.com/
- http://www.grid.org/

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-13 : 21:24:17
can you post the table structure for widgets and the relationship table (ie YourWidget is related to MyWidget), sample data and expected result ?

-----------------
'KH'

if you can't beat them, have someone else to beat them
Go to Top of Page

davidshq
Posting Yak Master

119 Posts

Posted - 2006-01-14 : 09:30:24
WidgetTable:
ID - WidgetName - WidgetPopularity - WidgetType
1 - RedWidget - 8 - Useful
2 - BlueWidget - 7 - Useful
3 - MyWidget - 8 - Useless
4 - YourWidget - 8 - Useless
5 - AnotherWidget - 7 - Useless
6 - WickedWidget - 7 - Helpful
WidgetRelationshipTable:
WidgetID - RelatedID
3 - 4
If a person wanted the top two Useless results the hoped for result would be:
3 - MyWidget - 8 - Useless
5 - AnotherWidget - 7 - Useless
B/c YourWidget with ID 4 would be filtered out b/c it has a relationship with Widget 3.
David.

- http://www.gamesecretary.com/
- http://www.thehungersite.com/
- http://www.grid.org/
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-14 : 11:11:33
Try this . . .
create table  #WidgetTable
(
ID int,
WidgetName varchar(50),
WidgetPopularity int,
WidgetType varchar(10)
)

create table #temp
(
ID int,
WidgetName varchar(50),
WidgetPopularity int,
WidgetType varchar(10)
)

insert into #WidgetTable
select 1 , 'RedWidget' , 8 , 'Useful' union all
select 2 , 'BlueWidget' , 7 , 'Useful' union all
select 3 , 'MyWidget' , 8 , 'Useless' union all
select 4 , 'YourWidget' , 8 ,'Useless' union all
select 5 , 'AnotherWidget' , 7 , 'Useless' union all
select 6 , 'WickedWidget' , 7 , 'Helpful'

create table #WidgetRelationshipTable
(
WidgetID int,
RelatedID int
)

insert into #WidgetRelationshipTable
select 3 , 4


delete #temp
insert into #temp
select top 1 * from #WidgetTable where WidgetType = 'Useful' order by WidgetPopularity desc, ID

insert into #temp
select top 2 w.* from #WidgetTable w left join #WidgetRelationshipTable r
on w.ID = r.RelatedID
where WidgetType = 'Useless'
and r.WidgetID is null
order by WidgetPopularity desc, ID

insert into #temp
select top 1 * from #WidgetTable where WidgetType = 'Helpful' order by WidgetPopularity desc, ID

select * from #temp


-----------------
'KH'

if you can't beat them, have someone else to beat them
Go to Top of Page

davidshq
Posting Yak Master

119 Posts

Posted - 2006-01-14 : 11:41:50
Thanks. Your code would get me good results, but there is one more refinement that I need. Lets say the #WidgetRelationshipTable looked like this:
insert into #WidgetRelationshipTable
select 3 , 4 union all
select 4 , 3
In this case when your insert into #temp command ran, I wouldn't get back two results, but only one. B/c now there are relationships going both ways between MyWidget and YourWidget. How do I get it to select one related widget, but then know not to pick another.
e.g., If MyWidget is in the result set then YourWidget should not be, and vice versa. But one of them should show up, not none of them.
Thanks.
David.


- http://www.gamesecretary.com/
- http://www.thehungersite.com/
- http://www.grid.org/
Go to Top of Page
   

- Advertisement -