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,% '
-- 解决办法是:将sql字段名前后加上,号,并且比较值前后也加上。
-- 特别注意的是:字段名加逗号时,要用字符串连接的形式,不能直接 ',name,'
select * from student where ' , ' + name + ' , ' like ' %,111,% '