mssql CASE,GROUP BY用法
更新時(shí)間:2009年02月11日 09:27:14 作者:
創(chuàng)建數(shù)據(jù)庫并利用case和group by實(shí)現(xiàn)數(shù)據(jù)庫的插入數(shù)據(jù)操作
復(fù)制代碼 代碼如下:
--create database dbTemp
use dbTemp
create table test
(
Pid int identity(1,1) not null primary key,
Years datetime,
IsFirstSixMonths int default(0), --0表示上半年1表示下半年--
TotalCome int
)
insert test
select '2007-1-1',0,50
union
select '2007-3-1',0,60
union
select '2007-12-1',1,80
union
select '2008-1-1',0,100
union
select '2008-12-1',1,100
select * from test
select convert(char(4),Years,120) as 'year',
IsFirstSixMonths=case when IsFirstSixMonths=0 then '上半年' when IsFirstSixMonths=1 then '下半年' END ,
sum(totalcome) as 'sum' from test
group by IsFirstSixMonths,convert(char(4),Years,120)
select convert(char(4),Years,120) as 'year',
IsFirstSixMonths=case when IsFirstSixMonths=0 then '上半年' ELSE '下半年' END ,
sum(totalcome) as 'sum' from test
group by IsFirstSixMonths,convert(char(4),Years,120)
--DROP DATABASE dbtemp
結(jié)果如下:
復(fù)制代碼 代碼如下:
2007 上半年 110
2007 下半年 80
2008 上半年 100
2008 下半年 100
相關(guān)文章
SQL Server實(shí)時(shí)同步更新遠(yuǎn)程數(shù)據(jù)庫遇到的問題小結(jié)
這篇文章主要介紹了SQL Server實(shí)時(shí)同步更新遠(yuǎn)程數(shù)據(jù)庫遇到的問題小結(jié),需要的朋友可以參考下2017-04-04
SQL Server使用CROSS APPLY與OUTER APPLY實(shí)現(xiàn)連接查詢
這篇文章介紹了SQL Server使用CROSS APPLY與OUTER APPLY實(shí)現(xiàn)連接查詢的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
使用SQL語句實(shí)現(xiàn)查詢排序,順序和倒序
這篇文章主要介紹了使用SQL語句實(shí)現(xiàn)查詢排序、順序和倒序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Sql?Server高版本數(shù)據(jù)庫數(shù)據(jù)備份后還原到低版本數(shù)據(jù)庫詳細(xì)步驟
不同版本SQL?Server數(shù)據(jù)庫備份還原存在問題,不能從高版本的數(shù)據(jù)庫導(dǎo)入到低版本數(shù)據(jù)中,這篇文章主要給大家介紹了關(guān)于Sql?Server高版本數(shù)據(jù)庫數(shù)據(jù)備份后還原到低版本數(shù)據(jù)庫的詳細(xì)步驟,需要的朋友可以參考下2023-10-10
更改SQL Server更改當(dāng)前數(shù)據(jù)庫的所有者:sp_changedbowner
更改SQL Server更改當(dāng)前數(shù)據(jù)庫的所有者:sp_changedbowner...2007-02-02

