views:

247

answers:

6

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.

A: 

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.

Tomasz Kowalczyk
A: 

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?

Nix
Sum of Qtotal column grouped by year in the TDateTime column. Count of duplicate TDateTime column.
ARK
+1  A: 

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
Abe Miessler
Yes, I want it grouped by the year in the TDateTime column.
ARK
Take a look at my update
Abe Miessler
wont this include the rows that do not have duplicate rows? the result of the query should only display duplicate count, excluding the distinct rows.
ARK
Hmm ok, I think I am going to have to do some refactoring. I added a `HAVING` statement but i think it's still missing something. Stand by...
Abe Miessler
Ok, I'm confused about where you are getting the count of 4 from in your desired results? You say count duplicate tDateTime but there are 5 duplicate dates in there? Did you mean to put 5?
Abe Miessler
A: 

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
Zafer
This query does not seem to work for the situation presented above.
ARK
I've updated the code.
Zafer
A: 
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)
Conrad Frix
I think you've answered the question as stated, but I think the actual question is wrong. As this is shown, the data returned is denormalized. It's showing the same COUNT for every row, with a value that is indicative of the entire population rather than the year of the group. I think that the inner subquery needs to be changed to a correlated subquery, filtering on "year(Tdatetime)".
Chris Wuestefeld
A: 

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.

You need to mark the accepted answer as accepted.
RickF