根据列和行计算数据

时间:2015-06-29 04:50:27

标签: sql tsql

我有一个包含以下列的表:

    Date    ReportDate  Received    Answered    Average Wait Time
5/1/2015    5/3/2015    10           10           0:00:04
5/1/2015    5/3/2015    10           10           0:00:10
5/1/2015    5/3/2015    4             4           0:00:02
5/1/2015    5/3/2015    5             5           0:00:03
5/2/2015    5/3/2015    10           10           0:00:09
5/2/2015    5/3/2015    9             9           0:00:03
5/2/2015    5/3/2015    12           12           0:00:09
5/2/2015    5/3/2015    15           15           0:00:02
5/2/2015    5/3/2015    20           20           0:00:10
Total                   95           95           0:00:07

我想计算总数并根据不同的报告日期将其存储在不同的表中,例如: -

ReportDate  TotalReceivedContacts   TotalAnsweredContacts   TotalAverageWaitTime
5/3/2015        95                       95                      0:00:07

像这样我有很多基于ReportDate的行。请帮忙。

4 个答案:

答案 0 :(得分:4)

试试这个:

SELECT ReportDate, SUM(Received) AS TotalReceivedContacts,
    SUM(Answered) AS TotalAnsweredContacts, AVG(AverageWaitTime) AS TotalAverageWaitTime
FROM Table
GROUP BY ReportDate

答案 1 :(得分:1)

SELECT SUM(Received) AS TotalReceivedContacts,SUM(Answered) AS   
TotalAnsweredContacts , AVG(WaitTime) AS TotalAverageWaitTime FROM  
YourTableName group by ReportDate 

试试这个:)

答案 2 :(得分:0)

这应该有效:

    SELECT ReportDate, SUM(Received) as ReceivedSum,
           SUM(Answered) as AnsweredSum ,AVG(WaitTime) as WaitTimeAVG
    FROM a
    GROUP BY ReportDate
    ORDER BY ReportDate

请参阅SQL Fiddle

答案 3 :(得分:0)

试试这个。

INSERT INTO another_table_name (ReportDate,TotalReceivedContacts,TotalAnsweredContacts,TotalAverageWaitTime)
SELECT ReportDate,SUM(Received) AS TotalReceivedContacts,SUM(Answered) AS        
TotalAnsweredContacts, AVG(Average Wait Time) as TotalAverageWaitTime FROM   
table_name GROUP BY ReportDate ORDER BY ReportDate

注意:" another_table_name"将是您数据库中的现有表,此查询将插入您从" table_name"中选择的所有数据。 to" another_table_name"。