1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Anyone good with SQL?

Discussion in 'BBS Hangout' started by KMC1499, Apr 19, 2013.

Tags:
  1. KMC1499

    KMC1499 Member

    Joined:
    Dec 6, 2002
    Messages:
    170
    Likes Received:
    2
    I need a little help getting the data back that i need from the SQl server

    Basically i need to add all the values of pages recorded for one user on unique instrument numbers. im getting at error that say incorrect syntax near ')' with the last ) highlighted.
    Any help would be greatly appreciated

    SELECT SUM(T1."NO_PAGES_RECORDED") AS Expr1
    FROM(SELECT DISTINCT(T1."INSTRUMENT_NUMBER")
    FROM "Anthem"."dbo"."OPR_PHYS_DOCUMENTS" T1, "Anthem"."dbo"."USERS" T2, "Anthem"."dbo"."OPR_LOGI_DOCUMENTS" T3 LEFT OUTER JOIN "Anthem"."dbo"."AUDIT_HEADER" T4 on T3."GLOBAL_ID" = T4."GLOBAL_ID" where T2."USER_ID" = T4."USER_ID" and T3."INSTRUMENT_NUMBER" = T1."INSTRUMENT_NUMBER" and T1."DATE_RECEIVED" between '2013-04-17 00:00:00.000' and '2013-04-17 23:00:00.000')
     
  2. EssTooKayTD

    EssTooKayTD Contributing Member

    Joined:
    Dec 15, 2005
    Messages:
    3,341
    Likes Received:
    73
    Your sum is referencing a table in your embedded statement I think is the problem. Not 100% sure on that, but maybe take the embedded statement, put it in a temp table, then do a sum on that temp table.

    Not to mention, it looks like you are trying to get the total number of pages, but your embedded select is returning instrument numbers.
     
  3. DrLudicrous

    DrLudicrous Contributing Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    Try this, I think it may work.

    SELECT SUM(T1."NO_PAGES_RECORDED") AS Expr1

    FROM "Anthem"."dbo"."OPR_PHYS_DOCUMENTS" T1, "Anthem"."dbo"."USERS" T2, "Anthem"."dbo"."OPR_LOGI_DOCUMENTS" T3 LEFT OUTER JOIN "Anthem"."dbo"."AUDIT_HEADER" T4 on T3."GLOBAL_ID" = T4."GLOBAL_ID" where T2."USER_ID" = T4."USER_ID" and T3."INSTRUMENT_NUMBER" = T1."INSTRUMENT_NUMBER" and T1."DATE_RECEIVED" between '2013-04-17 00:00:00.000' and '2013-04-17 23:00:00.000'

    group by T1."INSTRUMENT_NUMBER"
     
  4. KMC1499

    KMC1499 Member

    Joined:
    Dec 6, 2002
    Messages:
    170
    Likes Received:
    2
    Thanks for the prompt reply
    Unfortunately im not going to be able to alter the database just read records from it so i know that limits me.'

    DrLudicrous your SQL statement did return the expected number of rows but the count was way off so i need to figure out whats going on there. And i have been pulled in another direction now so i may not respond for a while.
     
  5. DrLudicrous

    DrLudicrous Contributing Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    Looking at what you're trying to retrieve you may also need to group by the user ID, although that would probably increase the number of rows returned.

    SELECT SUM(T1."NO_PAGES_RECORDED") AS Expr1, T1."INSTRUMENT_NUMBER", T2."USER_ID"
    FROM "Anthem"."dbo"."OPR_PHYS_DOCUMENTS" T1, "Anthem"."dbo"."USERS" T2, "Anthem"."dbo"."OPR_LOGI_DOCUMENTS" T3 LEFT OUTER JOIN "Anthem"."dbo"."AUDIT_HEADER" T4 on T3."GLOBAL_ID" = T4."GLOBAL_ID" where T2."USER_ID" = T4."USER_ID" and T3."INSTRUMENT_NUMBER" = T1."INSTRUMENT_NUMBER" and T1."DATE_RECEIVED" between '2013-04-17 00:00:00.000' and '2013-04-17 23:00:00.000'

    group by T1."INSTRUMENT_NUMBER", T2."USER_ID"
     
  6. KMC1499

    KMC1499 Member

    Joined:
    Dec 6, 2002
    Messages:
    170
    Likes Received:
    2
    I think the issue is the system user "admin" may have their results excluded for the records but not sitting at the cpu I couldnt tell ya
     
  7. wizkid83

    wizkid83 Contributing Member

    Joined:
    May 20, 2002
    Messages:
    6,335
    Likes Received:
    847
    do you need userid as one of the fields? How come it's not in your select statement on top?
     
  8. KMC1499

    KMC1499 Member

    Joined:
    Dec 6, 2002
    Messages:
    170
    Likes Received:
    2
    Userid is used to join the tables
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now