SQL选择具有不同日期的相同字段

时间:2012-05-23 19:37:19

标签: sql

我有一个查询,我需要运行MS SQL Server 2008的一些数据。基本上我有3个独特的字段,还有3个需要重复的时间范围。这样我将在主select语句中有三个子查询。我只是试验了一个子查询,然后复制n粘贴其他两个并更改日期范围。

这是我提出的SQL,但它给了我一个错误。我正在尝试使用子查询,因为我没有权限来创建过程。

SELECT
mon.description as ScriptName,
up0.pageseqnum as PageSeq,
usd.DisplayName as PageName,

--This subquery is the one week old data for the Response Time, Page Weight, and Number of Objects.
(SELECT
ROUND(AVG(CAST(up1.ResponseTime AS FLOAT)* 0.001),3) AS ResponseTime,
ROUND(AVG(CAST(up1.numbytes AS FLOAT)* 0.001), 3) AS NumberofKilobytes,
CONVERT(INT,AVG(up1.numobj),0) AS NumberOfObjects
FROM
table0 AS up1
WHERE
up1.Monitor_Id = up0.Monitor_id
AND up1.TestTime BETWEEN DATEADD(HOUR,4,'2012-05-14 00:00:00') AND DATEADD(HOUR,4,'2012-05-21 00:00:00')
AND ISNULL(up1.ContentMatchStatus,'0')= 0
AND up1.UserScriptStatus = 0
AND up1.TimeoutStatus = 0
AND up1.ResponseTime > 0
) AS CurrentWeek

FROM
table0 (nolock) AS up0
JOIN table1 usd ON up0.Monitor_Id=usd.monitor_id
and up0.PageSeqNum=usd.PageSeqNum
JOIN table3 mon on up0.Monitor_Id = mon.Monitor_id

WHERE
up0.Monitor_Id in (1, 2, 3, 4, 5, 6, 7, 8 ,9 ,10)

GROUP BY up0.Monitor_Id, mon.Description, up0.PageSeqNum, usd.DisplayName
ORDER BY mon.Description ASC;

我希望结果集应该包含这些列,并且我可以在本周之后再添加两个前几周的数据!请滚动看看我在说什么。

                                |           CurrentWeek (Sub Query)                  |           OneWeekOld (Sub Query)                   |
ScriptName | PageSeq | PageName | ResponseTime | NumberofKilobytes | NumberOfObjects | ResponseTime | NumberofKilobytes | NumberOfObjects |
Test1      |   0     |   Home   |      1.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |
Test1      |   1     |   Sale   |      2.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |
Test1      |   2     |   Bake   |      3.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |
Test1      |   3     |   Cake   |      4.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |
Test2      |   0     |   Home   |      1.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |
Test2      |   1     |   Sale   |      2.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |
Test2      |   2     |   Bake   |      3.2     |      50.23        |        56       |      1.2     |      50.23        |        56       |

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

SELECT  mon.description as ScriptName,
        up0.pageseqnum as PageSeq,
        usd.DisplayName as PageName,
        wk.ResponseTime,
        wk.NumberofKilobytes,
        wk.NumberOfObjects

FROM
table0 (nolock) AS up0
JOIN table1 usd ON up0.Monitor_Id=usd.monitor_id
and up0.PageSeqNum=usd.PageSeqNum
JOIN table3 mon on up0.Monitor_Id = mon.Monitor_id
CROSS APPLY (   SELECT  up1.Monitor_Id,
                        ROUND(AVG(CAST(up1.ResponseTime AS FLOAT)* 0.001),3) AS ResponseTime,
                        ROUND(AVG(CAST(up1.numbytes AS FLOAT)* 0.001), 3) AS NumberofKilobytes,
                        CONVERT(INT,AVG(up1.numobj),0) AS NumberOfObjects
                FROM table0 AS up1
                WHERE up1.TestTime BETWEEN DATEADD(HOUR,4,'2012-05-14 00:00:00') AND DATEADD(HOUR,4,'2012-05-21 00:00:00')
                AND ISNULL(up1.ContentMatchStatus,'0')= 0
                AND up1.UserScriptStatus = 0
                AND up1.TimeoutStatus = 0
                AND up1.ResponseTime > 0 
                GROUP BY up1.Monitor_Id) wk

WHERE up0.Monitor_Id in (1, 2, 3, 4, 5, 6, 7, 8 ,9 ,10)
AND up0.Monitor_Id = up1.Monitor_Id
ORDER BY mon.Description ASC;

如果您在sqlfiddle.com上发布您的架构会更容易,那么我可以针对它编写查询以返回您需要的内容。

此外我假设你使用SQL Server因此使用APPLY。请说明您正在使用的数据库系统。