SQL在选择中选择

时间:2011-05-27 08:43:07

标签: sql sql-server sql-server-2008 ssrs-2008

我正在创建一个将在SSRS报告中显示的数据集。

我在一个工作中有一个查询,它在每个月的第1天以滚动方式将计数放入表[dbo].[CountMetersDue];价值在整个月都会发生变化,因此需要在开始时拍摄快照。

我设置了报告,该报告使用自定义表达式生成累积趋势图。基本上取一个值,除以另一个值来计算一个百分比。因此,我有两个需要结合的查询...让我的年龄让我全神贯注!

我最后一点需要帮助。

    SELECT (SELECT [Count] 
        FROM   [MXPTransferDev].[dbo].[CountMetersDue] 
        WHERE  [MXPTransferDev].[dbo].[CountMetersDue].[DateTime] = 
               [MXPTransferDev].[dbo].[Readings].[dateRead]) AS [MetersDue], 
       COUNT(readingid)                                      AS [TotalReadings], 
       CONVERT(DATE, dateread)                               AS [dateRead] 
FROM   [MXPTransferDev].[dbo].[Readings] 
WHERE  ( [MXPTransferDev].[dbo].[Readings].[dateRead] BETWEEN 
                '01-may-11' AND '31-may-11' ) 
       AND ( webcontactid IS NOT NULL ) 
       AND ( meter = 1 ) 
GROUP  BY CONVERT(DATE, [MXPTransferDev].[dbo].[Readings].[dateRead]) 

CREATE TABLE [dbo].[CountMetersDue](
    [Count] [int] NULL,
    [DateTime] [datetime] NULL
) ON [USER]

GO

ALTER TABLE [dbo].[CountMetersDue] 
ADD  CONSTRAINT [DF_CountMetersDue_DateTime]  DEFAULT (getdate()) FOR [DateTime]
GO

CREATE TABLE [dbo].[Readings](
    [readingId] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [dateRead] [datetime] NOT NULL,
    [meter] [int] NOT NULL,
    [webcontactid] [bigint] NULL,

Readings

readingId   meter   reading dateRead            webcontactid
583089  4   3662    2011-05-25 15:00:33.040         479
583207  3   682     2011-05-25 15:00:33.027         479
583088  2   98064   2011-05-25 15:00:33.007         479

CountMetersDue

Count   DateTime
2793    2011-12-01 00:00:00.000
1057    2011-05-01 14:08:20.437
610     2011-03-01 00:00:00.000

2 个答案:

答案 0 :(得分:2)

第二次回答你的问题(在答案正确之前可能需要你自己澄清一下):

/* DDL: 2 tables [CountMetersDue] & [Readings]
    [CountMetersDue]
        ([DateTime] datetime,
        [Count] int)

    [Readings]
        ([ReadingId] bigint,
        [dateRead] datetime,
        [webcontactid] bigint,
        [meter] int)

    [CountMetersDue] - contains 1 record on the first of every month, with count of the number of readings at that date
    [Readings] - contains all the individual readings

    ie: 
        [CountMetersDue]
        01-Jan-2011     1000
        01-Feb-2011     2357
        01-Mar-2011     3000

        [Readings]
        1   01-Jan-2011     11  1
        2   02-Jan-2011     12  1
        3   03-Jan-2011     13  1
        ...
*/

    SELECT
    CONVERT(DATE, [dbo].[Readings].[dateRead]) AS dateRead, 
    COUNT([dbo].[Readings].[readingId]) AS TotalReadings,
    [dbo].[CountMetersDue].[Count] AS MetersDue

FROM
    [CountMetersDue]             /* get all count meters due */
    left join [Readings]           /* get any corresponding Reading records  
                                       where the dateRead in the same month as
                                       the CountMetersDue */
        on DATEPART(year, Readings.dateRead) = DATEPART(year, [CountMetersDue].[DateTime]) /* reading in same year as CountMetersDue */
        and DATEPART(month, Readings.dateRead) = DATEPART(month, [CountMetersDue].[DateTime]) /* reading in same month as CountMetersDue */
        WHERE  ([MXPTransferDev].[dbo].[Readings].[dateRead]) BETWEEN 
               @StartDate AND @EndDate
       AND ( webcontactid IS NOT NULL ) 
       AND ( meter = 1 ) 
GROUP BY
    [dbo].[CountMetersDue].[Count],CONVERT(DATE, [dbo].[Readings].[dateRead])

答案 1 :(得分:1)

这将是您正在寻找的查询?
可以通过将子查询括在括号'()'中来包含子查询。

SELECT (SELECT [Count] FROM [xxxxx].[dbo].[CountMetersDue] AS tabA WHERE tabA.[datefield] = tabB.dateRead) AS [MetersDue], COUNT(readingId) AS [TotalReadings], CONVERT(DATE, dateRead) AS [dateRead]
FROM         [xxxxx] AS tabB
WHERE     (dateRead BETWEEN @StartDate AND @EndDate) AND (webcontactid IS NOT NULL) AND (meter = 1)
GROUP BY CONVERT(DATE, dateRead)