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 > Need Help on SQL statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-05-07, 23:22
ontology ontology is offline
Registered User
 
Join Date: May 2007
Posts: 2
Question Need Help on SQL statement

Hi everyone , I have a table as follow


CustID Name ComplainDate Status
10001 | Nina | 10-Jun-06 | Accept
10001 | Nina | 11-Jun-06 | Accept
10001 | Nina | 13-Jun-06 | Reject
10002 | John | 12-Jan-00 | Reject
10002 | John | 13-Jan-00 | Accept
10003 | Daniel | 18-Feb-00 | Reject

but when I create a query view I need to appear with the ranking column but I no idea how to write the SQL statement which output of the view as follow:

CustID Name ComplainDate Status Rank
10001 | Nina | 10-Jun-06 | Accept | 1
10001 | Nina | 11-Jun-06 | Accept | 2
10001 | Nina | 13-Jun-06 | Reject | 3
10002 | John | 12-Jan-00 | Reject | 1
10002 | John | 13-Jan-00 | Accept | 2
10003 | Daniel | 18-Feb-00 | Reject | 1


Anyone pls help thanks in advance
Reply With Quote
  #2 (permalink)  
Old 05-06-07, 00:28
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
This is the general method. Things get a little more complex if CustID and ComplainDate do not represent a unique key.
Code:
Select	[YourTable].CustID,
	[YourTable].Name,
	[YourTable].ComplainDate,
	[YourTable].Status,
	count(*) as Rank
from	[YourTable]
	inner join [YourTable] Ordinal
		on [YourTable].CustID = Ordinal.CustID
		and [YourTable].ComplainDate >= Ordinal.ComplainDate
group by [YourTable].CustID,
	[YourTable].Name,
	[YourTable].ComplainDate,
	[YourTable].Status
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 05-06-07, 22:36
ontology ontology is offline
Registered User
 
Join Date: May 2007
Posts: 2
thanks for your solution man
Reply With Quote
  #4 (permalink)  
Old 05-08-07, 09:33
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
There is a better way to do this without using the count() function that SQL provides. Imagine if you had to do a count for 10,000 rows
It requires that you use an incrementing variable integer.
Give me a day and i'm sure I can work it out... it's a tricky process..
Reply With Quote
  #5 (permalink)  
Old 05-08-07, 11:12
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What's wrong with the COUNT? The system has to process all rows anyway. With a suitable execution plan, it will process rows by group already and then the count is trivially computed along the way.

Another alternative is to use the SQL RANK or DENSE_RANK window functions.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development

Last edited by stolze; 05-08-07 at 11:40.
Reply With Quote
  #6 (permalink)  
Old 05-08-07, 12:05
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
aschk, if he just wanted an ordinal value across the entire dataset he could select it into a temporary table with an auto-incrementing Identity. I think this is what you are thinking of. But the poster wants ordinal values across groups within the data, so that technique won't work.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 05-09-07, 03:17
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Right, using GENERATED ALWAYS/DEFAULT identity columns or sequences would be applicable to the whole table. But there is another drawback for identity columns: you would have to materialize the temp data. Subselects also produce (logical) temp tables, but no materialization is necessary.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 05-09-07, 06:51
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
As an example of what i was going to demonstrate :

Code:
SELECT CustID,Name,ComplainDate,Status
      ,IF(@custno = CustID
      ,@rank := @rank + 1
      ,@rank := 1 + least(0,@custno := CustID)
     ) Rank
FROM test AS t, (select (@custno:=0),(@rank:=0)) x
ORDER BY CustID
No count

Last edited by aschk; 05-09-07 at 09:08.
Reply With Quote
  #9 (permalink)  
Old 05-09-07, 12:04
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Besides, the above syntax not being valid SQL, you have another major problem: That won't work at all because it assumes that the ORDER BY is executed before the SELECT list. But SQL states that first the SELECT list is evaluated and then any ORDER BY applied. Thus, the rows may come in any order first, where the rank thing is computed. Then the whole thing is sorted by CustID. Obviously, you have no guarantee for specific Rank values.

So your only options (with standard SQL) are COUNT, RANK/DENSE_RANK, ROW_NUMBER, or common table expressions.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 05-10-07, 07:15
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Apologies, I always seem to find myself in the SQL forum when I should be in the MySQL forum
MySQL doesn't have the RANK/DENSE_RANK/OVER PARTITION options that other DBS exhibit. Hence the solution offered.
Reply With Quote
  #11 (permalink)  
Old 05-10-07, 12:08
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
This being a general "SQL" forum, your answer was perfectly appropriate, but if it uses syntax specific to MySQL you should note that.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
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