Urgent help with SQL query

Discussion in 'Databases' started by mantra, Oct 10, 2010.

  1. #1
    I have the following query:

    SELECT dbo.portinfo.PORTINFOID, Sum(dbo.loccvg.VALUEAMT) AS SumOfVALUEAMT, dbo.loccvg.LOCID
    FROM (dbo.loc INNER JOIN dbo.loccvg ON dbo.loc.LOCID = dbo.loccvg.LOCID) INNER JOIN (dbo.portinfo INNER JOIN dbo.portacct ON dbo.portinfo.PORTINFOID = dbo.portacct.PORTINFOID) ON dbo.loc.ACCGRPID = dbo.portacct.ACCGRPID
    GROUP BY dbo.portinfo.PORTINFOID, dbo.loccvg.LOCID
    HAVING (((dbo.portinfo.PORTINFOID)=73) AND ((Sum(dbo.loccvg.VALUEAMT))>=50000 And (Sum(dbo.loccvg.VALUEAMT))<=100000));

    The query works fine. It brings the following type of results:

    PORTINFOID SumofVALUEAMT LOCID
    73 1 210
    73 34 211
    73 20 333

    I would like to nest the above query into a query that will give me a total sum of SumofVALUEAMT field and a count of the LOCID field.

    So my result should look like this

    PORTINFOID SUM LOCID
    73 55 3

    thanks
     
    mantra, Oct 10, 2010 IP
  2. mantra

    mantra Well-Known Member

    Messages:
    594
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #2
    seriously, no one can help here???

    anyone?? I need this done today.
     
    mantra, Oct 10, 2010 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    I'm not sure, but following should be of some help..

    
    SELECT dbo.portinfo.PORTINFOID, Sum(dbo.loccvg.VALUEAMT) AS SumOfVALUEAMT, COUNT(dbo.loccvg.LOCID)
    FROM (dbo.loc INNER JOIN dbo.loccvg ON dbo.loc.LOCID = dbo.loccvg.LOCID) INNER JOIN (dbo.portinfo INNER JOIN dbo.portacct ON dbo.portinfo.PORTINFOID = dbo.portacct.PORTINFOID) ON dbo.loc.ACCGRPID = dbo.portacct.ACCGRPID
    GROUP BY dbo.portinfo.PORTINFOID
    HAVING (((dbo.portinfo.PORTINFOID)=73) AND ((Sum(dbo.loccvg.VALUEAMT))>=50000 And (Sum(dbo.loccvg.VALUEAMT))<=100000));
    
    Code (markup):
     
    mastermunj, Oct 12, 2010 IP