`
pcajax
  • 浏览: 2110552 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Sql server之常用三表联查举例

 
阅读更多

1.创建表

 

CREATE TABLE [dbo].[Customer](

    [CustomerID] [int] NULL,

    [CustomerName] [nvarchar](50) NULL

) ON [PRIMARY]

 

CREATE TABLE [dbo].[Customer_Order](

    [CustomerID] [int] NULL,

    [OrderID] [int] NULL,

    [Data] [datetime] NULL

) ON [PRIMARY]

 

CREATE TABLE [dbo].[OrderID](

    [OrderID] [int] NULL,

    [Amount] [int] NULL

) ON [PRIMARY]

 

 

2.无订单的用户

select customerName

from customer C

left join Customer_Order CO

on c.customerid=co.customerid

where ISNULL(CO.customerid,0)!=0

group by c.customername

 

3.每个人的订单成交量

select customername,isnull(SUM(Amount),0) as amounttotal from customer c

left join customer_order co on c.customerid=co.customerid

left join orderid o on co.orderid=o.orderid

group by customername

order by amounttotal

 

 

4.2009年谁成交的订单大于7000

select customername,isnull(SUM(Amount),0) as amounttotal,YEAR(co.data) as yearDate from customer c

left join customer_order co on c.customerid=co.customerid and YEAR(co.data)=2009

left join orderid o on co.orderid=o.orderid

group by customername,YEAR(co.data)

having isnull(SUM(Amount),0) >7000

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics