如何从同一个表中返回sql中的多行

时间:2013-05-13 21:47:31

标签: sql-server database subquery

这里很新,可以真正使用一些帮助。我有一张表

TimeID(pk), DateEntry(date), EntryTiem(time), 
ProjID(int), ProjName(varchar), Phone(bit), 
Research(bit), Notes(varchar), ProjActive(bit), 
TimeDateStamp(date).

我正在尝试将多行数据返回到每个ProjName中,但我没有运气。 这是我到目前为止所提出的......

SELECT ProjName AS 'Project Name',  
    (SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
     + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)
     FROM WorkTime
     WHERE Phone = 0 AND Research = 0 AND EntryTime IS NOT NULL
     ) AS 'Total Time No PMRE',     
    (SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2) 
     + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2) 
     FROM WorkTime
     WHERE Phone = 1 AND RESEARCH = 0 AND EntryTime IS NOT NULL
     ) AS 'Total Time Phone',
     (SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
      + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)  
      FROM WorkTime
      WHERE Phone = 0 AND RESEARCH = 1 AND EntryTime IS NOT NULL
      ) AS 'Total Time Research',
      (SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
       + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2) 
       FROM WorkTime
       WHERE EntryTime IS NOT NULL
       ) AS 'TotalTime'
FROM WorkTime
WHERE EntryTime IS NOT NULL
GROUP BY ProjName   

返回以下内容......

Project Name   Total Time No PMRE   Total Time Phone   Total Time Research   Total Time
AAAA           19:06:15             2:00:00            1:00:06               22:06:21
BBBB           19:06:15             2:00:00        1:00:06               22:06:21
CCCC           19:06:15             2:00:00        1:00:06               22:06:21
DDDD           19:06:15             2:00:00        1:00:06               22:06:21

哪个错了。每列中的金额是该条件的总和,所有ProjName加在一起而不是单独分开。

输出应该是......

Project Name   Total Time No PMRE   Total Time Phone   Total Time Research   Total Time
AAAA           00:00:19             0:00:00            0:00:00               00:00:19
BBBB           00:00:04             0:00:00        0:00:00               00:00:04
CCCC           03:00:00             2:00:00        1:00:06               06:06:06
DDDD           16:05:52             0:00:00        0:00:00               16:05:52

有没有人有任何建议或可以帮助我指出正确的方向?谢谢!

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找的是条件SUM:

SELECT ProjName AS 'Project Name',  
    SUM(CASE WHEN (Phone = 0 AND Research = 0 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END) AS 'Total Time No PMRE',
    SUM(CASE WHEN (Phone = 1 AND RESEARCH = 0 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END) AS 'Total Time Phone',
    SUM(CASE WHEN (Phone = 0 AND RESEARCH = 1 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END) AS 'Total Time Research',
    SUM(DATEDIFF(second, 0, EntryTime)) AS 'TotalTime'
FROM WorkTime
WHERE EntryTime IS NOT NULL
GROUP BY ProjName

将格式代码添加到SUM的结果中,您将没事

修改

回答您对该格式的评论,请将您的格式设为“模板”:

CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
 + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)

并替换SUM(DATEDIFF(second, 0, EntryTime)

SUM(CASE WHEN (Phone = 0 AND Research = 0 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END)的每次出现

或者,您可以在包装源查询的查询中添加格式,如下所示:

SELECT CAST([Total Time No PMRE] / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST([Total Time No PMRE] / 60 % 60 AS varchar(2)),2)
    + ':' + RIGHT('0' + CAST(([Total Time No PMRE] % 60 AS varchar(2)), 2)
... -- The other columns
FROM (
    SELECT ProjName AS 'Project Name',
        SUM(CASE WHEN (Phone = 0 AND Research = 0 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END) AS 'Total Time No PMRE',
        SUM(CASE WHEN (Phone = 1 AND RESEARCH = 0 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END) AS 'Total Time Phone',
        SUM(CASE WHEN (Phone = 0 AND RESEARCH = 1 AND EntryTime IS NOT NULL) THEN DATEDIFF(second, 0, EntryTime) ELSE NULL END) AS 'Total Time Research',
        SUM(DATEDIFF(second, 0, EntryTime)) AS 'TotalTime'
    FROM WorkTime
    WHERE EntryTime IS NOT NULL
    GROUP BY ProjName
 ) AS ds

希望这有助于你

答案 1 :(得分:0)

您需要correlate these subqueries查询外部查询中的内容,可能与您分组的内容相同:

SELECT ProjName AS 'Project Name',  
    (
        SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
        + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)
        FROM WorkTime
        WHERE Phone = 0 AND Research = 0 AND EntryTime IS NOT NULL
            AND ProjName = a.ProjName -- Correlate to outer query
    ) AS 'Total Time No PMRE',     
    (
        SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2) 
        + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2) 
        FROM WorkTime
        WHERE Phone = 1 AND RESEARCH = 0 AND EntryTime IS NOT NULL
            AND ProjName = a.ProjName -- Correlate to outer query
    ) AS 'Total Time Phone',
    (
        SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
        + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)  
        FROM WorkTime
        WHERE Phone = 0 AND RESEARCH = 1 AND EntryTime IS NOT NULL
            AND ProjName = a.ProjName -- Correlate to outer query
    ) AS 'Total Time Research',
    (
        SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
        + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2) 
        FROM WorkTime
        WHERE EntryTime IS NOT NULL
            AND ProjName = a.ProjName -- Correlate to outer query
    ) AS 'TotalTime'
FROM WorkTime a -- Add alias
WHERE EntryTime IS NOT NULL
GROUP BY ProjName