简体   繁体   中英

sql query to find sum of all rows and count of duplicates

If data is in the following format:

SID  TID  Tdatetime        QID   QTotal  
----------------------------------------
100  1    01/12/97 9:00AM  66    110   
100  1    01/12/97 9:00AM  66    110  
100  1    01/12/97 10:00AM 67    110  
100  2    01/19/97 9:00AM  66    .  
100  2    01/19/97 9:00AM  66    110  
100  2    01/19/97 10:00AM 66    110  
100  3    01/26/97 9:00AM  68    120  
100  3    01/26/97 9:00AM  68    120  
110  1    02/03/97 10:00AM 68    110  
110  3    02/12/97 9:00AM  64    115  
110  3    02/12/97 9:00AM  64    115  
120  1    04/05/97 9:00AM  66    105  
120  1    04/05/97 10:00AM 66    105  

I would like to be able to write a query to sum the QTotal column for all rows and find the count of duplicate rows for the Tdatetime column.

The output would look like:

  Year   Total  Count
97 | 1340 | 4
The third column in the result does not include the count of distinct rows in the table. And the output is grouped by the year in the TDateTime column.

This will work if you really want to group by the tDateTime column:

SELECT DISTINCT tDateTime, SUM(QTotal), Count(distinct tDateTime)
FROM Table
GROUP BY tDateTime
HAVING  Count(distinct tDateTime) > 1

But your results look like you want to group by the Year in the tDateTime column. Is this correct?

If so try this:

SELECT DISTINCT YEAR (tDateTime), SUM(QTotal), Count(distinct tDateTime)
FROM Table
GROUP BY YEAR (tDateTime)
HAVING  Count(distinct tDateTime) > 1

The following query may help:

SELECT 
    'YEAR ' + CAST(sub.theYear AS VARCHAR(4)), 
    COUNT(sub.C), 
    (SELECT SUM(QTotal) FROM MyTable WHERE YEAR(Tdatetime) = sub.theYear) AS total
FROM 
   (SELECT 
        YEAR(Tdatetime) AS theYear, 
        COUNT(Tdatetime) AS C 
    FROM MyTable 
    GROUP BY Tdatetime, YEAR(Tdatetime)
    HAVING COUNT(Tdatetime) >= 2) AS sub

You must do SELECT from this table GROUPing by QTotal, using COUNT(subSELECT from this table WHERE QTotal is the same). If I only I had time I would write you SQL statement, but it'll take some minutes.

Something like:

select Year(Tdatetime)  ,sum(QTotal), count(1) from table group by year(Tdatetime )

or full date

select Tdatetime  ,sum(QTotal), count(1) from table group by year(Tdatetime) 

Or your ugly syntax ( : ) )

select 'Year ' + cast(Year(tdatetime) as varchar(4)) 
     + '|' + cast(sum(QTotal) as varchar(31)) 
     + '|' + cast(count(1) as varchar(31)) 
 from table group by year(Tdatetime )

Or do you want just the year? Sum all columns? Or just by year?

SELECT
 YEar + year(Tdatetime),
 SUM ( QTotal ),
 (SELECT COUNT(*) FROM ( 
 SELECT Tdatetime FROM tDateTime GROUP BY Tdatetime        
 HAVING COUNT(QID) > 1) C
FROM
 Tdatetime t

GROUP BY 
 YEar + year(Tdatetime)

This is the first time I have asked a question on stackoverflow. It looks like I have lost my original ID info. I had to register to login and add comments to the question I posted.

To answer OMG Ponies question, this is a SQL Server 2008 database. @Abe Miessler , the row with SID 120 does not contain duplicates. the first row for SID 120 shows 9:00AM in the datetime column , and the second row shows 10:00AM.

@Zafer, your query is the accepted answer. I made a few minor tweaks to get it to work. Thanks. Thanks due to Abe Miessler and the others for your help.

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