2011-05-27 133 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