惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
罗磊的独立博客
量子位
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
M
MIT News - Artificial intelligence
月光博客
月光博客
IT之家
IT之家
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
D
Docker
The GitHub Blog
The GitHub Blog
B
Blog
V
Visual Studio Blog
博客园 - Franky
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
博客园 - 聂微东
U
Unit 42

博客园 - brake

dataview 查询 - brake - 博客园 名言名心事 一个男人写给老婆的年终总结 一个系统工程,并不是说要每个部件能力都最强、系统最好、性能最优才是最好,关键是要协调,各个部件协调匹配才是最好 成功并没有模式,每个人走的路都不一样 让学习成为一种习惯,当学习成为一种习惯时,你就会欲罢不能.想不学都不行! 解析HTML文件 - 运用SgmlReader类来解析HTML文件 asp.net實現異步調用總結 資料表欄位自动增量属性去除 Illustrator工具也能匯出XAML Code 如何讀取http地址頁面內容,http地址有可能是夸網站 - brake - 博客园 从对技术“新”、“奇”、“特”的一味追捧,到对稳定系统、成熟产品的理性认识,以及多次探讨“业务”与“技术”的关系之后,没有最好的技术,只有恰当的技术! WCF議程 WCF开发指南(自编) C# 實現關機,重啟電腦 圖片從右向左出現 ASP导出Excel数据的四种方法 C#编写Windows服务的基本过程 C# 用SQLDMO.dll 备份和恢复数据库
SQL 查询关键字用法祥解
brake · 2009-06-28 · via 博客园 - brake

1 case关键字

-----case start-------------------------------------------------------------------------------------
/*
关键字 case 
case 语法
--------1-----------------------------------------------------------------------------------------------
case     <表达式A> 
     when  <Α> then   <值A>
     when  <表达式B> then   <值B>
     when  <表达式C> then   <值C>
     else <值D>
end
----------2---------------------------------------------------------------------------------------------
case       when  <条件表达式1> then   <值A>
    when  <条件表达式2> then   <值B>
    when  <条件表达式3> then   <值C>
    else <值D>
end
*/

create table tabCase (UID int ,Areaid varchar(10),price money)
insert into tabCase select 1,    'GD',    8
 
union  select  2,    'GD',    10
 
union  select 3,    'GX',    12
 
union  select 4,    'GX',    14
 
union  select 5,    'SD',    16
 
union  select 6,    'SD',    18
 
union  select 7,    'GX',    20
 
union  select 8,    'SD',    30
 
union  select 9,    'GX',    40
 
union  select 10,    'GD',    50

select UID , Areaid , price from tabCase

select UID, case areaid when 'GD' then '广东'
         
when 'GX' then '广西'
         
when 'SD' then '山东'
        
end as AreaidName 
,price 
    
from tabCase 
----------------------------------------------------------------------------------------------------
select UID, case areaid when 'GD' then '广东'
         
when 'GX' then '广西'
         
when 'SD' then '山东'
        
end as Areaid 
,
case when price <=10 then '超低价商品'  
    
when price <=20 then '低价商品'  
    
when price <=50 then '高价商品'  
    
end as price
    
from tab 
------------------------------------------------------------------------------------------
select Areaid
,GDCount 
= sum(case when areaid='GD' then 1 else 0 end )
,GXCount 
= sum(case when areaid='GX' then 1 else 0 end )
,SDCount 
= sum(case when areaid='SD' then 1 else 0 end )
from tab group by areaid

drop table tab 
-----case End-------------------------------------------------------------------------------------

2 convert 关键字

----convert start----------------------------------------------------
--
1 日期转换并格式化
select convert(varchar,getdate(),120) , convert(varchar,getdate(),108)
--2 其它数据类型转换
select convert(int '12'),convert(varchar,125)
----convert end----------------------------------------------------

100 豎表轉橫表

drop table tb
create table tb(UserName varchar(10) , Subject varchar(10) , Score int)
insert into tb values('張三' , '語文' , 74)
insert into tb values('張三' , '數學' , 83)
insert into tb values('張三' , '物理' , 93)
insert into tb values('李四' , '語文' , 74)
insert into tb values('李四' , '數學' , 84)
insert into tb values('李四' , '物理' , 94)
select UserName,sum(case when Subject= '物理' then Score else 0 end[物理],sum(case when Subject= '語文' then Score else 0 end[語文],sum(case when Subject= '數學' then Score else 0 end[數學] from tb group by UserName
declare @sql varchar(1000)
set @sql='select UserName'
select @sql=@sql+',sum(case when Subject= ''' +Subject+ ''' then Score else 0 end) ['+Subject+']' from (select distinct Subject from tb)a
set @sql = @sql + ' from tb group by UserName'
print @sql
exec(@sql

 3 比較二個日期區(DateS1,DateE1) , (DateS2,DateE2 )間是否有交集 ,有交集返回1,否則返回0

Code


4 SQL 分頁儲存過程

Code


5 根據 資料表生成新增SQL句語

Code


6 獲得表指定列的最大值加一

Code