T-SQL编程

  子查询
1。select ... where 列或运算式 比较运算运算【any|all](子查询)
   只要主查询中列或运算式与子查询所得结果中任一(any)或全部(all)数据符合比较条件的话则主查询的结果为我们要的数据

       选出不同的人金额最高的订单
    select *  from  sales a
    where tomat=(select max(totmat) from sales  where name=a.name)
    
    select sale_id,tot_amt
    from sales 
    where tot_amt>any(select tot_amt from sales where sale_id='e0013'and 'order_date='1996/11/10')
   
2。select ...where 列或运算式[not] in (子查询)
    只要主查询中列或运算式是在(不在)子查询所得结果列表中的话,则主查询的结果为我们要的数据
    select sales_id,tot_amt
    from sales 
    where sale _id  in(select sale_id from employee where sex='F')
3.select ...where [not] exists (  子查询)
  子查询的结果至少存在一条数据时,则主查询的结果为我们要的数据。(exists)或自查询的结果找不到数据时,则主查询的结果为我们要的数据(not exists)
 我们经常查询的两个表有多少重复的记录就用这个
 以下范例让你找出滞销的产品,也就是尚未有任何销售记录的库存产品。此范例主要是查询以库文件中的每一条产品代码到销售明细表中去查询,如果查询不到任何一条,表示该产品未曾卖出任何一件。
  select * from stock a
  where not exists(select * from sale_item b
                          where a.prod_id=b.prod_id and a.stup_id=b.stup_id)

 select emp_no,emp_name from employee
 from employee a

 where (select sum(tot_amt) from sales b where a.sale_id=b.emp_no)<200000


union (all)
利用union 将多个select 的结果结合起来.union命令将来自不同select命令所生成的数据混合在一起,并将重复的数据删除仅留下一条,也就是数学上的并集关系。
如果你想让重复的数据也留下时,可以使用union all取代原来的union命令

1.select prod_id ,sup_id 
  from sale_item
  where order_no=10001
  union
  select prod_id,sup_id
  from purchase 
  where pur_no=50001
   order by 1,2
2.在sqlserver方面可以用case实现的,但在access里,只能用iif或union
  select a.rmpwr,a.buypwr,yd-case when b.dl>0 then a.yd+b.rmpwr-b.bdl 
  else a.yd-b.yd end
 from (select mid,rmpwr,buypwr,(buypwr-rmpwr)as yd from main where month(dt)=2)a,(select mid,rmpwr,buypwr,(buypwr-rmpwr)as yd,bdl from main where month(dt)=1 b where a.mid=b.mid
 在本机数据库中a,b同上
 select a.ydl+b.rmpwr-b.ydl as yd  from a,b where a.mid=b.mid and b.bdl>0
 union all
select a.yd-b.yd from a,b where a.mid=b.mid and isnull(b.bdl,-1)<=0 

数据分组
1。选前几条数据
  
   select top 10 orderid,amt=unitprice*quantity
   from ordredetails 
   order by amt desc
2.利用group by 子句对select命令所选数据分组。分组后可以显示出来
  group by是除了不含汇总函数(sum,avg,count,min,max等以外的列)
  select prod_id,sum(qty*unit_price) tot_amt
 from sale_item
 group by prod_id
 order by tot_amt desc
3.group by 子句还可以加 with cube语句,加上with cube语句,其返回的数据除了和没有with cube语句一样外,,另外它会得到一些加总列。
  select sup_id,prod_id ,sum(qty*unit_price),sum(qty) tot_amt
  from sale_item
  groupby sup_id,prod_id with cube
  将有对应每个sup_id的加总列,每个prod_id 的加总列,和null,null全部的加总列
4.with cube语句换成with rollup ,则只返回最高层次的group列(最靠近group by的列的加总)
   上面的例子,只返回sup_id的加总列,每个sup_id后有个加总列
5。使用grouping区分空值    
cube操作生成的空值null带来一个问题,即如何区分cube操作生成的null值和实际数据中返回的null值。这个问题,可以用grouping函数解决。如果列中的值来自事实数据,则grouping函数返回0,如果列中的值是cube操作所生成的null,则返回1。在cube操作中,所生成的null代表全体值。可将select语句写成使用grouping函数将null替换为字符串all。
因为事实数据中的null表名数据值未知,所以select语句还可以用字符串unknow替代来自事实数据的null。
      select productid,grouping(productid),orderid,grouping(orderid),sum(quantity)
     from order
    group by productid,orderid
    with cube
    order by productid,orderid
6.group by   子句经常会和having子句搭配,用来找出每个组别中满足指定条件的数据,以下命令由产品文件中找出代码重复者,也就是同一型产品有两家以上的供应商。
  select prod_id,count(*) from stock
   group by prod_id
   having count(*)>1
   order by prod_id



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值