Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

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 ASP Free Forums Sponsor:
  #1  
Old July 26th, 2004, 02:25 AM
mae123 mae123 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 mae123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
select Top 3

SELECT Country, CatDescription
FROM VIEW1
WHERE (Country IN ('hong kong', 'LA'))
GROUP BY Country, CatDescription
ORDER BY Country

Country Category
---------------------------------------
Hong Kong Point of Purchase
Hong Kong Literature
Hong Kong Advertisement
Hong Kong Gifts Premium
LA Gifts Premium
LA Advertisement
LA Seminar Kit
LA Literature
LA Point of Purchase

How to select Top 3 from the View 1 to get the result below?

Country Category
---------------------------------------
Hong Kong Point of Purchase
Hong Kong Literature
Hong Kong Advertisement
Hong Kong Gifts Premium
LA Gifts Premium
LA Advertisement
LA Seminar Kit

thanks.


Reply With Quote
  #2  
Old July 26th, 2004, 10:15 PM
Tonny-Soeroso Tonny-Soeroso is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 36 Tonny-Soeroso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 56 m 27 sec
Reputation Power: 0
May be you must create stored procedure to get result. I Think don't use view, in my experience in ERP Development, if we use view, query may be in the lock position in the server because we must load all data first before filtering.



Regards



Tonny

Reply With Quote
  #3  
Old July 27th, 2004, 02:35 AM
Kris_Vanherck's Avatar
Kris_Vanherck Kris_Vanherck is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Belgium, Antwerp
Posts: 177 Kris_Vanherck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 sec
Reputation Power: 5
Quote:
Originally Posted by Tonny-Soeroso
in ERP Development, if we use view, query may be in the lock position in the server because we must load all data first before filtering.

no such thing exists in SQL Server

PHP Code:
 SELECT TOP 3 CountryCatDescription 
FROM VIEW1 
WHERE Country 
'hong kong' 
GROUP BY CountryCatDescription 
ORDER BY Country 
UNION
SELECT TOP 3 Country
CatDescription 
FROM VIEW1 
WHERE Country 
'LA' 
GROUP BY CountryCatDescription 
ORDER BY Country 

Reply With Quote
  #4  
Old July 27th, 2004, 02:56 AM
Tonny-Soeroso Tonny-Soeroso is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 36 Tonny-Soeroso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 56 m 27 sec
Reputation Power: 0
SELECT TOP 3 Country, CatDescription
FROM VIEW1
WHERE Country
=
'hong kong'
GROUP BY Country,
CatDescription
ORDER BY Country
UNION
SELECT TOP 3 Country
,
CatDescription
FROM VIEW1
WHERE Country
=
'LA'
GROUP BY Country,
CatDescription
ORDER BY Country

I think that is no smart solution, how do you know that country only exist 'hongkong' and 'la' value?
Why we must hard code the where condition? How about using parameter? may be this is more rasionable.

Reply With Quote
  #5  
Old July 27th, 2004, 06:35 AM
Kris_Vanherck's Avatar
Kris_Vanherck Kris_Vanherck is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Belgium, Antwerp
Posts: 177 Kris_Vanherck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 sec
Reputation Power: 5
his previous question (http://forums.aspfree.com/t33428/s.html) makes me asume he's calling it from code somewhere , and i just gave him an example of how to do it, the smart person can take it from there

Reply With Quote
  #6  
Old July 27th, 2004, 09:22 PM
mae123 mae123 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 mae123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
passing variable

the country is passed as the coundition. so, I can select might be more than 3 country.
Union is not really solving it right? any good suggestion?

Reply With Quote
  #7  
Old July 28th, 2004, 02:39 AM
Kris_Vanherck's Avatar
Kris_Vanherck Kris_Vanherck is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Belgium, Antwerp
Posts: 177 Kris_Vanherck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 sec
Reputation Power: 5
unions are not limited to 2 selects, you can do
PHP Code:
 SELECT ....
UNION
SELECT 
....
UNION
SELECT 
....
UNION
.
.



here is an alternative (i used ID for the primary key), but it's not much better:
PHP Code:
 SELECT CountryCatDescription 
FROM VIEW1 
WHERE ID IN 
(SELECT TOP 1 ID FROM View1 WHERE Country 'hong kong'
OR 
ID IN (SELECT TOP 1 ID FROM View1 WHERE Country 'hong kong') AND ID NOT IN (SELECT TOP 1 ID FROM View1 WHERE (Country 'hong kong'))
OR 
ID IN (SELECT TOP 1 ID FROM View1 WHERE Country 'hong kong') AND ID NOT IN (SELECT TOP 2 ID FROM View1 WHERE (Country 'hong kong'))
OR 
ID IN (SELECT TOP 1 ID FROM View1 WHERE Country 'LA'
OR 
ID IN (SELECT TOP 1 ID FROM View1 WHERE Country 'LA') AND ID NOT IN (SELECT TOP 1 ID FROM View1 WHERE (Country 'LA'))
OR 
ID IN (SELECT TOP 1 ID FROM View1 WHERE Country 'LA') AND ID NOT IN (SELECT TOP 2 ID FROM View1 WHERE (Country 'LA'))
GROUP BY CountryCatDescription 
ORDER BY Country 

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > select Top 3


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 6 hosted by Hostway
Stay green...Green IT