复杂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 ...
随机推荐
- Flex移动应用程序开发的技巧和窍门(三)
这是关于 Flex 移动应用程序开发的技巧和窍门系列文章的第三部分内容.第一部分内容主要集中讨论了视图之间以及应用程序执行之间切换时的数据处理.第二部分则主要涵盖了应用程序动作条和标签组件风格化方面的 ...
- 开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台
Nuget地址:https://www.nuget.org/packages/OAuthLogin2.0/ 项目结构说明: AuthorizationProviders文件夹下主要存放内置的授权平台. ...
- puppet的配置清单书写
puppet的配置清单书写 1使用数组,合并同类的 例如你想安装很多软件,如果分开来写的话,很麻烦,不简洁,这时我们可以使用数组来完成 以前我们这样来写 class packages{ package ...
- 对lua中_ENV表的理解(lua5.2版本以后)
当我拿到_ENV表的时候,会去想这个_ENV表是干什么用的? 首先看如下代码: print(_ENV) --0x1d005f0 print(_G) --0x1d005f0 ViewCode 看了上面的 ...
- Hadoop权威指南:HDFS-数据流
Hadoop权威指南:HDFS-数据流 [TOC] 剖析文件读取 客户端通过调用FileSystem对象的open()方法来打开希望读取的文件,对于HDFS来说, 这个对象是分布式文件系统的一个实例 ...
- C#实现HttpUtility.UrlEncode输出大写字母
在c#中,HttpUtility.UrlEncode("www+mzwu+com")编码结果为www%2bmzwu%2bcom,在和Java开发的平台做对接的时候,对方用用url编 ...
- 【LeetCode题解】数组Array
1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...
- contenteditable实现可编辑的HTML标签
最近工作中遇到了一个小问题,让我学到了新的标签属性--contenteditable. 我需要实现的是手机端界面,特别简单的一个页面,如下图: 在我脑海里第一时间想到的应该就是一个form表单然后里面 ...
- jquery实现显示和隐藏toggle()方法的使用
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Extjs form 组件
1.根类 Ext.form.Basic 提供了,表单组件,字段管理,数据验证,表单提交,数据加载的功能 2.表单的容器 Ext.form.Panel 容器自动关联 Ext.form.Basic 的实例 ...