Results 1 to 20 of 20

Thread: [RESOLVED] Eliminating multiple entrances in SQL query

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Resolved [RESOLVED] Eliminating multiple entrances in SQL query

    ello all

    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:
    1. "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,
    Last edited by josephine; Jul 11th, 2006 at 09:05 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    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 "

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    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,

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    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).

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Eliminating multiple entrances in SQL query

    You are very welcome! Glad to help

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    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.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    answers:

    Quote Originally Posted by szlamany
    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
    as requested, attached are data samples.

    "Source Table" uses the following SQL query:
    Code:
    select B.serial, B.version, CONVERT(char(20), B.date,113) 'Date', C.name 'Status' from BLCodes C, Bootloader B WHERE B.status = C.code
    as you can see, there are multiple entries for each serial.
    an entry is created whenever a Serial reports either, "Start (value=in progress)" or "Finish (value=finished)".

    required entries from Source Table are:
    if Serial reported "Finish" then show date in 'date finished' and show one date prior to latest date is 'Date Started'.
    if Serial reported "Started" and did not report "Finish", i.e., it is "In Progress" then show date in 'date started' and status = "in progress" no 'date finished'

    format of data presentation should be somethin like "Desired Table", i.e., one row per Serial.

    pls lemme know if any other info is needed...
    and again, much tnx for your help, i really appreciate it.

    regards,
    Attached Images Attached Images    

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Re: Eliminating multiple entrances in SQL query

    absolutely!
    look in current table att. that is you suggestion of yesterday.

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    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!!!
    Attached Images Attached Images  

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    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.

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    results:

    Thread Epilogue.

    much thanks for all your help on this... esp the code comments, through which i learned a lot.

    (best result i got is captured in post #15.)

    happy friday!

    regards,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width