MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 30th, 2004, 07:39 AM
MrHansen MrHansen is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 MrHansen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unique columns in sort by importance result

I try to search my data and sort the result by importance.

I'm using a MS Access database and my data (table1) looks like this:

Code:
ID   NAME            TEXT
1    Apples          Good red apples
2    Bananas         Fine yellow bananas
3    Yellow apples   Great yellow apples

I want to search the data and get a result where the column "NAME" is more important than "TEXT". My SQL looks like this:

Code:
SELECT id,name,text,1 AS searchorder FROM table1 WHERE name LIKE '*yellow*'
UNION
SELECT id,name,text,2 AS searchorder FROM table1 WHERE text LIKE '*yellow*'
ORDER BY searchorder 

The output is this:

Code:
ID   NAME            TEXT                  SEARCHORDER
3    Yellow apples   Great yellow apples   1
2    Bananas         Fine yellow bananas   2
3    Yellow apples   Great yellow apples   2

So far so good - the order by importance works - but I do not get unique columns because of the searchorder column.

Can I fix my SQL so I get unique columns where the last line of "Yellow apples" does not appear or am I lost in space?

Best regards,
Peter from Denmark

Reply With Quote
  #2  
Old December 30th, 2004, 08:29 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
Code:
SELECT id,[name],[text]
FROM table1 
WHERE [name] LIKE '*yellow*'
or [text] like '*yellow*'
order by iif([name] LIKE '*yellow*',1,2)

Reply With Quote
  #3  
Old December 30th, 2004, 09:54 AM
MrHansen MrHansen is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 MrHansen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Great! Thank you! It works fine!

In fact I have 4 different colums I want to sort by importance, so I have extended your SQL to this:

Code:
SELECT id,[name],[text],[desc],[art] FROM table1 
WHERE [name] LIKE '*yellow*' OR [text] LIKE '*yellow*' OR [desc] LIKE '*yellow*' OR [text] LIKE '*yellow*'
ORDER BY IIF([name] LIKE '*yellow*',1,IIF([text] LIKE '*yellow*',2,IIF([desc] LIKE '*yellow*',3,4)))

Peter from Denmark

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Unique columns in sort by importance result


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT