If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sql (Inner Join)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-04, 08:45
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
Sql (Inner Join)

I have 2 tables. The first table name register and the second table name showrank.
Table register

id name username
1 ae1 w
2 ae2 e
3 ae3 r
4 ae4 t
5 ae5 u

Table showrank
id totalgainloss
1 200
2 500
3 100
4 90
5 150

I want the result to show the first three highest totalgainloss. with name... like this

ae2 500
ae1 200
ae5 150

I tried
SELECT register.*, showrank.totalgainloss FROM register INNER JOIN showrank ON register.id = showrank.id ORDER BY showrank.totalgainloss DESC ;"

But the result that I got was
ae2 500
ae1 200
ae5 150
ae3 100
ae4 90

I want only the first three highest totalgainloss. Anyone know how to do it !!
Reply With Quote
  #2 (permalink)  
Old 09-08-04, 09:29
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Which DBMS? Each has its own proprietary ways of doing "top n" queries.

However, a generic (slower) SQL solution would be something like:

SELECT register.*, showrank.totalgainloss
FROM register
INNER JOIN showrank ON register.id = showrank.id
WHERE 3 >= (SELECT COUNT(DISTINCT totalgainloss) FROM showrank s2 WHERE s2.totalgainloss >= showrank.totalgainloss)
ORDER BY showrank.totalgainloss DESC ;"
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 09-08-04, 10:12
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
I use Microsoft Access 2000
I have already tried your sql but it doesn't work !!
Reply With Quote
  #4 (permalink)  
Old 09-08-04, 10:43
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
"Doesn't work" in what sense? Returns too many rows? Gives a syntax error message?

In SQL Server I believe you can do something like "SELECT TOP 3 ..."
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 09-08-04, 11:08
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
This is the message that I got when I type your sql

Microsoft JET Database Engine error '80040e14'

Syntax error. in query expression '3 >= (SELECT COUNT(DISTINCT totalgainloss) FROM showrank s2 WHERE s2.totalgainloss >= showrank.totalgainloss)'.
Reply With Quote
  #6 (permalink)  
Old 09-08-04, 11:36
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
OK, so that's something SQL Server can't handle for some reason. What about the "TOP 3" syntax then?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 09-08-04, 11:48
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
How can I write sql. If I use top synteax. I quite new in sql :-)
Please suggest !!
Reply With Quote
  #8 (permalink)  
Old 09-08-04, 12:42
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Try asking in the SQL Server forum. I don't know SQL Server.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #9 (permalink)  
Old 09-08-04, 14:01
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
The Jet database engine used by MS-Access isn't quite standard SQL, but it ought to process:
Code:
SELECT TOP 3 register.*, showrank.totalgainloss
   FROM register
   INNER JOIN showrank ON register.id = showrank.id
   ORDER BY showrank.totalgainloss DESC ;"
-PatP
Reply With Quote
  #10 (permalink)  
Old 09-08-04, 17:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by gop373
This is the message that I got when I type your sql

Microsoft JET Database Engine error '80040e14'

Syntax error. in query expression '3 >= (SELECT COUNT(DISTINCT totalgainloss) FROM showrank s2 WHERE s2.totalgainloss >= showrank.totalgainloss)'.
the jet engine cannot do COUNT DISTINCT
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 09-09-04, 04:38
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
I tried
SELECT TOP 3 register.*, showrank.totalgainloss
FROM register
INNER JOIN showrank ON register.id = showrank.id
ORDER BY showrank.totalgainloss DESC ;"

but I got message HTTP 500 - Internal server error :-(
Reply With Quote
  #12 (permalink)  
Old 09-09-04, 04:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by gop373
I got message HTTP 500 - Internal server error :-(
contact the help line, the database is broken
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On