[RESOLVED] Eliminating multiple entrances in SQL query
ello all :wave:
in a table i have multiple entrances.
can i, via the SQL statement, make sure i have just 1 entry per B.serial?
and that the above entry will have the latest B.date?
SQL statement now:
VB Code:
"select B.serial, B.version, CONVERT(char(20), B.date,113) 'Date Started', C.name 'Status' from BLCodes C, Bootloader B WHERE B.status = C.code"
tnx in advance for help on this.
regards,
Re: Eliminating multiple entrances in SQL query
SQL has aggregate functions - they work in unison with the GROUP BY clause.
Code:
SELECT B.serial, B.version, CONVERT(char(20), MAX(B.date),113) 'Latest Date Started'
FROM ...
GROUP BY B.Serial, B.Version
Issue is that everything NOT in an AGGREGATE function must be in the GROUP BY clause - so you will have issues getting the STATUS. If you want the status of the latest entry then that's a different query all together...
Re: Eliminating multiple entrances in SQL query
1st, tnx 4 a quick reply szalmany. howzit..
unfortunately i get an error as i run the script.
not sure i fully get the issue of
"everything NOT in an AGGREGATE function must be in the GROUP BY clause "
Re: Eliminating multiple entrances in SQL query
Yes - that's exactly the error I was talking about in my post.
You either put a field in an AGGREGATE function - such as MAX(), MIN() or SUM()...
or you put it in the GROUP BY clause.
You will need to give a more clear example of your data and what you are trying to accomplish before we can give specific help.
Re: Eliminating multiple entrances in SQL query
Quote:
Originally Posted by szlamany
You will need to give a more clear example of your data and what you are trying to accomplish before we can give specific help.
enviorment is MS SQL;
2 tables: B=> B.id,B.Serial, b.version, B.state, B.date
C=>C.codeId, C.name, c.code
B.state is related to C.code. and shows in it value of C.name
at present my result is a table with headers:
Serial; Version; Date; Status;
table has multiple entrances (on same serial) for status "started" "finished".
need a query to display one row per serial,
in it:the latest date entry for "Started" (i.e., ignore earlier dates).
the latest date entry for "Finished"
the status of the above entry (started/finished)
hope its clearer now, if any info missin, pls advise..
tnx a lot 4 the help.
regards,
Re: Eliminating multiple entrances in SQL query
So that seems more to me like you want to "select" only a specific row for a serial no - and that row happens to have the "latest" date for that serial number.
Also note that I've put a more conventional FROM/JOIN clause on that SELECT. Since the "primary" table you are attacking is the Bootloader - it should be the FROM table. JOIN to it any "related" tables - such as the description from the BLCodes table.
Code:
select B.serial
, B.version
, CONVERT(char(20), B.date,113) 'Date Started'
, C.name 'Status'
from Bootloader B
Left join BLCodes C on C.Code=B.Status
Where B.Date=(Select Max(B1.Date) From Bootloader B1
Where B1.Serial=B.Serial)
So the sub-query "brings up" the latest date for a serial number and the WHERE clause only selects that row. Does version or state play into this? If so add them to the WHERE clause of the sub-query.
Re: Eliminating multiple entrances in SQL query
Quote:
Originally Posted by szlamany
So the sub-query "brings up" the latest date for a serial number and the WHERE clause only selects that row. Does version or state play into this? If so add them to the WHERE clause of the sub-query.
state does play into it, and i added it as you suggested.
its works perfectly!!!
tnx so much again for your help szlamany!! i appreciate it, (as rated).
Re: [RESOLVED] Eliminating multiple entrances in SQL query
You are very welcome! Glad to help :)
Re: Eliminating multiple entrances in SQL query
Quote:
Originally Posted by szlamany
Since the "primary" table you are attacking is the Bootloader - it should be the FROM table. JOIN to it any "related" tables - such as the description from the BLCodes table.
sorry to re open this.. but one more question if i may please, as i'm very confused by it.
in "primary" bootloader table entries are added once with B.status=0 (start)
and the second time with b.status=1 (finish). (multiple entries for same serial)
with your help i managed to "ignore" the entries with older dates, and just check the latest date and its status.
now client wants to display date started and date finished, for every Bootload;
i.e.,
if latest date has status code 0 (started), file is in progress. and there is no date finished.
if latest date has status code 1 (finished), bootload proccess is done, and i must show : date started (one date smaller than MAX(date))
and date finished MAX(date)
hope this is clear... as its not so clear for me. :blush:
Re: Eliminating multiple entrances in SQL query
Are there only ever 2 rows for a serial number?
How does VERSION and STATE play into this?
Showing me some sample data would be really helpful in making this a one or two post answer ;)
Re: Eliminating multiple entrances in SQL query
CH0000000129 has a IN PROGRESS, FINISHED and another IN PROGRESS...
That's possible?
But you only want to show the CH0000000129 row once - with the latest IN PROGRESS and the potential FINISHED that follows that - right?
Re: Eliminating multiple entrances in SQL query
absolutely!
look in current table att. that is you suggestion of yesterday.
Re: Eliminating multiple entrances in SQL query
Ok - so this modification to yesterdays query will only select the LATEST IN-PROGRESS entry - right?
Code:
select B.serial
, B.version
, CONVERT(char(20), B.date,113) 'Date Started'
, C.name 'Status'
from Bootloader B
Left join BLCodes C on C.Code=B.Status
Where B.Date=(Select Max(B1.Date) From Bootloader B1
Where B1.Serial=B.Serial and B1.Status='In Progress')
Then this JOIN should add to that the possible FINISHED entry that might follow it.
Code:
select B.serial
, B.version
, CONVERT(char(20), B.date,113) 'Date Started'
, CONVERT(char(20), BF.date,113) 'Date Finished'
, C.name 'Status'
from Bootloader B
Left join BLCodes C on C.Code=B.Status
Left Join Bootloader BF on BF.Serial=B.Serial and BF.Date>=B.Date
and BF.Status='Finished'
Where B.Date=(Select Max(B1.Date) From Bootloader B1
Where B1.Serial=B.Serial and B1.Status='In Progress')
If you need to add version to these joins make sure to put them in the WHERE Sub-query and the new BF JOIN.
If you want to process one date or the other do this:
Case When BF.Serial is null Then B.Date Else BF.Date End
This checks if the BF row exists and optionally returns either the B.Date or BF.Date...
1 Attachment(s)
Re: Eliminating multiple entrances in SQL query
Quote:
Originally Posted by szlamany
---
wow!
took me 10 minutes just to read the statemnet. and im very happy to say i understand it!
have changed only 2 things:
the text 'in progress' to a numeric code 0.
the text 'finished' to numeric code 1.
statement now is
Code:
select B.serial, B.version, CONVERT(char(20), B.date,113) 'Date Started', CONVERT(char(20), BF.date,113) 'Date Finished', C.name 'Status' from Bootloader B Left join BLCodes C on C.Code=B.Status Left Join Bootloader BF on BF.Serial=B.Serial and BF.Date>=B.Date and BF.Status=1 Where B.Date=(Select Max(B1.Date) From Bootloader B1 Where B1.Serial=B.Serial and B1.Status=0)
Please check result of statement is in attachment below; (last.jpg)
only problem is they all say "IN PROGRESS" when only CH1000000129 is actually in progress. (it has no "Date Finish"....)
if can work around it=> PERFECT!
if not, => PERFECT!
Sir, you are a star!!!
Re: Eliminating multiple entrances in SQL query
I'm glad it's working for you!
This should do the last step you need:
Code:
select B.serial
, B.version
, CONVERT(char(20), B.date,113) 'Date Started'
, CONVERT(char(20), BF.date,113) 'Date Finished'
, Isnull(CF.name,C.Name) 'Status'
from Bootloader B
Left join BLCodes C on C.Code=B.Status
Left Join Bootloader BF on BF.Serial=B.Serial
and BF.Date>=B.Date and BF.Status=1
Left join BLCodes CF on C.Code=BF.Status
Where B.Date=(Select Max(B1.Date)
From Bootloader B1
Where B1.Serial=B.Serial and B1.Status=0)
by JOIN'ing the Code table on the BF code you get that data only if the serial # is completed - and ISNULL() will return the proper one.
You could also use the CASE...IS NULL... type statement I posted back a bit - but ISNULL() function should do it.
I'm off to an all afternoon meeting - I'll check back later.
Re: Eliminating multiple entrances in SQL query
I edited that last post - the JOIN was in a bad spot - hope you see the correct version!
Re: Eliminating multiple entrances in SQL query
hope you have a good meeting,
my ems says:
"The column prefix 'BF' does not match with a table name or alias name used in the query."
i must leave too.
have a good evening.see ya 2moro.
Re: Eliminating multiple entrances in SQL query
That should be fixed by the EDIT I made - the position of that JOIN was important - it needed to be after the BF join.