SQL/T-SQL实例参考
CASE
,D.[Score] B_Score
,'Distince'=
CASE
WHEN C.Score > D.Score THEN C.[Score] - D.[Score]
WHEN C.Score < D.Score THEN D.[Score] - C.[Score]
ELSE 0
END
FROM [tbiz_AssScoreAction] C
INNER JOIN [tbiz_AssScoreAction] D
ON C.QuestionID =D.QuestionID AND (C.RelationID -30) = D.RelationID
WHERE C.RelationID=30 AND C.ProjectID=65 AND C.PersonID=2430
--等价语法
,D.[Score] - C.Score Ta_Zi
,CASE
WHEN C.[Score] > D.[Score] THEN C.[Score] - D.[Score]
WHEN C.[Score] < D.[Score] THEN D.[Score] - C.[Score]
ELSE 0
END AS Distince
CASE 判断 NULL的情况
CASE WHEN [ReadState] IS NULL OR [ReadState]=0 THEN '未读' ELSE '已读' END AS ReadStateName
SELECT CASE Type_No
WHEN 2 THEN Customer_No
WHEN 4 THEN Customer_No
WHEN 10 THEN Customer_No
WHEN 11 THEN Customer_No
WHEN 3 THEN Organization_Name
WHEN 5 THEN Organization_Name
END AS Customer_No
FROM [Lx_Finance_Transfer_ForNC]
SELECT
A.TESTID,
A.SCALEID,
B.NAME,
B.COUNT,
B.ASSTIME,
A.STATE,
CASE
WHEN A.MATCH IS NULL THEN
0 ELSE CAST (A.MATCH AS NUMERIC ( 26, 2 ))
END AS MATCH
FROM
TD_TEST_INFO A
JOIN ASS_SCALE B ON A.SCALEID= B.SCALEID
JOIN
SELECT T.Id,T.pingZhengNum FROM Lx_Finance_Transfer_ForNC T
LEFT JOIN Lx_Finance_Flow_ForNC F ON T.Id=F.PingZhengId
LEFT JOIN
解读:尽管主档表T只有1条记录,但是在分档表中有2条记录与之关联,所以SELECT结果集有2条.
SELECT T.Id T_Id,T.pingZhengNum,F.* FROM Lx_Finance_Transfer_ForNC T
INNER JOIN Lx_Finance_Flow_ForNC F ON T.Id=F.PingZhengId
-- 效果和LEFT JOIN 一样
RIGHT JOIN
解读:T表从RIGHT链接F表,SQL引擎会保留F表中所有记录
自连接
SELECT A.* ,
B.RelationName ,
B.Score T_Score ,
B.ID T_ID
FROM [tbiz_AssScoreWeidu] A
INNER JOIN [tbiz_AssScoreWeidu] B
ON A.WeiduNo = B.WeiduNo AND A.RelationID = B.RelationID + 30
WHERE A.ProjectID = 65
AND A.PersonID = 2430
AND ( A.RelationID = 30 OR B.RelationID = 0 )
AND A.WeiduNo = 'C211-13'
自链接+WHERE条件
SELECT A.DictItemID ,
B.ItemName ParentItemName ,
A.ItemType ,
A.ItemName ,
A.ItemCode ,
A.CreatedBy ,
A.IsDelete
FROM tcfg_DictItem A
LEFT JOIN tcfg_DictItem B ON B.DictItemID = A.ParentID
WHERE A.ParentID != 0
一对多关系JOIN一行
SELECT *
FROM (
SELECT A.Id
,A.PingZhengNum
,A.CRT_Date
,A.[Enabled]
,B.Id FlowId
,B.Type_No_Name
,B.VouchType
,ROW_NUMBER() OVER
(
PARTITION BY A.Id
ORDER BY B.CRT_Date
)
AS rn
FROM [Lx_Finance_Transfer_ForNC] A LEFT OUTER JOIN dbo.Lx_Finance_Flow_ForNC B
ON A.Id=B.PingZhengId
) m
WHERE rn=1
解读:主档表记录多,分档表记录少,参考
方案二
SELECT A.Id
,A.PingZhengNum
,A.CRT_Date
,A.[Enabled]
,B.Id FlowId
,B.Type_No_Name
,B.VouchType
FROM [Lx_Finance_Transfer_ForNC] A
OUTER APPLY
(
SELECT TOP 1 *
FROM Lx_Finance_Flow_ForNC C
WHERE PingZhengId = A.Id
ORDER BY CRT_Date DESC
) B
ROW_NUMBER() OVER(....) AS Xxx
SELECT [PersonID] ,
ROW_NUMBER() OVER
(
ORDER BY A.Id
) AS RowID
FROM [tbiz_AssScore] A
简单的说row_number()从1开始,为每一条"分组"记录返回一个数字,分组对应关键词PARTITION BY XXX,为可选关键词
当出现GROUP BY 子句时,GROUP BY会影响到ROW_NUMBER()中的字段,如下面的第5行
SELECT [PersonID] ,
AVG([Score]) AVG_Score,
ROW_NUMBER() OVER
(
ORDER BY A.PersonID
) AS RowID
FROM [tbiz_AssScore] A
GROUP BY PersonID
ORDER BY AVG_Score
PARTITION BY 子句 + ROW_NUMBER() OVER(....) AS rn
PARTITION BY 理解成"分组"
SELECT * FROM(
SELECT C.[ID]
,D.ID BID
,C.[ProjectID]
,C.[PersonID]
,C.[QuestionID]
,C.[QuestionName]
,C.[WeiduNo]
,C.[WeiduNAME]
,C.[RelationID]
,C.[RelationName]
,C.[Score]
,D.RelationName B_RelationName
,D.[Score] B_Score
,D.[Score] - C.Score Ta_Zi
,ROW_NUMBER() OVER
(
PARTITION BY D.ID,C.[ProjectID],C.[PersonID],C.[QuestionID]
ORDER BY C.[ID]
)
AS rn
FROM [tbiz_AssScoreAction] C
INNER JOIN [tbiz_AssScoreAction] D
ON C.QuestionID = D.QuestionID AND (C.RelationID -30) = D.RelationID
WHERE C.RelationID=30 AND C.ProjectID=65 AND C.PersonID=2430
) m WHERE rn = 1 ORDER BY Ta_Zi
DECLARE
DECLARE @MedicalInstitutionID VARCHAR(max) SET @MedicalInstitutionID='SYS20130228000000012'
DECLARE @ResidentCardID VARCHAR(max) SET @ResidentCardID=NULL
DECLARE @Name VARCHAR(max) SET @Name=NULL
DECLARE @InDateLeft DATETIME SET @InDateLeft=NULL
DECLARE @InDateRight DATETIME SET @InDateRight=NULL
DECLARE @AcceptsOperatorID VARCHAR(max) SET @AcceptsOperatorID=NULL
DECLARE @AcceptsOperatorName VARCHAR(max) SET @AcceptsOperatorName=NULL
DECLARE @IsPrint INT SET @IsPrint=null
SQL/T-SQL实例参考的更多相关文章
- SQL/T-SQL实例参考-2
对多关联查询,查询多中的记录,但是返回一的结果集 子查询语法 --一对多关联查询,查询多中的记录,但是返回一的结果集 SELECT C.* FROM ( SELECT A.BasicID FROM [ ...
- SQL/T-SQL实例参考-1
CASE ,D.[Score] B_Score ,'Distince'= CASE WHEN C.Score > D.Score THEN C.[Score] - D.[Score] WHEN ...
- SQL Server的实例恢复解析
同Oracle一样,SQL Server在非一致性关闭的时候也会进行实例恢复(Instance Recovery),本文根据stack overflow的文章介绍一些SQL Server实例恢复的知识 ...
- SQL群集多实例卸载、安装
安装SQL多实例群集: 准备工作:准备SQL群集管理员及服务账号:sqladmin和srv-sql,sqladmin和srv-sql都属于群集节点计算机的administrators组 预留群集名称账 ...
- SQL Server 多实例下的复制
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 搭建步骤(Procedure) 注意事项(Attention) 二.背景(Contexts) ...
- 在线API,桌面版,jquery,css,Android中文开发文档,JScript,SQL掌用实例
学习帮助文档大全 jquery,css,Android中文开发文档,JScript,SQL掌用实例 http://api.jq-school.com/
- 当SQL Server的实例位于集群的特定节点时,数据库无法远程访问
搭建好了一个集群环境,发现当SQL Server的实例位于集群的其中一个节点时,数据库无法远程访问,报如下错误.但在另一个 节点时,数据库访问正常. 标题: 连接到服务器 -------------- ...
- Delphi调用SQL分页存储过程实例
Delphi调用SQL分页存储过程实例 (-- ::)转载▼ 标签: it 分类: Delphi相关 //-----下面是一个支持任意表的 SQL SERVER2000分页存储过程 //----分页存 ...
- Step7:SQL Server 多实例下的复制
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 搭建步骤(Procedure) 注意事项(Attention) 二.背景(Contexts) ...
随机推荐
- Android5.0资源 colorAccent,colorPrimary,colorPrimaryDark
- 【swift学习笔记】一.页面转跳的条件判断和传值
在转跳页面之前我们可能会判断一下是否有输入正确的数据,如下图所示,如果没有数据输入则弹出提示,不进行转跳. 一:页面转跳 1.第一个页面的上放上一个Label一个textfield和一个按钮 2.在第 ...
- 备忘:spring jdbc事务代码 mybatis, nhibernate
http://files.cnblogs.com/files/mikelij/mymavenMar1.rar
- Spring IOC容器创建对象的方式
一.无参构造函数创建 我们用Spring创建Stu ...
- Pyhont-Urllib2
Urllib2 相当于的Urllib 的升级版 但又不能代替 Urllib 这个我得新手很费解呢...这个问题留着把,,等以后成大牛的时候在回来想想这儿问题!! Urllib2 常用 // 1 设置 ...
- Asp.Net MVC<四>:路由器
路由的核心类型基本定义于System.Web.dll中,路由机制同样可应用与Web Forms,实现请求地址和物理文件的分离. web form中使用路由器的示例 路由配置 protected voi ...
- Python笔记(1)变量与表达式
列表list list是用的最多的类型 可以count计数 可嵌套,多钟类型并存 支持 + * a = [1,2,3] a_ref = a a_copy = a[:] 引用,a变化a_ref也变化 指 ...
- 经典KMP算法C++与Java实现代码
前言: KMP算法是一种字符串匹配算法,由Knuth,Morris和Pratt同时发现(简称KMP算法).KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.比 ...
- perl 模块安装
You can check if you have them installed in your machine with: > perl -e 1 -M<module> It wi ...
- redis配置详解
##redis配置详解 # Redis configuration file example. # # Note that in order to read the configuration fil ...