简体   繁体   中英

Aggregate query returning multiple rows when I only want one

this is the original query

  SELECT     'MSD-RES-CRUISE' AS QUERYNAME, dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT(dbo.BOOKINGS.USERID) AS TOTAL2 
    FROM         dbo.BOOKINGS INNER JOIN  dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
                  dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR 
    WHERE     (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, Dateadd(day,2, @startdate)) AND CONVERT(int, Dateadd(day,2, @enddate))) AND (dbo.MAJOR.SDESCR = 'Cruises') AND  
                          (dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A') and dbo.BOOKINGS.STATUS <> 'XL' GROUP BY dbo.BOOKINGS.USERID 

the query i want to join

 SELECT     calltimeint, avetimeint
    FROM         dbo.agentdailycalls
    where userid = @user and date1 between @startdate and @enddate

where the userids match and the groupname matches query name

this is what i used

SELECT     t0.QUERYNAME, t0.USERID, total, TOTAL2, calltimeint, avetimeint 
FROM       (    SELECT     'MSD-RES-CRUISE' AS QUERYNAME, dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT(dbo.BOOKINGS.USERID) AS TOTAL2 
    FROM         dbo.BOOKINGS INNER JOIN  dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
                  dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR 
    WHERE     (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, Dateadd(day,2, @startdate)) AND CONVERT(int, Dateadd(day,2, @enddate))) AND (dbo.MAJOR.SDESCR = 'Cruises') AND  
                          (dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A') and dbo.BOOKINGS.STATUS <> 'XL' GROUP BY dbo.BOOKINGS.USERID 
) t0 
INNER JOIN (   SELECT     sum(calltimeint) as calltimeint, sum(avetimeint) as avetimeint , userid
    FROM         dbo.agentdailycalls
    where userid = @user and date1 between @startdate and @enddate and GroupName = 'MSD-RES-CRUISE'
 group by userid
) t1         ON t1.userId = t0.USERID 

Just do a JOIN on the two queries:

SELECT     QUERYNAME, USERID, total, TOTAL2, calltimeint, avetimeint
FROM       ('MSD-RES-CRUISE' ... GROUP BY abcfiles.dbo.BOOKINGS.USERID) t0
INNER JOIN (SELECT userid, groupname, calltimeint, avetimeint ... and @enddate) t1
        ON t1.userId = t0.USERID AND t01.groupname = t0.QUERYNAME

OLD ANSWER, based on a previous version of the question

Instead of selecting calltimeint and avetimeint it looks like you want to select the SUM of each of those columns, and remove them from the GROUP BY :

SELECT     ... SUM(dbo.agentdailycalls.calltimeint), SUM(dbo.agentdailycalls.avetimeint)
FROM       dbo.BOOKINGS INNER JOIN ...
GROUP BY   dbo.BOOKINGS.USERID

Adding them to the GROUP BY means you want a row for every different combination of these values. So, GROUP BY userid, calltimeint, avetimeint means "return one row for every different combination of userid, call time, and average time".

What you really meant (presumably) is "return one row for each user", so you should only have userid in the GROUP BY .

Try This:

SELECT     'MSD-RES-CRUISE' AS QUERYNAME
        , abcfiles.dbo.BOOKINGS.USERID
            , SUM(tt.calltime)
            , SUM(tt.aveTimeint)
        , SUM(abcfiles.dbo.BOOKINGS.APRICE) AS total
        , COUNT(abcfiles.dbo.BOOKINGS.USERID) AS TOTAL2 
FROM         abcfiles.dbo.BOOKINGS 
INNER JOIN  abcfiles.dbo.TOURS 
            ON abcfiles.dbo.BOOKINGS.TOUR = abcfiles.dbo.TOURS.TOUR 
INNER JOIN  abcfiles.dbo.MAJOR 
            ON abcfiles.dbo.TOURS.MAJOR = abcfiles.dbo.MAJOR.MAJOR
INNER JOIN         (SELECT 
                     calltime
               , aveTimeint
            FROM 
                    dbo.agentdailycalls adc
            ) tt 
                     ON tt.userid = abcfiles.dbo.BOOKINDS.USERID 
                    AND tt.date1 BETWEEN @startdate AND @endDate***                         
WHERE     (abcfiles.dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, Dateadd(day,2, @startdate)) AND CONVERT(int, Dateadd(day,2, @enddate))) 
AND (abcfiles.dbo.MAJOR.SDESCR = 'Cruises') 
AND  (abcfiles.dbo.BOOKINGS.USERID = @USER) 
AND (abcfiles.dbo.MAJOR.DIVISION = 'A') 
and abcfiles.dbo.BOOKINGS.STATUS <> 'XL' 
GROUP BY abcfiles.dbo.BOOKINGS.USERID 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM