1: Select right(sztp,2) as sztp, sum(case when ef = 'e' and left(sztp,2) = '20' then 1 else 0 end) as 20e, sum(case when ef = 'f' and left(sztp,2) = '20' then 1 else 0 end) as 20f, sum(case when ef = 'e' and left(sztp,2) = '40' then 1 else 0 end) as 40e, sum(case when ef = 'f' and left(sztp,2) = '40' then 1 else 0 end) as 40f from 表 group by right(sztp,2)
如果sztp就20dc,40dc两种,ef就e,f两种,可以直接写:select sztp ,[20e]=sum(case when sztpef=20 and ef='e' then 1 end) ,[20f]=sum(case when sztpef=20 and ef='f' then 1 end) ,[40e]=sum(case when sztpef=40 and ef='e' then 1 end) ,[40f]=sum(case when sztpef=40 and ef='f' then 1 end) from( select sztp=right(sztp,len(sztp)-2),sztpef=left(aztp,2),ef,con from 表 ) a gropu by sztp
1--- create table ai( sztp varchar(100), ef varchar(10), con varchar(100)) insert into ai select '20dc', 'e' , 'a' insert into ai select '20dc' , 'f' , 'b' insert into ai select '40dc' , 'e' , 'c' insert into ai select '40dc' , 'f', 'd'goselect right(sztp,2) as sztp, count(case when left(sztp,2)='20' and ef='e' then ef end) as [20e], count(case when left(sztp,2)='20' and ef='f' then ef end) as [20f], count(case when left(sztp,2)='40' and ef='e' then ef end) as [40e], count(case when left(sztp,2)='40' and ef='f' then ef end) as [20f] from ai group by right(sztp,2)go2-----yyyy-mm-dd declare @a varchar(100) select @a=convert(varchar(10),getdate(),120) select @a--yyyymmdd declare @a varchar(100) select @a=convert(varchar(10),getdate(),112) select @a--yyyy/mm/dd declare @a varchar(100) select @a=convert(varchar(10),getdate(),111) select @a
按 datetime 值的 Microsoft® SQL Server™ 标准内部格式返回当前系统日期和时间。语法
GETDATE ( )返回类型
datetime注释
日期函数可用在 SELECT 语句的选择列表或用在查询的 WHERE 子句中。在设计报表时,GETDATE 函数可用于在每次生成报表时打印当前日期和时间。GETDATE 对于跟踪活动也很有用,诸如记录事务在某一帐户上发生的时间
Select right(sztp,2) as sztp,
sum(case when ef = 'e' and left(sztp,2) = '20' then 1 else 0 end) as 20e,
sum(case when ef = 'f' and left(sztp,2) = '20' then 1 else 0 end) as 20f,
sum(case when ef = 'e' and left(sztp,2) = '40' then 1 else 0 end) as 40e,
sum(case when ef = 'f' and left(sztp,2) = '40' then 1 else 0 end) as 40f
from 表 group by right(sztp,2)
select convert(char(8),getdate(),112) ----20020101
select convert(char(8),getdate(),108) ---06:05:05
101 美国 mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国 dd/mm/yy
4 104 德国 dd.mm.yy
5 105 意大利 dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10 110 美国 mm-dd-yy
11 111 日本 yy/mm/dd
12 112 ISO yymmdd
- 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
- 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM
select convert(char(8),getdate(),112) ----20020101
select convert(char(8),getdate(),108) ---06:05:05
101 美国 mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国 dd/mm/yy
4 104 德国 dd.mm.yy
5 105 意大利 dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10 110 美国 mm-dd-yy
11 111 日本 yy/mm/dd
12 112 ISO yymmdd
- 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
- 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM
select CONVERT(varchar(10), getDate(),120) --不要时间2002-1-1
select convert(char(8),getdate(),112) ----20020101
select convert(char(8),getdate(),108) ---06:05:05 101 美国 mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国 dd/mm/yy
4 104 德国 dd.mm.yy
5 105 意大利 dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10 110 美国 mm-dd-yy
11 111 日本 yy/mm/dd
12 112 ISO yymmdd
- 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
- 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM
,[20e]=sum(case when sztpef=20 and ef='e' then 1 end)
,[20f]=sum(case when sztpef=20 and ef='f' then 1 end)
,[40e]=sum(case when sztpef=40 and ef='e' then 1 end)
,[40f]=sum(case when sztpef=40 and ef='f' then 1 end)
from(
select sztp=right(sztp,len(sztp)-2),sztpef=left(aztp,2),ef,con from 表
) a gropu by sztp
declare @a varchar(100)
select @a=convert(varchar(10),getdate(),120)
select @a--yyyymmdd
declare @a varchar(100)
select @a=convert(varchar(10),getdate(),112)
select @a--yyyy/mm/dd
declare @a varchar(100)
select @a=convert(varchar(10),getdate(),111)
select @a
create table ai( sztp varchar(100), ef varchar(10), con varchar(100))
insert into ai select '20dc', 'e' , 'a'
insert into ai select '20dc' , 'f' , 'b'
insert into ai select '40dc' , 'e' , 'c'
insert into ai select '40dc' , 'f', 'd'goselect right(sztp,2) as sztp,
count(case when left(sztp,2)='20' and ef='e' then ef end) as [20e],
count(case when left(sztp,2)='20' and ef='f' then ef end) as [20f],
count(case when left(sztp,2)='40' and ef='e' then ef end) as [40e],
count(case when left(sztp,2)='40' and ef='f' then ef end) as [20f]
from ai
group by right(sztp,2)go2-----yyyy-mm-dd
declare @a varchar(100)
select @a=convert(varchar(10),getdate(),120)
select @a--yyyymmdd
declare @a varchar(100)
select @a=convert(varchar(10),getdate(),112)
select @a--yyyy/mm/dd
declare @a varchar(100)
select @a=convert(varchar(10),getdate(),111)
select @a