从两个表中获取第一条记录

时间:2010-12-07 22:32:14

标签: sql

我希望从以下示例表中获取客户和项目描述的第一个日期

创建我的示例

CREATE TABLE cust
(
CUSTNO int,
ENAME varchar(50)
)

CREATE TABLE orders
(
CUSTNO int,
Description varchar(50),
ORDERDATE date
)

INSERT INTO cust VALUES (7369, 'SMITH');
INSERT INTO cust VALUES (7499, 'ALLEN');
INSERT INTO cust VALUES (7521, 'WARD');
INSERT INTO cust VALUES (7566, 'JONES');
INSERT INTO cust VALUES (7654, 'MARTIN');
INSERT INTO cust VALUES (7698, 'BLAKE');
INSERT INTO cust VALUES (7782, 'CLARK');
INSERT INTO cust VALUES (7788, 'SCOTT');
INSERT INTO cust VALUES (7839, 'KING');
INSERT INTO cust VALUES (7844, 'TURNER');
INSERT INTO cust VALUES (7876, 'ADAMS');
INSERT INTO cust VALUES (7900, 'JAMES');
INSERT INTO cust VALUES (7902, 'FORD');
INSERT INTO cust VALUES (7934, 'MILLER');

INSERT INTO orders VALUES (7782, 'Something','17-DEC-1980');
INSERT INTO orders VALUES (7782, 'Something else', '17-DEC-2000');
INSERT INTO orders VALUES (7900, 'Something', '17-DEC-1980');
INSERT INTO orders VALUES (7900, 'Something else','17-DEC-1990');
INSERT INTO orders VALUES (7934, 'Something','17-DEC-1980');

正在尝试这样的事情

  select [ENAME],[cust].[CUSTNO], MIN([ORDERDATE]),[Description]
  from [cust],[orders]
  where [cust].[CUSTNO]=[orders].[CUSTNO]
  group by [cust].[CUSTNO],[ENAME],[Description]

我的问题是这会返回太多行。我只想看到每个客户,然后在那里列出第一个日期(如果没有订单,则为空白或null)。

有什么想法吗?

5 个答案:

答案 0 :(得分:3)

 with MinOrder as (
  select
  custno, min(orderdate) orderdate
  from [orders]
  GROUP BY custno)

  select [ENAME],[cust].[CUSTNO], o.[orderdate], [Description]
   from 
  cust 
  LEFT JOIN orders o
  ON cust.custNo = o.custno
  LEFT JOiN MinOrder mo
  ON mo.orderdate = o.orderdate
    and mo.custno = o.custno

如果您不能使用CTE,则可以使用内联视图

 select [ENAME],[cust].[CUSTNO], o.[orderdate], [Description]
   from 
  cust 
  LEFT JOIN orders o
  ON cust.custNo = o.custno
  LEFT JOiN (  select
     custno, min(orderdate) orderdate
  from [orders]
  GROUP BY custno)  mo
  ON mo.orderdate = o.orderdate
    and mo.custno = o.custno

答案 1 :(得分:2)

;with cteMinOrder as (
    select [CUSTNO], MIN([ORDERDATE]) as minDate
        from [orders]
        group by [CUSTNO]
)
select [ENAME],[cust].[CUSTNO], mo.minDate, [Description]
    from [cust]
        left join cteMinOrder mo
            on [cust].[CUSTNO]=mo.[CUSTNO]
        left join [orders]
            on [cust].[CUSTNO]=[orders].[CUSTNO]
                and mo.minDate = [orders].[ORDERDATE]

答案 2 :(得分:2)

  SELECT c.[ENAME],c.[CUSTNO], o.[orderdate], o.[Description] 
   FROM  cust  c
  LEFT JOIN orders o 
    ON c.custNo = o.custno 
  LEFT JOIN 
        ( SELECT   custno, min(orderdate) orderdate 
            FROM [orders] 
            GROUP BY custno)  mo 
    ON mo.orderdate = o.orderdate 
        AND mo.custno = o.custno 

如果存在订单,您需要左联接才能获得订单,但如果没有订单,仍然可以获得客户。 @conrad Frix的解决方案也可以正常工作,但是如果您使用不接受with语句的数据库,我会这样做。

将来,您应该停止编写implict连接(使用逗号分隔表并将连接条件置于where子句中)。它们是一种糟糕的编程技术,并且导致您无法正确理解连接的原因(或者您可能已知使用左连接)。

答案 3 :(得分:1)

select [ENAME],[cust].[CUSTNO],[minorderdate],[Description]
from 
  [cust]
left join
  select [CUSTNO],MIN([ORDERDATE]) AS minorderdate
  from [orders]
  group by [CUSTNO] as t
on [cust].[CUSTNO] = [t].[CUSTNO];

语法可能需要调整,具体取决于您正在使用的实际方言,但这种关系应该达到您想要的效果。

答案 4 :(得分:-1)

只需忽略[ENAME]子句

中的[Description]GROUP BY即可