473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help needed on confusing Select Query please.

4
Greetings,

I am currently trying to organize some data in a way that is straightforward to parse in my web application. Here is the query:

select SN, Date, ActionDefID, Actiondef.SeqNum, Actiondef.SubSeqNum
from dbo.ActionLog
join dbo.ActionDef on dbo.ActionLog.ActionDefID = ActionDef.ID_ActionDef
join dbo.serials on dbo.ActionLog.S_Number = dbo.serials.ID_Serials
WHERE LinkID = 1002
ORDER by SeqNum DESC, SubSeqNum DESC, SN DESC


This results in a table as follows:

SN | Date | ActionDefID | SeqNum | SubSeqNum

88671 2006-05-12 11:07:06.000 24 300 0
-88671 2006-05-11 11:17:06.000 23 200 0
88670 2006-05-11 11:17:06.000 23 200 0
88669 2006-05-11 11:17:06.000 23 200 0
88668 2006-05-11 11:17:06.000 23 200 0
88667 2006-05-11 11:17:06.000 23 200 0
-88671 2006-05-10 10:07:06.077 22 100 0
-88670 2006-05-10 10:07:06.077 22 100 0
-88669 2006-05-10 10:07:06.077 22 100 0
-88668 2006-05-10 10:07:06.077 22 100 0
-88667 2006-05-10 10:07:06.060 22 100 0
88666 2006-05-10 10:07:06.060 22 100 0
88665 2006-05-10 10:07:06.060 22 100 0
88664 2006-05-10 10:07:06.060 22 100 0
88663 2006-05-10 10:07:06.060 22 100 0
88662 2006-05-10 10:07:06.060 22 100 0
88661 2006-05-10 10:07:06.060 22 100 0
88660 2006-05-10 10:07:06.060 22 100 0
88659 2006-05-10 10:07:06.043 22 100 0
88658 2006-05-10 10:07:06.043 22 100 0
88657 2006-05-10 10:07:06.043 22 100 0


Now, what I would like is for all SNs to be UNIQUE based on the latest Date. This would be accomplished by removing all entires with a '-' in front of them. I have tried grouping by MAX(Date), but that has not worked for me either. The purpose of this Query is to identify the most current step that each SN in the process is at.

Hence wanting to remove duplicate SNs. Thank you in advance for any help you can provide me with!
May 10 '06 #1
7 2627
cweiss
36
If you removing anything with a '-' in front of it would give you the records you're looking for, why not just say something like:

SN Not Like '-%'
May 10 '06 #2
Juason
4
If you removing anything with a '-' in front of it would give you the records you're looking for, why not just say something like:

SN Not Like '-%'

I put the '-' there manually when I copied it into this post, just to illustrate which ones I didn't want returned :P
May 10 '06 #3
cweiss
36
So...that's not your results table :D

Hmm, you can't group on just date, because the other fields have different values. If the pattern of your data is consistent with what you have in the example, then you can group using max of all fields except SN. I don't know that you have to use a subquery, but you get the general idea:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   SN, MAX(DATE), MAX(ACTIONDEFID), MAX(SEQNUM), MAX(SUBSEQNUM)
  3.   FROM (select SN, Date, ActionDefID, Actiondef.SeqNum, Actiondef.SubSeqNum
  4. from dbo.ActionLog 
  5. join dbo.ActionDef on dbo.ActionLog.ActionDefID = ActionDef.ID_ActionDef
  6. join dbo.serials on dbo.ActionLog.S_Number = dbo.serials.ID_Serials
  7. WHERE LinkID = 1002
  8. ORDER by SeqNum DESC, SubSeqNum DESC, SN DESC)
  9. GROUP BY SN
I'm not 100% this will work, or if it's the best way to go about it though.
May 10 '06 #4
Juason
4
That gives me the following error :

The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.


The ActionLog table contains every single thing that happens in our process. What I'd like to do, is select a single LinkId, and find all the SNs associated with it in their most current states. Most current meaning, newest date-wise. Unfortunately my initial query has the SN duplicated, because it shows both new and old actions. I've just had no luck using the grouping. Thank you again for any help you can give me.



So...that's not your results table :D

Hmm, you can't group on just date, because the other fields have different values. If the pattern of your data is consistent with what you have in the example, then you can group using max of all fields except SN. I don't know that you have to use a subquery, but you get the general idea:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   SN, MAX(DATE), MAX(ACTIONDEFID), MAX(SEQNUM), MAX(SUBSEQNUM)
  3.   FROM (select SN, Date, ActionDefID, Actiondef.SeqNum, Actiondef.SubSeqNum
  4. from dbo.ActionLog 
  5. join dbo.ActionDef on dbo.ActionLog.ActionDefID = ActionDef.ID_ActionDef
  6. join dbo.serials on dbo.ActionLog.S_Number = dbo.serials.ID_Serials
  7. WHERE LinkID = 1002
  8. ORDER by SeqNum DESC, SubSeqNum DESC, SN DESC)
  9. GROUP BY SN
I'm not 100% this will work, or if it's the best way to go about it though.
May 10 '06 #5
cweiss
36
Try taking out the ORDER BY clause in the subquery and see if that works. Oh what dbms are you using? Oracle? MySQL? Access?

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   SN, MAX(DATE), MAX(ACTIONDEFID), MAX(SEQNUM), MAX(SUBSEQNUM)
  3.   FROM (select SN, Date, ActionDefID, Actiondef.SeqNum, Actiondef.SubSeqNum
  4. from dbo.ActionLog 
  5. join dbo.ActionDef on dbo.ActionLog.ActionDefID = ActionDef.ID_ActionDef
  6. join dbo.serials on dbo.ActionLog.S_Number = dbo.serials.ID_Serials
  7. WHERE LinkID = 1002)
  8. GROUP BY SN
May 10 '06 #6
Juason
4
I am using Microsoft SQL Server 2005. I get the following error without the ORDER:

Incorrect syntax near the keyword 'GROUP'.
May 10 '06 #7
cweiss
36
Well, this is one way you could do in oracle, but I don't know if it'll translate over into SQL Server:

Expand|Select|Wrap|Line Numbers
  1. SELECT A.SN, A.Date, A.ActionDefID, B.SeqNum, B.SubSeqNum
  2. FROM 
  3.   dbo.ActionLog A, 
  4.   ActionDef B, 
  5.   dbo.serials C
  6.   (SELECT SN, MAX(DATE) MAX_DATE FROM DBO.ACTIONLOG GROUP BY SN) D
  7. WHERE
  8. A.ActionDefID = B.ID_ActionDef AND
  9. A.S_Number = C.ID_Serials AND
  10. A.SN = D.SN AND
  11. A.DATE = D.MAX_DATE
  12. WHERE LinkID = 1002
  13. ORDER by SeqNum DESC, SubSeqNum DESC, SN DESC
If that doesn't work, then your only other option is to use Oracle :D

(or hopefully someone else here can help you, either way)
May 10 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Somerset Bob | last post by:
I've posted my query at the phpbb forum, where I got half an answer and then no more. I posted it again at another phpbb forum, where I got half an answer and no more. I posted it in alt.php, where...
0
by: Somerset Bob | last post by:
I've posted my query at the phpbb forum, where I got half an answer and then no more. I posted it again at another phpbb forum, where I got half an answer and no more. I posted it in alt.php, where...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.