SQL/T-SQL实例参考-1
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实例参考-1的更多相关文章
- SQL/T-SQL实例参考
,D.[Score] B_Score ,'Distince'= CASE WHEN C.Score > D.Score THEN C.[Score] - D.[Score] WHEN C.Sco ...
- SQL/T-SQL实例参考-2
对多关联查询,查询多中的记录,但是返回一的结果集 子查询语法 --一对多关联查询,查询多中的记录,但是返回一的结果集 SELECT C.* FROM ( SELECT A.BasicID FROM [ ...
- 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) ...
随机推荐
- Day2-Python基础2---列表、元组操作
一.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ["maqing"," peilin" ...
- HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 侯捷STL学习(三)--分配器测试
第七节:分配器测试 标准的分配器Allocator,#include<ext/...>都是拓展的 可以用不同的分配器测试同一容器 分配器allocate() & deallocat ...
- spring--AOP--日志---demo1---bai
AOP日志DEMO1: 实体类: package com.etc.entity; import org.aspectj.lang.annotation.Pointcut; public class U ...
- java 多线程系列---JUC原子类(五)之AtomicLongFieldUpdater原子类
AtomicLongFieldUpdater介绍和函数列表 AtomicLongFieldUpdater可以对指定"类的 'volatile long'类型的成员"进行原子更新.它 ...
- 问题:oracle case when;结果:Oracle CASE WHEN 用法介绍
Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...
- Android源码中添加C可执行程序
在Android源码中添加C/CPP可执行程序一般保存在external目录中 下面是每个文件的内容 ①add.c #include "add.h" int add (int a, ...
- spring bean属性及子元素使用总结
spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报 分类: Spring&SpringMVC(17) 版权声明:本文为博主原创 ...
- [Python Study Notes]行人检测
# -------------------------------------------------------------- # @文件: 行人识别.py # @工程: blog # @时间: 2 ...
- [Python Study Notes]pandas.DataFrame.plot()函数绘图
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...