SQL Server 统计报表(不断收藏中)

create table #t (out_no varchar(10) primary key,date datetime,part varchar(30),qty numeric(12,4),price numeric(12,4))
insert into #t
select 'A01','2009-1-11','B001',100,1.1 union all
select 'A02','2009-1-12','B002',200,1.3 union all
select 'A03','2009-2-22','B003',120,1.5 union all
select 'A04','2009-3-22','B004',155,1.2 union all
select 'A05','2009-4-20','B005',600,1.6 union all
select 'A06','2009-4-22','B006',750,1.6 
--
--select * from #t

select
    月份='出货数量',
    [1月]=sum(case when month(date)=1 then qty else 0 end),
    [2月]=sum(case when month(date)=2 then qty else 0 end),
    [3月]=sum(case when month(date)=3 then qty else 0 end),
    [4月]=sum(case when month(date)=4 then qty else 0 end),
    [5月]=sum(case when month(date)=5 then qty else 0 end)
from #t
union all
select
    月份='出货金额',
    [1月]=sum(case when month(date)=1 then price*qty else 0 end),
    [2月]=sum(case when month(date)=2 then price*qty else 0 end),
    [3月]=sum(case when month(date)=3 then price*qty else 0 end),
    [4月]=sum(case when month(date)=4 then price*qty else 0 end),
    [5月]=sum(case when month(date)=5 then price*qty else 0 end)
from #t

/*
月份               1月                  2月                          3月                            4月                           5月
-------- --------------------------------------- --------------------------------------- --------------------------------------- ------------------------
出货数量      300.0000           120.0000                  155.0000                 1350.0000                    0.0000
出货金额      370.0000           180.0000                  186.0000                 2160.0000                    0.0000

(2 行受影响)
*/

drop table #t



2、

假设我们有一字段名为name,其值是用逗号分隔的。

值为:'111,111xu2,1112'。

现在,我们需要编写语句搜索该name值 like '11'的。

按理说,这个name中没有11,我们要的结果就是返回空。

但是如果我们 select * from student where name like '%11%'的话,依然可以正常的查询出结果。

---

此时,我们应该采用如下的语句来实现:

 

select  *  from  student  where  name  like  ' %11% '  -- 按照我的想法是不能查到的。但结果是查到了
--
解决办法是:将sql字段名前后加上,号,并且比较值前后也加上。
--
特别注意的是:字段名加逗号时,要用字符串连接的形式,不能直接 ',name,'
select  *  from  student  where  ' , ' + name + ' , '  like  ' %,111,% '


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值