2011-05-27 132 views
1

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

我在一项工作中查询,该工作在每个月的第一天以滚动方式将计数放入表[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 
+0

顺便说一句,增加了两个空格的一个行的结果结束越线。这比单独的文本块更容易在眼睛上。 – Mr47 2011-05-27 09:01:16

+0

你可以发布你的表的完整DDL吗?这将使它更容易帮助。 – Tony 2011-05-27 11:24:53

+0

我认为你的餐桌设计可能需要改变。你有一个表[CountMetersDue]',它有一个字段'[Count]'。计数器的计数不应该从查询中派生而是从表中读取? 为什么你需要从表中选择'TOP 1'?此表中还有多少行数?正如我之前所说的那样,发布表格的DDL,因为可能有另一种方法来获得您需要的结果。 – Tony 2011-05-27 11:31:41

回答

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]) 
+0

感谢您的安德鲁我会检查它...我已经提供了一些示例数据在我原来的查询... – jhowe 2011-05-27 14:39:52

+0

@jeff不用担心,我只是在一个小的更正(托架在错误的地方)。感谢您提供样本数据,您是否也可以提供样本输出? – 2011-05-27 14:46:33

+0

Andrew几乎在那里......一直在测试你的查询和编辑一些比特,因为日期都不同,这个组由于工作不正常,所以不得不使用一天转换......会让你知道星期二是怎么回事,并向你展示我的最终代码!谢谢你的帮助! – jhowe 2011-05-27 15:18:49

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) 
+0

对不起,我没有说清楚有两个不同的表......选择最高值是一个静态值,不会改变我猜我需要使用连接,也许是交叉连接? – jhowe 2011-05-27 08:50:16

+0

建议的查询不起作用吗?如果'select top'是一个静态值,我会认为我的查询是正确的... – Mr47 2011-05-27 08:51:29

+0

我只是检查...几分钟... – jhowe 2011-05-27 08:56:20