在Select中选择SQL

在Select中选择SQL,sql,sql-server,sql-server-2008,ssrs-2008,Sql,Sql Server,Sql Server 2008,Ssrs 2008,我正在创建一个将显示在SSRS报告中的数据集 我在一个作业中有一个查询,它在每个月的第一天滚动将计数放入表[dbo].[CountMetersDue];该值在整个月都会发生变化,因此需要在开始时拍摄快照 我已经设置了报告,它使用自定义表达式生成累积趋势图。基本上取一个值,除以另一个值算出一个百分比。因此,我有两个需要合并的查询。。。我花了很长时间才弄明白这一切 我只需要最后一点帮助 SELECT (SELECT [Count] FROM [MXPTransferDe

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

我在一个作业中有一个查询,它在每个月的第一天滚动将计数放入表
[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

这就是您要查找的查询?
调用子查询时,可以将它们括在括号“()”中


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


对不起,我没说清楚有两张不同的桌子。。。select top值是一个不会改变的静态值我猜我需要使用连接,可能是交叉连接?那么建议的查询不起作用吗?如果“选择顶部”是一个静态值,我认为我的查询是正确的……如果这对您有所帮助,您可以接受答案。如果没有,请评论您可能会有哪些错误,如果您愿意:)您好,误解的要求。。。在select top表中需要历史条目,因此2月、3月、4月等将有1个条目。我需要在select top表中返回计数,以匹配主查询中的日期…我需要将日期参数添加到子查询中,还是可以执行某种交叉联接或其他操作?我试图删除select top位和order,希望它能使用where子句中的参数,但我猜不会…顺便说一下,在一行末尾添加两个空格会导致换行。这比这一整块的文本更容易理解。你能为你的表发布完整的DDL吗?这将使帮助更容易。我认为您的桌子设计可能需要更改。您有一个表
[CountMetersDue]
,其中有一个字段
[Count]
。到期的米数不应该从查询中导出,而不是从表中读取吗?为什么需要从表中选择
TOP1
?此表中还有多少其他计数行?正如我前面所说的,发布表的DDL,因为可能有另一种方法可以获得所需的结果。重新发布查询的最新版本。我可以匹配countmeters的列名,以匹配另一个日期列,然后是否可以加入表?@jeff-如果没有表的DDL,很难知道如何加入表。您可以调整字段的数量,以仅显示此查询中涉及的字段。谢谢您的帮助,我将检查它。。。我在最初的查询中提供了一些示例数据…@jeff不用担心,我只是做了一个小的更正(括号放错了位置)。感谢您提供样本数据,您是否也可以提供样本输出?Andrew就在那里。。。一直在测试您的查询并编辑一些位,因为日期都不一样,组成员无法正常工作,因此必须使用日转换。。。我会在周二告诉你我的最终代码!谢谢你的帮助@杰夫:不用担心,当你得到最终代码后,请编辑答案以帮助其他人:-)在我将此标记为答案之前,安德鲁快速提问左外连接的原因是什么?
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)
/* 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])