Results 1 to 7 of 7

Thread: [RESOLVED] ANOTHER SQL Question ...

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Resolved [RESOLVED] ANOTHER SQL Question ...

    My SQL isn't that sharp, so I hope that there is an easy answer to this one.

    The following grabs records from a table, and I format a few fields along the way. Thats all fine.

    VB Code:
    1. SELECT     a.BRANCH_NUMBER, b.EST_BRNM, a.DIVISION, a.REGION, a.REGIONAL_MGR, a.SAFETY_ADVISOR, SUBSTRING(Audit_Date, 7, 2)
    2.                       + '/' + SUBSTRING(Audit_Date, 5, 2) + '/' + SUBSTRING(Audit_Date, 1, 4) AS Audit_Date, a.WEIGHTED_RESULT, a.WEIGHTED_PERCENTAGE,
    3.                       SUBSTRING(Audit_Review_Date, 7, 2) + '/' + SUBSTRING(Audit_Review_Date, 5, 2) + '/' + SUBSTRING(Audit_Review_Date, 1, 4) AS Audit_Review_Date,
    4.                        a.ACTION_PLAN_COMPLETE, SUBSTRING(Action_Plan_Completion_Date, 7, 2) + '/' + SUBSTRING(Action_Plan_Completion_Date, 5, 2)
    5.                       + '/' + SUBSTRING(Action_Plan_Completion_Date, 1, 4) AS Action_Plan_Completion_Date, a.RISK_INDICATOR,
    6.                       CASE RISK_INDICATOR WHEN 'R' THEN 'A' WHEN 'A' THEN 'B' WHEN 'G' THEN 'C' END AS SortCode
    7. FROM         AUDIT_LOCATION_SUMMARY a INNER JOIN
    8.                       ESTATES.DBO.BRANCH_DETAILS b ON (b.EST_CHAN = a.CHAIN AND b.EST_BRAN = a.BRANCH_NUMBER)
    9. WHERE     a.CHAIN = '01'
    10. ORDER BY 14 DESC

    Now ... For each Branch there may be more than 1 record returned, based on "Audit_Date". I want all these fields, but only the LATEST date for each Branch, i.e. if more than 1 record exists for any Branch I only want the one with the latest date.

    Bearing in mind I am using SUBSTRING to turn my dates around (they are stored as strings in the format YYYYMMDD), I am having trouble using the MAX command.

    Am I barking up the wrong tree ?
    Last edited by Hack; Jul 20th, 2006 at 12:52 PM. Reason: Added [RESOLVED] to thread title Last edited by TheBionicOrange : Today at 07:05 AM.

  2. #2

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: ANOTHER SQL Question ...

    OK figured this one out too ...

    I changed it to ....

    VB Code:
    1. SELECT     MAX(a.AUDIT_DATE), a.BRANCH_NUMBER, b.EST_BRNM, a.DIVISION, a.REGION, a.REGIONAL_MGR, a.SAFETY_ADVISOR, SUBSTRING(Audit_Date, 7,
    2.                       2) + '/' + SUBSTRING(Audit_Date, 5, 2) + '/' + SUBSTRING(Audit_Date, 1, 4) AS Audit_Date, a.WEIGHTED_RESULT, a.WEIGHTED_PERCENTAGE,
    3.                       SUBSTRING(Audit_Review_Date, 7, 2) + '/' + SUBSTRING(Audit_Review_Date, 5, 2) + '/' + SUBSTRING(Audit_Review_Date, 1, 4) AS Audit_Review_Date,
    4.                        a.ACTION_PLAN_COMPLETE, SUBSTRING(Action_Plan_Completion_Date, 7, 2) + '/' + SUBSTRING(Action_Plan_Completion_Date, 5, 2)
    5.                       + '/' + SUBSTRING(Action_Plan_Completion_Date, 1, 4) AS Action_Plan_Completion_Date, a.RISK_INDICATOR,
    6.                       CASE RISK_INDICATOR WHEN 'R' THEN 'A' WHEN 'A' THEN 'B' WHEN 'G' THEN 'C' END AS SortCode
    7. FROM         AUDIT_LOCATION_SUMMARY a INNER JOIN
    8.                       ESTATES.DBO.BRANCH_DETAILS b ON (b.EST_CHAN = a.CHAIN AND b.EST_BRAN = a.BRANCH_NUMBER)
    9. WHERE     a.CHAIN = '01'
    10. GROUP BY a.AUDIT_DATE, a.BRANCH_NUMBER, b.EST_BRNM, a.DIVISION, a.REGION, a.REGIONAL_MGR, a.SAFETY_ADVISOR, a.AUDIT_DATE,
    11.                       a.WEIGHTED_RESULT, a.WEIGHTED_PERCENTAGE, a.AUDIT_REVIEW_DATE, a.ACTION_PLAN_COMPLETE, a.ACTION_PLAN_COMPLETION_DATE,
    12.                       a.RISK_INDICATOR
    13. ORDER BY 15

    This is starting to be a very long-winded statement !

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

    Re: ANOTHER SQL Question ...

    This is MS SQL Server - right?

    If so, start using user defined functions - they are great!

    Code:
    Create Function dbo.FormatDate(@InDate varchar(8))
       Returns varchar(10) as
    Begin
    	Declare @OutDate varchar(10)
    	Set @OutDate=SubString(@InDate,5,2)+'/'+Right(@InDate,2)+'/'+Left(@InDate,4)
    	Return @OutDate
    End
    Go
    Select dbo.FormatDate('20060131')
    Here is how to create one - and a sample of how to call it.

    This returns:

    Code:
               
    ---------- 
    01/31/2006
    
    (1 row(s) affected)

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

  4. #4

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: ANOTHER SQL Question ...

    That seems like a sound idea

    Where do these user defined functions get stored ?

    If its any help I use Enterprise Manager to access my tables, DTS packages etc.

    Oh and yes it is Microsoft SQL Server.

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

    Re: ANOTHER SQL Question ...

    We never use EM to manipulate our DB's - we always use QUERY ANALYZER and save these little scripts as .SQL text files - they go into SOURCE SAFE nicely that way.

    but at any rate - the UDF is stored in the DB just like a SPROC is - just defined a bit differently and the dbo. prefix is required.

    In EM there is a branch for USER DEFINED FUNCTIONS just like TABLES, VIEWS and STORED PROCEDURES - they go in there.

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

  6. #6

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: ANOTHER SQL Question ...

    Brilliant. Its about time I started learning how to do these things with a bit more structure to them.
    Thanks a lot Szlamany

    Thats hard enough to spell sober .. I would hate to try and type that whilst drunk !!

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

    Re: ANOTHER SQL Question ...

    Glad to help!

    "sz" is good enough...

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

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