有关SQL分组的问题

时间:2010-03-19 10:29:23

标签: sql grouping

我试图在不使用子查询的情况下实现以下目的。

对于资金,我想选择最新的信函创建日期和“自创建信函后创建的最早工作清单”资金日期。

FundingId Leter (1, 1/1/2009 )(1, 5/5/2009) (1, 8/8/2009) (2, 3/3/2009) 

FundingId WorkList (1, 5/5/2009 ) (1, 9/9/2009) (1, 10/10/2009) (2, 2/2/2009) 

预期结果 -

FundingId Leter WorkList (1, 8/8/2009, 9/9/2009)

我写了一个查询如下。它有一个bug。它将省略那些最小WorkList日期小于最新Letter日期的FundingId(即使它有另一个工作清单,其中包含大于字母创建日期)。

CREATE TABLE #Funding(
[Funding_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_No] [int] NOT NULL,
CONSTRAINT [PK_Center_Center_ID] PRIMARY KEY NONCLUSTERED ([Funding_ID] ASC)
) ON [PRIMARY]

CREATE TABLE #Letter(
[Letter_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_ID] [int] NOT NULL,
[CreatedDt] [SMALLDATETIME],
CONSTRAINT [PK_Letter_Letter_ID] PRIMARY KEY NONCLUSTERED ([Letter_ID] ASC)
) ON [PRIMARY]

CREATE TABLE #WorkList(
[WorkList_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_ID] [int] NOT NULL,
[CreatedDt] [SMALLDATETIME],
CONSTRAINT [PK_WorkList_WorkList_ID] PRIMARY KEY NONCLUSTERED ([WorkList_ID] ASC)
) ON [PRIMARY]

SELECT F.Funding_ID,
Funding_No, 
MAX (L.CreatedDt),
MIN(W.CreatedDt)
FROM #Funding F
INNER JOIN #Letter L ON L.Funding_ID = F.Funding_ID
LEFT OUTER JOIN #WorkList W ON W.Funding_ID = F.Funding_ID
GROUP BY F.Funding_ID,Funding_No
HAVING MIN(W.CreatedDt) > MAX (L.CreatedDt)

如何在不使用子查询的情况下编写正确的查询?

请帮忙

由于

Lijo

2 个答案:

答案 0 :(得分:1)

您的问题是How can I write a correct query without using subquery?

但你没有使用子查询......所以你已经有了答案。

答案 1 :(得分:1)

使用派生表是最好的:

OP的表格:

CREATE TABLE #Funding(
[Funding_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_No] [int] NOT NULL,
CONSTRAINT [PK_Center_Center_ID] PRIMARY KEY NONCLUSTERED ([Funding_ID] ASC)
) ON [PRIMARY]

CREATE TABLE #Letter(
[Letter_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_ID] [int] NOT NULL,
[CreatedDt] [SMALLDATETIME],
CONSTRAINT [PK_Letter_Letter_ID] PRIMARY KEY NONCLUSTERED ([Letter_ID] ASC)
) ON [PRIMARY]

CREATE TABLE #WorkList(
[WorkList_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_ID] [int] NOT NULL,
[CreatedDt] [SMALLDATETIME],
CONSTRAINT [PK_WorkList_WorkList_ID] PRIMARY KEY NONCLUSTERED ([WorkList_ID] ASC)
) ON [PRIMARY]

OP的样本数据:

INSERT INTO #Funding (Funding_No) VALUES (1)
INSERT INTO #Funding (Funding_No) VALUES (2)

INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (1,'1/1/2009')
INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (1,'5/5/2009')
INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (1,'8/8/2009')
INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (2,'3/3/2009')

INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (1, '5/5/2009')
INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (1, '9/9/2009')
INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (1, '10/10/2009')
INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (2, '2/2/2009')

表CREATE看起来像TSQL,但没有给出版本,所以也可以使用CTE。但是,这使用派生表:

SELECT
    dt.Funding_ID,LCreatedDt,MIN(CreatedDt) AS WCreatedDt
    FROM (SELECT
              f.Funding_Id,l.LCreatedDt
              FROM #Funding    f
              LEFT OUTER JOIN (SELECT
                                   Funding_ID,MAX(CreatedDt) AS LCreatedDt
                                   FROM #Letter
                                   GROUP BY Funding_ID
                              ) l ON f.Funding_ID=l.Funding_ID
         ) dt
    LEFT OUTER JOIN #WorkList w ON dt.Funding_ID=w.Funding_ID
    WHERE w.CreatedDt>dt.LCreatedDt
    GROUP BY dt.Funding_ID,LCreatedDt

输出:

Funding_ID  LCreatedDt              WCreatedDt
----------- ----------------------- -----------------------
1           2009-08-08 00:00:00     2009-09-09 00:00:00

(1 row(s) affected)

要抢占声称我的查询使用子查询的任何人,请先阅读本文 子查询基础:http://msdn.microsoft.com/en-us/library/aa213252(SQL.80).aspx

  

子查询是SELECT查询   返回单个值并嵌套   在SELECT,INSERT,UPDATE或   DELETE语句,或在另一个语句中   子查询。可以使用子查询   任何地方都允许表达。