SqlServer2008全套 3 select...into 建新表,分组,排序,函数使用

第六课 数据查询与管理

select * from v_sales_shop where area_name='East'
  and outdate='20141001'
  
  --可以按门店和日期  或者只按日期(门店)分组
  select dbno_bi,SUM(nb)NB from shop_sales where dbno_bi='SE47'
  group by  dbno_bi,outdate
  
  --求平均
  SELECT AA.dbno_bi ,AVG(AA.NB) XX FROM
    (select dbno_bi,SUM(nb)NB from shop_sales where dbno_bi LIKE'SE4%'
  group by  dbno_bi)AA
  GROUP by dbno_bi
  
  --rollup 会多出一列: 算总的平均值
    SELECT AA.dbno_bi ,AVG(AA.NB) XX FROM
    (select dbno_bi,SUM(nb)NB from shop_sales where dbno_bi LIKE'SE4%'
  group by  dbno_bi)AA
  GROUP by rollup(dbno_bi)
  --会对每个门店分组 并且每个求一次总的平均值
  SELECT AA.dbno_bi ,AA.outdate,AVG(AA.NB) XX FROM
    (select dbno_bi,outdate,SUM(nb)NB from shop_sales where dbno_bi LIKE'SE4%'
  group by  dbno_bi,outdate)AA
  GROUP by rollup(dbno_bi,outdate)
  
  
    --cube 也会多出一列: 算总的平均值
    SELECT AA.dbno_bi ,AVG(AA.NB) XX FROM
    (select dbno_bi,SUM(nb)NB from shop_sales where dbno_bi LIKE'SE4%'
  group by  dbno_bi)AA
  GROUP by cube(dbno_bi)
  
  
  
    select outdate,AVG(nb)NB from shop_sales where dbno_bi LIKE'SE4%'
  group by  outdate
  
--------max

select MAX(endprice) from shop_sales where outdate>='20141101'

-----前3名
select top 3  endprice from shop_sales 
order by endprice desc

------having
  SELECT AA.dbno_bi ,AVG(AA.NB) XX FROM
    (select dbno_bi,SUM(nb)NB from shop_sales where dbno_bi LIKE'SE4%'
  group by  dbno_bi)AA
  GROUP by dbno_bi
  having AVG(AA.NB)>30
  order by dbno_bi desc
 
 --插入语句
 
 select  * from shop_sales where dbno_bi='xxx'
 insert into shop_sales values ('xxx','dd','FF','19491001',500,5,'DFFFF','DD-SS')
 insert into shop_sales (dbno_bi,class2)values ('xxx','dd')--只查两个属性的值
 
 ---insert...select
 ---选中表右键--》编写脚本--》create到 这样就创建了个结构一样的表
 insert into shop_sales1 select top 100 * from shop_sales 
 
 --- select...into 会新建一个数据表
select * 
into #X临时表呢
from shop_sales where dbno_bi='SE47'

select dbno_bi,SUM(nb)NB 
into test_top
from shop_sales where dbno_bi LIKE'SE4%'
group by dbno_bi

select * from #X临时表呢

---update
update #X临时表呢 set dbno_bi='SSSS' where nb=-1

--update...from  见截图

update #X临时表呢 set
from shop_sales  where 

---delete 表名就ok 
delete   #X临时表呢 

------TOP
select top 10 * from test_top
--返回10%
select top 10 percent * from test_top

declare @i int
set @i=20
select top (@i) percent * from test_top

declare @i int
set @i=20
select top (@i) percent * from test_top order by  NB desc

--重复的会被显示
select top 10 * from test_top order by  NB desc
select top 10  with ties  * from test_top order by  NB desc


select top 10 * from test_top order by  NB desc
--怎么显示NB不重复的...
select top 10 * from(
 SELECT NB from test_top group BY NB HAVING COUNT(1) = 1
 )AA  order by NB desc
--显示NB不重复
select top 10 NB from test_top group BY NB order by NB desc

update top (6) test_top set dbno_bi='响当当的'

-----------------compute子句 对结果集进行运算
select * from shop_sales1 where outdate='20141102'
compute sum(endprice),AVG(endprice),MAX(endprice),MIN(endprice)


------在where子句使用运算符
select * from shop_sales where
dbno_bi='SE47' AND outdate='20141102'
and endprice between 2000 and 3000

--是5的整数倍
select * from shop_sales where
dbno_bi='SE47' AND outdate='20141102'
and endprice%5=0 


---3 排序 分数和学号排序


---4 分组



--函数的使用










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值