复杂SQL代码实例
DECLARE @begin DATETIME,@end DATETIME,@shanghutype INT, @beginshanghuarea BIGINT ,@endshanghuarea bigint
SELECT @begin='1753-01-01',@end='9999-12-31',@shanghutype=1,@beginshanghuarea=-1,@endshanghuarea=9223372036854775807 IF OBJECT_ID('tempdb.dbo.#tmp', 'U') IS NOT NULL
DROP TABLE #tmp;
CREATE TABLE #tmp
(
SysNo BIGINT,
Source INT,
ji DECIMAL(20,6),
dai DECIMAL(20,6) ) IF @shanghutype=1
BEGIN ;WITH tmpj
AS ( SELECT
b.AdjustType ,
a.Source,
AdjustAmount = ISNULL(c.AdjustAmount, 0),
(CASE WHEN A.Source=1 THEN A.VendorSysNo ELSE A.DistributorSysNo END ) AS SysNo
FROM [BBCAccount].[dbo].[Account] a WITH ( NOLOCK )
LEFT JOIN BBCFinance.dbo.AccountAdjustReceipt b WITH ( NOLOCK ) ON a.SysNo = b.AccountSysNo
AND b.Status = 2
LEFT JOIN BBCFinance.dbo.AccountBalanceChange c WITH ( NOLOCK ) ON b.AccountSysNo = c.AccountSysNo LEFT JOIN BBCAccount.dbo.Distributor distributor WITH(NOLOCK) ON a.DistributorSysNo=distributor.SysNo
LEFT JOIN BBCAccount.dbo.Vendor vendor WITH(NOLOCK) ON A.VendorSysNo=vendor.SysNo
WHERE (a.InDate BETWEEN @begin and @end )
AND a.SOURCE=@shanghutype
AND ( a.VendorSysNo BETWEEN @beginshanghuarea AND @endshanghuarea)
)
INSERT INTO #tmp(SysNo,Source,ji,dai)
SELECT distinct
(CASE WHEN a1.Source=1 THEN a1.VendorSysNo ELSE a1.DistributorSysNo END ) AS SysNo, a1.Source,
ji = ( SELECT SUM(bb.AdjustAmount)
FROM tmpj bb
WHERE (bb.SysNo = a1.VendorSysNo OR bb.SysNo=a1.DistributorSysNo)
AND bb.AdjustType = 1
) ,
dai = ( SELECT SUM(bb.AdjustAmount)
FROM tmpj bb
WHERE (bb.SysNo = a1.VendorSysNo OR bb.SysNo=a1.DistributorSysNo)
AND bb.AdjustType = -1
) FROM [BBCAccount].[dbo].[Account] a1 WITH ( NOLOCK )
WHERE (a1.InDate BETWEEN @begin and @end )
AND a1.SOURCE=@shanghutype
AND ( a1.VendorSysNo BETWEEN @beginshanghuarea AND @endshanghuarea) END
ELSE
BEGIN
IF OBJECT_ID('tempdb.dbo.#tmp', 'U') IS NOT NULL
DROP TABLE #tmp;
;WITH tmpj
AS ( SELECT
b.AdjustType ,
a.Source,
AdjustAmount = ISNULL(c.AdjustAmount, 0),
(CASE WHEN A.Source=1 THEN A.VendorSysNo ELSE A.DistributorSysNo END ) AS SysNo
FROM [BBCAccount].[dbo].[Account] a WITH ( NOLOCK )
LEFT JOIN BBCFinance.dbo.AccountAdjustReceipt b WITH ( NOLOCK ) ON a.SysNo = b.AccountSysNo
AND b.Status = 2
LEFT JOIN BBCFinance.dbo.AccountBalanceChange c WITH ( NOLOCK ) ON b.AccountSysNo = c.AccountSysNo LEFT JOIN BBCAccount.dbo.Distributor distributor WITH(NOLOCK) ON a.DistributorSysNo=distributor.SysNo
LEFT JOIN BBCAccount.dbo.Vendor vendor WITH(NOLOCK) ON A.VendorSysNo=vendor.SysNo
WHERE (a.InDate BETWEEN @begin and @end )
AND a.SOURCE=@shanghutype
AND ( a.DistributorSysNo BETWEEN @beginshanghuarea AND @endshanghuarea)
)
INSERT INTO #tmp(SysNo,Source,ji,dai)
SELECT distinct
(CASE WHEN a1.Source=1 THEN a1.VendorSysNo ELSE a1.DistributorSysNo END ) AS SysNo, a1.Source,
ji = ( SELECT SUM(bb.AdjustAmount)
FROM tmpj bb
WHERE (bb.SysNo = a1.VendorSysNo OR bb.SysNo=a1.DistributorSysNo)
AND bb.AdjustType = 1
) ,
dai = ( SELECT SUM(bb.AdjustAmount)
FROM tmpj bb
WHERE (bb.SysNo = a1.VendorSysNo OR bb.SysNo=a1.DistributorSysNo)
AND bb.AdjustType = -1
) FROM [BBCAccount].[dbo].[Account] a1 WITH ( NOLOCK )
WHERE (a1.InDate BETWEEN @begin and @end )
AND a1.SOURCE=@shanghutype
AND ( a1.DistributorSysNo BETWEEN @beginshanghuarea AND @endshanghuarea) END IF OBJECT_ID('tempdb.dbo.#tmp2', 'U') IS NOT NULL
DROP TABLE #tmp2;
WITH TMP
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY a.SysNo ORDER BY B.INDATE DESC ) AS RowNumber ,
-- a.SysNo ,
BU.Name ,
b.AdjustedBalance,
(CASE WHEN A.Source=1 THEN A.VendorSysNo ELSE A.DistributorSysNo END ) AS SysNo
FROM [BBCAccount].[dbo].[Account] a WITH ( NOLOCK )
LEFT JOIN BBCAccount.dbo.BusinessType BU WITH ( NOLOCK ) ON a.BizTypeCode = BU.Code
AND BU.ParentCode IS NULL
AND BU.Type = 0
AND BU.IsSystem = 1
LEFT JOIN BBCFinance.dbo.AccountBalanceChange b WITH ( NOLOCK ) ON a.SysNo = b.AccountSysNo
LEFT JOIN BBCFinance.dbo.AccountAdjustReceipt c ON b.AccountSysNo = c.AccountSysNo
AND b.BizID = c.ChangeNo
LEFT JOIN BBCAccount.dbo.Distributor distributor WITH(NOLOCK) ON a.DistributorSysNo=distributor.SysNo
LEFT JOIN BBCAccount.dbo.Vendor vendor WITH(NOLOCK) ON A.VendorSysNo=vendor.SysNo
WHERE c.Status = 2
AND BU.Type = 0
AND BU.IsSystem = 1
AND BU.ParentCode IS NULL
AND BU.Type = 0
AND BU.IsSystem = 1
),
TMP1
AS ( SELECT SysNo ,
Name ,
AdjustedBalance
FROM TMP
WHERE RowNumber = 1
)
SELECT SysNo ,
Name ,
amount = SUM(AdjustedBalance)
INTO #tmp2
FROM TMP1
GROUP BY Name ,
SysNo; IF OBJECT_ID('tempdb.dbo.#tmp', 'U') IS NOT NULL
DROP TABLE #ty; ;WITH tt
AS
( SELECT
t1.SysNo,total=SUM(t2.amount)
FROM #tmp t1
LEFT JOIN #tmp2 t2 ON t1.SysNo = t2.SysNo
--WHERE
-- t1.SysNo = 2272
GROUP BY t1.SysNo ) SELECT
t1.SysNo,t1.Source,t1.ji,t1.dai,t2.Name,t2.amount,tt.total
INTO #ty
FROM #tmp t1
LEFT JOIN #tmp2 t2 ON t1.SysNo = t2.SysNo
LEFT JOIN tt
ON t1.SysNo=tt.SysNo
--WHERE t1.SysNo = 2272
ORDER BY t1.SysNo; SELECT T.*, qmye=(ye+djye+kms+bzj+fxye) FROM ( SELECT SysNo,Source,ji AS jffse,dai AS dffse,
ye=CASE WHEN Name='余额账户' THEN amount ELSE 0.00 END,
djye=CASE WHEN Name='冻结余额账户' THEN amount ELSE 0.00 END,
kms=CASE WHEN Name='跨贸税账户' THEN amount ELSE 0.00 END,
bzj=CASE WHEN Name='保证金账户' THEN amount ELSE 0.00 END,
fxye=CASE WHEN Name='供应商的分销余额' THEN amount ELSE 0.00 END
FROM #ty) AS T --DECLARE @sql_col VARCHAR(8000)
--DECLARE @sql_str VARCHAR(8000)
--DECLARE @sql_ VARCHAR(MAX)
--SELECT @sql_col = ISNULL(@sql_col + ',','') + QUOTENAME(name) FROM #ty
--SET @sql_='
--select a.*,
--jie=(select top(1) ji from #ty ty where ty.sysno=a.sysno),
--dai=(select top(1) dai from #ty ty where ty.sysno=a.sysno)
--from
--(
--select *
--from(
-- select totAL,Name,SysNo from #ty
-- )as tw
--pivot( max(totAL) for Name in('+@sql_col+') )piv
--) a
--'
--EXEC(@sql_) --SELECT TOP(1) * FROM #ty
复杂SQL代码实例的更多相关文章
- sql注入实例分析
什么是SQL注入攻击?引用百度百科的解释: sql注入_百度百科: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具 ...
- (4.22)Microsoft 管理控制台启用 SSL 加密的 SQL Server 实例
如何通过使用 Microsoft 管理控制台启用 SSL 加密的 SQL Server 实例 关键词:MSSQL加密,sql server加密,sql server客户端与服务器传输内容加密 转自:h ...
- 使用Phoenix将SQL代码移植至HBase
1.前言 HBase是云计算环境下最重要的NOSQL数据库,提供了基于Hadoop的数据存储.索引.查询,其最大的优点就是可以通过硬件的扩展从而几乎无限的扩展其存储和检索能力.但是HBase与传统的基 ...
- IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)
IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...
- C#读写Access数据库、表格datagridview窗体显示代码实例
C#读写Access数据库.表格datagridview窗体显示代码实例 最近项目中用到C#对于Access数据库表读写.mdb操作,学习了下相关的东西,这里先整理C#对于Access数据库的操作,对 ...
- MATLAB的PLOT函数线型设置及横坐标为字符串的代码实例
2.横坐标为字符串的代码实例 cell={‘PLS’,’SVM’,’RF’,’NNET’,’NB’,’PLR’,’C5.0′,’PDA’,’KNN’,’GLM’,’BCT’};%分类方法yData=[ ...
- EntityFramework 7 如何查看执行的 SQL 代码?
EF 其他版本:EntityFramework 如何查看执行的 SQL 代码? 在 EF7 中,并没有 Context.Database.Log 属性访问方式,但改变更加强大了,我们可以使用下面方式配 ...
- EntityFramework 如何查看执行的 SQL 代码?
在 VS 调试的时候,如果我们项目中使用的是 EntityFramework,查看 SQL 执行代码就不像 ADO.NET 那样直观了,我们需要设置下,可以参考下: How can I log the ...
- SQL/T-SQL实例参考
,D.[Score] B_Score ,'Distince'= CASE WHEN C.Score > D.Score THEN C.[Score] - D.[Score] WHEN C.Sco ...
随机推荐
- 在JS中使用COM组件的方法
首先创建一个COM组件,插入一个双接口Itest,在此接口上实现以下三个方法: STDMETHODIMP Ctest::test(void) //无输入输出参数 { // TODO: 在此添加实现代码 ...
- 基于Doubango的iOS客户端开源框架
一.ios-ngn-statck工程 1.Tests ---功能测试 2.底层模块(c和c++) Doubango --- 基于3GPP IMS/RCS 并能用于嵌入式和桌面系统的开源框架 1) ti ...
- 树莓派VNC搭建相关问题,啦啦啦~
为了节省money,于是我决定用VNC界面来代替显示器,为后面做C++ Qt以及Python Qt开发打下基础,我别无选择!下面开始进入正题: 1-- 下载VNC-Viewer-6.0.1-Windo ...
- 初识Dapper
16年年底开发一个项目,拍卖的项目,对于我这个不入流的程序员来说,雪微是个挑战.程序猿这个行业就是学到老用到老吧.个人比较喜欢sql原生的写法,对EF 还是不怎么感冒,EF 虽然强大,但是用起来还不怎 ...
- Git学习之路(1)-Git简介
▓▓▓▓▓▓ 大致介绍 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds ...
- LCD驱动移植在在mini2440(linux2.6.29)和FS4412(linux3.14.78)上实现对比(deep dive)
1.Linux帧缓冲子系统 帧缓冲(FrameBuffer)是Linux为显示设备提供的一个接口,用户可以将帧缓冲看成是显示内存的一种映像,将其映射到进程地址空间之后,就可以直接进行读写操作,而写操作 ...
- 详谈Struts2
介绍struts2: struts2是一个基于mvc设计模式的web层框架. 详谈struts2的执行流程: struts2的执行流程:用户发送请求---->首先经过Struts2的核心过滤器- ...
- VS Code C# 插件离线版 1.6.2
VS Code C# 插件离线版,目前最新稳定版为:1.6.2 . 由于在扩展中下载C#插件总是缓慢,或者容易出现错误,特制作离线版本共享出来. 本离线版本为Windows版本,其他系统请在扩展中下载 ...
- C++编程练习(6)----“实现简单的队列的链式存储结构“
队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出.简称链队列. 实现代码如下: /* LinkQueue.h 头文件 */ #include<iostream> #defi ...
- Spring 中使用Quartz实现任务调度
前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...