2013-07-15 122 views
0

我有一種複雜的問題。我們的想法是,我需要的不同Serv_Acct從一個表(這是容易的,我已經有一個):SQL Server DISTINCT或GROUP BY TEMP表

DECLARE 

@CustomerID int = 8, 
@UtilityCompanyID int = 1 


    SELECT DISTINCT SERV_ACCT, MAX(INV_DATE) 
    FROM tblAPSData 

    WHERE SERV_ACCT NOT IN (SELECT ServiceAccount 
       FROM tblMEP_Meters 
       JOIN tblMEP_Sites 
       ON tblMEP_Meters.SiteID = tblMEP_Sites.ID 

       JOIN tblMEP_Projects 
       ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID 

       WHERE CustomerID = 8 
       AND Type = 1 
       ) 

    AND ACCOUNT IN (SELECT AccountNumber 
        FROM tblMEP_CustomerAccounts 
        WHERE CustomerID = @CustomerID 
        AND UtilityCompanyID = @UtilityCompanyID) 

    AND INV_DATE > DATEADD(month, -6, getdate()) 
    GROUP BY SERV_ACCT 

當我運行此我得到16行:

SERV_ACCT (No column name) 
0289S61288 2013-06-12 00:00:00.000 
0492S90281 2013-06-12 00:00:00.000 
1303S90280 2013-06-12 00:00:00.000 
1435S01282 2013-06-12 00:00:00.000 
1440S13289 2013-06-24 00:00:00.000 
1548S00286 2013-06-12 00:00:00.000 
2498S21288 2013-06-12 00:00:00.000 
5384S92284 2013-06-24 00:00:00.000 
5538S21284 2013-06-12 00:00:00.000 
6109S12286 2013-06-12 00:00:00.000 
7358S00281 2013-06-12 00:00:00.000 
7488S22289 2013-06-12 00:00:00.000 
8058S12287 2013-06-12 00:00:00.000 
9058S00288 2013-06-12 00:00:00.000 
9168S00282 2013-06-12 00:00:00.000 
9645S21281 2013-06-12 00:00:00.000 

現在,部分我沒有得到的是,每個服務帳戶都有一個帳戶號碼,一些服務帳戶有多個帳戶號碼,我只想要一個帳戶號碼是從最近的日期開始的服務帳戶。看到這個問題時,我嘗試做這樣:

DECLARE 

@CustomerID int = 8, 
@UtilityCompanyID int = 1 


    SELECT DISTINCT SERV_ACCT, ACCOUNT, MAX(INV_DATE) 
    FROM tblAPSData 

    WHERE SERV_ACCT NOT IN (SELECT ServiceAccount 
       FROM tblMEP_Meters 
       JOIN tblMEP_Sites 
       ON tblMEP_Meters.SiteID = tblMEP_Sites.ID 

       JOIN tblMEP_Projects 
       ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID 

       WHERE CustomerID = 8 
       AND Type = 1 
       ) 

    AND ACCOUNT IN (SELECT AccountNumber 
        FROM tblMEP_CustomerAccounts 
        WHERE CustomerID = @CustomerID 
        AND UtilityCompanyID = @UtilityCompanyID) 

    AND INV_DATE > DATEADD(month, -6, getdate()) 
    GROUP BY SERV_ACCT, ACCOUNT 
    ORDER BY SERV_ACCT 

現在的結果是:

SERV_ACCT ACCOUNT (No column name) 
0289S61288 117512280 2013-06-12 00:00:00.000 
0492S90281 117512280 2013-06-12 00:00:00.000 
0492S90281 651412281 2013-04-08 00:00:00.000 
1303S90280 117512280 2013-06-12 00:00:00.000 
1435S01282 117512280 2013-06-12 00:00:00.000 
1440S13289 312937281 2013-06-24 00:00:00.000 
1548S00286 117512280 2013-06-12 00:00:00.000 
1548S00286 308710287 2013-04-08 00:00:00.000 
2498S21288 117512280 2013-06-12 00:00:00.000 
5384S92284 979437282 2013-06-24 00:00:00.000 
5538S21284 117512280 2013-06-12 00:00:00.000 
6109S12286 117512280 2013-06-12 00:00:00.000 
7358S00281 117512280 2013-06-12 00:00:00.000 
7358S00281 659710281 2013-04-08 00:00:00.000 
7488S22289 117512280 2013-06-12 00:00:00.000 
8058S12287 117512280 2013-06-12 00:00:00.000 
9058S00288 117512280 2013-06-12 00:00:00.000 
9168S00282 117512280 2013-06-12 00:00:00.000 
9168S00282 570810282 2013-04-08 00:00:00.000 
9645S21281 117512280 2013-06-12 00:00:00.000 

正如你看到的,Serv_Acct不顯着了!我的目標是隻用最新的帳戶獲得獨特的Serv_Acct(即16)。任何幫助如何做到這一點?我問了一個朋友,她告訴我,我可以用臨時表做到這一點!真的嗎?

回答

1

嘗試使用您列爲子查詢的第一個查詢,並將其加入到tblapsdata中。它會是這個樣子:

DECLARE 

@CustomerID int = 8, 
@UtilityCompanyID int = 1 

SELECT MD.SERV_ACCT, AD.ACCOUNT, MD.MAXINVDATE 
FROM 
(SELECT DISTINCT SERV_ACCT, MAX(INV_DATE) as MAXINVDATE 
FROM tblAPSData 

WHERE SERV_ACCT NOT IN (SELECT ServiceAccount 
      FROM tblMEP_Meters 
      JOIN tblMEP_Sites 
      ON tblMEP_Meters.SiteID = tblMEP_Sites.ID 

      JOIN tblMEP_Projects 
      ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID 

      WHERE CustomerID = 8 
      AND Type = 1 
      ) 

AND ACCOUNT IN (SELECT AccountNumber 
       FROM tblMEP_CustomerAccounts 
       WHERE CustomerID = @CustomerID 
       AND UtilityCompanyID = @UtilityCompanyID) 

AND INV_DATE > DATEADD(month, -6, getdate()) 
GROUP BY SERV_ACCT) MD 
join tblAPSdata AD on MD.SERV_ACCT = AD.SERV_ACCT and MD.MAXINVDATE = AD.INV_DATE 

注意如何將只能從原來的查詢返回的記錄,但將附加相應的帳號。請注意,如果您有多個具有SAME DATE的帳號,則會返回更多記錄。

+0

謝謝Mike!這正是我正在尋找的東西,你能告訴我怎麼用臨時表來做到這一點?再次感謝你。 –