SQL 语句中union all和order by同时使用
ALTER proc [dbo].[pr_get_time_limit] as select
cast(a.anjianmingcheng as varchar(100)) as anjianmingcheng,
cast(c.caiwumingcheng as varchar(100)) as caiwumingcheng,
cast(c.caiwubianhao as varchar(100)) as caiwubianhao,
case
when datediff(day,c.caoqi,getdate())> 0 then '保存超期'+cast(abs(datediff(day,c.caoqi,getdate())) as varchar(50))+'天'
else cast(abs(datediff(day,c.caoqi,getdate())) as varchar(50))+'天后保存超期'
end as state,
c.caoqi as tagtime
from SACW_CaiWu c
left join SACW_CaiWuDiaoYongJiLu as d on c.Id=d.caiwuId
left join SACW_AnJian as a on c.anjianId=a.id
where c.jiazhijine>0 and getdate()>dateadd(day,-7,c.caoqi) or c.kucunshuliang>0 and getdate()>dateadd(day,-7,c.caoqi) union all select
cast(a.anjianmingcheng as varchar(100)) as anjianmingcheng,
cast(c.caiwumingcheng as varchar(100)) as caiwumingcheng,
cast(c.caiwubianhao as varchar(100)) as caiwubianhao,
case
when datediff(day,d.yujingTime,getdate())> 0 then '归还逾期'+cast(abs(datediff(day,d.yujingTime,getdate())) as varchar(50))+'天'
else cast(abs(datediff(day,d.yujingTime,getdate())) as varchar(50))+'天后归还逾期'
end as state,
d.yujingTime as tagtime
from SACW_CaiWuDiaoYongJiLu d
left join SACW_CaiWu c on c.Id=d.caiwuId
left join SACW_AnJian as a on c.anjianId=a.id
where d.jiazhijine>0 and getdate()> dateadd(day,-7,d.yujingTime) or d.caiwushuliang>0 and getdate()>dateadd(day,-7,d.yujingTime)
ALTER proc [dbo].[pr_get_time_limit] as DECLARE @TempTime datetime
SET @TempTime = DATEADD(DAY,7,GETDATE()) SELECT
a.anjianmingcheng as [anjianmingcheng],
t.cm as [caiwumingcheng],
t.cb as [caiwubianhao],
t.tagtime as [tagtime],
case
when t.orderby = 0 then
case
when t.timeSpan > 0 then '调用逾期'+cast(t.timeSpan as varchar(50))+'天'
--when t.timeSpan = 0 then '即将逾期'
else cast(abs(t.timeSpan) as varchar(50))+'天后调用逾期'
end
else
case
when t.timeSpan > 0 then '保存超期'+cast(t.timeSpan as varchar(50))+'天'
--when t.timeSpan = 0 then '即将超期'
else cast(abs(t.timeSpan) as varchar(50))+'天后保存超期'
end
end as [state] FROM (
select
c.anjianId as aid,
cast(c.caiwumingcheng as varchar(100)) as cm,
cast(c.caiwubianhao as varchar(100)) as cb,
datediff(day,d.yujingTime,getdate()) as timeSpan,
d.yujingTime as tagtime,
0 as orderby
from SACW_CaiWuDiaoYongJiLu d
left join SACW_CaiWu c on c.Id=d.caiwuId
where (d.jiazhijine>0 or d.caiwushuliang>0) and @TempTime > d.yujingTime union all select
c.anjianId as aid,
cast(c.caiwumingcheng as varchar(100)) as cm,
cast(c.caiwubianhao as varchar(100)) as cb,
datediff(day,c.caoqi,getdate()) as timeSpan,
c.caoqi as tagtime,
1 as orderby
from SACW_CaiWu c
where (c.jiazhijine>0 or c.kucunshuliang>0) and @TempTime > c.caoqi
) as t
left join SACW_AnJian as a on t.aid=a.id
order by t.orderby,t.timeSpan
SQL 语句中union all和order by同时使用的更多相关文章
- 【转载】SQL语句中Union和Union All的区别
在使用到SQL语句进行数据库查询的过程中,如果需要求两个数据集合的并集,一般会使用到联合查询关键字Union或者Union All,其实Union和Union All两者的使用有一定差别,查出来的数据 ...
- (转载)SQL语句中Group by语句的详细介绍
转自:http://blog.163.com/yuer_d/blog/static/76761152201010203719835 SQL语句中Group by语句的详细介绍 ...
- Mybatis中动态SQL语句中的parameterType不同数据类型的用法
Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型, 此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...
- 向已写好的多行插入sql语句中添加字段和值
#region 添加支款方式--向已写好的多行插入sql语句中添加字段和值 public int A_ZhifuFS(int diqu) { ; string strData = @"SEL ...
- sql 语句中使用条件判断case then else end
sql 语句中使用条件判断case then else end范例: SELECT les.[nLessonNo] FROM BS_Lesson AS les WHERE les.[sClassCod ...
- sql语句中获取datetime任何部分
sql语句中获取datetime的日期部分 sql语句中 经常操作操作datetime类型数据.今天在写一个存储过程的时候需要将 一个datetime的值的 日期部分提取出来.网上有许多这方面的介绍. ...
- sql语句:union
sql语句中,join,left join中,是将两个或多个表横向连接,而有时,我们需要将几个表或1个表纵向连接,甚至是连接自身,就比如,某些数据库脚本特别不合理的时候,但我们又不能说啥 ....郁 ...
- SQL语句中的乘号
在ADO中,我们需要在SQL语句中使用乘法运算,可是添加'*'以后执行程序总是会出错,这是因为‘*’与sql中的‘*’关键字重合了,所以编译会出错. 解决办法:将乘法运算放到sql语句外面,将结果放入 ...
- LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods
我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...
随机推荐
- log4j写入数据库
转发自http://www.cnblogs.com/adolfmc/p/3432720.html Log4j 配置数据库连接池(将日志信息保存到数据库) org.apache.log4j.jdbc.J ...
- 基于VC的ACM音频编程接口压缩Wave音频(一)
(一)概述 音频数据一般都具有较高的采样率,经过压缩的原始数据才具有实用价值,否则不仅要占用大量存储空间而且在播放或进行网络传输时效率也是非常低下的,所以音频数字压缩编码在多媒体应用中有着广泛而又重要 ...
- Java反射机制调用private类型的构造方法
单例类: package singleton; public class SingletonTest { // 私有构造方法 private SingletonTest(){ System.out.p ...
- MSP430单片机的位操作
MSP430单片机属于RISC(Reduced Instruction Set Computer)型处理器.与普通的51单片机的复杂指令集相区别.普通51单片机执行一条指令最少需要12个时钟周期,而R ...
- 记一次troubleshooting(一):奇慢的脚本
背景: 事情发生的时间是几年前,那时刚从windows server运维的部门调动过来,对linux和数据库还是处于一知半解的状态. 领导找过来说:前任遗留下来的问题你来调查一下,有个客户说他们的日次 ...
- iBeacon行为分析
研究iBeacon也有段时间了, 总结一下这段时间对于ibeaacon行为的分析. iOS 7.0及以后的版本开始支持iBeacon. 硬件方面, iPhone4S 及以后, ipad 3代及以后, ...
- 软件开发常用快捷键 & 命令总结
HTML + JavaScript + CSS 1. 在web开发经常会遇到如: 这样的字符.它其实是Html将一些特殊字符(Html语法字符)的一种表达方式. 下面列举几个常用字符: 空 ...
- 从分布式一致性谈到CAP理论、BASE理论
问题的提出 在计算机科学领域,分布式一致性是一个相当重要且被广泛探索与论证问题,首先来看三种业务场景. 1.火车站售票 假如说我们的终端用户是一位经常坐火车的旅行家,通常他是去车站的售票处购买车票,然 ...
- JavaScript思维导图—DOM基本操作
JavaScript思维导图-来自@王子墨http://julying.com/blog/the-features-of-javascript-language-summary-maps/ DOM基本 ...
- 安卓手动新增SDK
一:下载SDK 比如,我正要使用sdk5.0,于是我随便找了一个源下载:http://download.csdn.net/detail/dan_seek/8216801 二:整合进eclipse 解压 ...