好的SQL写法
DECLARE @beginTime VARCHAR(20)= '2017-12-20 00:00:00';
DECLARE @endTime VARCHAR(20)= '2017-12-26 00:00:00';
DECLARE @country VARCHAR(20)= '';
DECLARE @OrderFrom VARCHAR(20)= '';
DECLARE @TooWeight VARCHAR(20)= '';
SELECT @beginTime + '~' + @endTime AS '日期+时间' ,
'15号仓' AS '实体仓' ,
@country AS '国家地区' ,
( SELECT COUNT(0)
FROM dbo.TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = @country
AND a.CreateTime >= @beginTime
AND a.CreateTime < @endTime
AND a.DeleteUser = 0
AND a.TooWeight = CAST(SUBSTRING(@TooWeight, 0,
CHARINDEX('[', @TooWeight)) AS INT)
AND a.OrderFrom = CAST(SUBSTRING(@OrderFrom, 0,
CHARINDEX('[', @OrderFrom)) AS INT)
) AS '一段预报单量' ,
( SELECT COUNT(0)
FROM dbo.TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = @country
AND a.ReceivedTime >= @beginTime
AND a.ReceivedTime < @endTime
AND a.DeleteUser = 0
AND a.TooWeight = CAST(SUBSTRING(@TooWeight, 0,
CHARINDEX('[', @TooWeight)) AS INT)
AND a.OrderFrom = CAST(SUBSTRING(@OrderFrom, 0,
CHARINDEX('[', @OrderFrom)) AS INT)
) AS '一段签收单量' ,
( SELECT COUNT(0)
FROM dbo.TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = @country
AND a.RejectTime >= @beginTime
AND a.RejectTime < @endTime
AND a.DeleteUser = 0
AND a.TooWeight = CAST(SUBSTRING(@TooWeight, 0,
CHARINDEX('[', @TooWeight)) AS INT)
AND a.OrderFrom = CAST(SUBSTRING(@OrderFrom, 0,
CHARINDEX('[', @OrderFrom)) AS INT)
) AS '一段签收异常单量' ,
( SELECT COUNT(0)
FROM dbo.TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = @country
AND a.DomesticWarehouseInTime >= @beginTime
AND a.DomesticWarehouseInTime < @endTime
AND a.DeleteUser = 0
AND a.TooWeight = CAST(SUBSTRING(@TooWeight, 0,
CHARINDEX('[', @TooWeight)) AS INT)
AND a.OrderFrom = CAST(SUBSTRING(@OrderFrom, 0,
CHARINDEX('[', @OrderFrom)) AS INT)
) AS '一段入库单量' ,
( SELECT COUNT(0)
FROM dbo.TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = @country
AND a.DomesticWarehouseInTime >= @beginTime
AND a.DomesticWarehouseInTime < @endTime
AND a.DeleteUser = 0
AND a.StatusFlag = 11
AND a.TooWeight = CAST(SUBSTRING(@TooWeight, 0,
CHARINDEX('[', @TooWeight)) AS INT)
AND a.OrderFrom = CAST(SUBSTRING(@OrderFrom, 0,
CHARINDEX('[', @OrderFrom)) AS INT)
) AS '一段入库异常单量';
--------------------------------------------------------------
SELECT
mailNo,
CASE
WHEN catalogName IS NULL
OR catalogName LIKE '%其他%'
OR catalogName LIKE '%用品%'
OR catalogName LIKE '%邮费%' THEN
productName
ELSE
catalogName
END catalogName,
itemName,
itemNo,
hsCode,
TRUNCATE (
temp.Weight * temp.productNum / SUM(temp.productNum),
2
) AS weight,
(
TRUNCATE (
temp.Weight * temp.productNum / SUM(temp.productNum),
2
)
) - 0.01 AS netWt,
productNum AS num,
TRUNCATE (TRUNCATE (price, 2) * productNum, 2) AS total,
TRUNCATE (price, 2) AS price,
currency,
CASE temp.unit
WHEN '千克' THEN
temp.Weight * temp.productNum / SUM(temp.productNum)
WHEN '百个' THEN
CONVERT (
temp.productNum / 100.0,
SIGNED
)
ELSE
productNum
END qty,
unit,
unit1,
CASE temp.unit1
WHEN '千克' THEN
temp.Weight * temp.productNum / SUM(temp.productNum)
WHEN '百个' THEN
CONVERT (productNum / 100.0, SIGNED)
ELSE
productNum
END qty1,
unit1 AS unit2,
CASE temp.unit1
WHEN '千克' THEN
temp.Weight * temp.productNum / SUM(temp.productNum)
WHEN '百个' THEN
CONVERT (productNum / 100.0, SIGNED)
ELSE
productNum
END qty2,
'' AS giftFlag,
'' AS note,
'' AS gnum,
'' AS orderNo,
productName
FROM
(
SELECT
b.end_mail_no AS mailNo,
IFNULL(
MAX(
substring_index(
d.item_category_name,
'-' ,- 1
)
),
MAX(d.item_name)
) AS catalogName,
e.item_name AS itemName,
e.item_no AS itemNo,
e.g_code AS hsCode,
CASE
WHEN SUM(c.weight) < 0.02 THEN
0.02
ELSE
SUM(c.weight)
END AS weight,
d.item_quantity AS quantity,
(
SUM(d.total_item_actual_price) * 0.1498
) / SUM(d.item_quantity) AS price,
'美元' AS currency,
SUM(d.item_quantity) AS productNum,
e.unit,
e.unit1,
SUM(d.item_quantity) AS qty1,
e.unit2,
SUM(d.item_quantity) AS qty2,
d.item_name AS productName
FROM
prealert_order_carriage a
LEFT JOIN prealert_end_delivery_info b ON a.id = b.order_carriage_id
LEFT JOIN prealert_package c ON a.id = c.order_carriage_id
LEFT JOIN prealert_order_item d ON c.id = d.package_id
LEFT JOIN sys_category_customs e ON d.second_category_id = e.category_id
WHERE
a.id = 284985127406145536
GROUP BY
b.end_mail_no,
e.item_name,
e.item_no,
e.g_code,
d.item_quantity,
e.unit,
e.unit1,
e.unit2,
d.item_name
) temp
GROUP BY
mailNo,
catalogName,
itemName,
itemNo,
hsCode,
quantity,
unit,
unit1
好的SQL写法的更多相关文章
- 查询分页的几种Sql写法
查询分页的几种Sql写法 摘自:http://www.cnblogs.com/zcttxs/archive/2012/04/01/2429151.html 1.创建测试环境,(插入100万条数据大概耗 ...
- MySQL查询不区分大小写的sql写法
MySQL查询不区分大小写的sql写法 mysql查询默认是不区分大小写的 如: select * from some_table where str=‘abc'; select * from som ...
- DB2、ORACLE SQL写法的主要区别
DB2.ORACLE SQL写法的主要区别 说实话,ORACLE把国内的程序员惯坏了,代码中的SQL充斥着大量ORACLE特性,几乎没人知道ANSI的标准SQL是什么样子,导致程序脱离了ORACL ...
- 字段自动递增的数据库建表的SQL写法
数据库建表的SQL写法如下: 数据库建表的SQL写法如下: create table dataC( a int identity(1,2) primary key, b varchar(20)) ...
- SQL Server中参数化SQL写法遇到parameter sniff ,导致不合理执行计划重用的一种解决方案
parameter sniff问题是重用其他参数生成的执行计划,导致当前参数采用该执行计划非最优化的现象.想必熟悉数据的同学都应该知道,产生parameter sniff最典型的问题就是使用了参数化的 ...
- sql查询一天内的where写法,sql写法
sql查询一天内的写法: 1. where createtime BETWEEN (select date_format(now(),'%Y-%m-%d 00:00:00')) and (select ...
- MySQL实现阶段累加的sql写法 ,eq:统计余额
最近项目碰到一个新的需求,统计每日充值/消费之后的余额.对于这种需求,其实也很简单,只需要在每次充值/消费后,计算下余额,然后保存下来就可以了.但是对于这种需求,一条sql就能搞定,都不需要做冗余字段 ...
- sql server中format函数的yyyyMMddHHmmssffff时间格式兼容旧版sql写法
问题:博主看到项目脚本,有些地方使用了format函数来把当前日期转换成yyyyMMddHHmmssffff的格式,但在测试环境数据库是sql 2008 r2,是不支持format这个函数的.脚本会报 ...
- 你不会还在用这8个错误的SQL写法吧?
1.LIMIT 语句 分页查询是最常用的场景之一,但也通常也是最容易出问题的地方.比如对于下面简单的语句,一般 DBA 想到的办法是在 type, name, create_time 字段上加组合索引 ...
- Mysql 实例:mysql语句练习50题(普通sql写法)
为了练习sql语句,在网上找了一些题,自己做了一遍,收益颇多.很多地方换一种思路,有更好的写法,欢迎指正. 题目地址:https://blog.csdn.net/fashion2014/article ...
随机推荐
- Dictionary——通过value找Key
Dictionary<string, string> dic = GetRoleDescriptions(); string key = dic.FirstOrDefault(x => ...
- _ 下划线 Underscores __init__
Underscores in Python https://shahriar.svbtle.com/underscores-in-python Underscores in Python This p ...
- 2014-08-28——PC端几款主流浏览器的内核
Trident(IE浏览器) Mozilla(Gecko)(熟悉的有Firefox,Flock等浏览器) WebKit(熟悉的有Safari.Chrome等浏览器) Opera(presto)(Ope ...
- 通过天天模拟器加burpsuite抓取手机app流量
通过天天模拟器,代理抓取安卓app数据包.也可以抓取https. 1.下载天天模拟器,官方下载即可,下载安装. 2.启动天天模拟器,设置代理,点击上方wlan设置图标,打开wlan设置,如下: 3.鼠 ...
- Django 进阶(分页器&中间件)
分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...
- 0402-服务注册与发现-Eureka Server使用、将服务注册到Eureka server上
一.Eureka Server使用 官方文档地址:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud. ...
- ggplot2画图时标题无法居中的问题
折腾了一天,终于好了.应该是版本的问题.在R3.2.2能居中(别问我为什么知道),在R3.3.2上就不能.解决方式如下: library(ggplot2) ggplot(data=mtcars, ae ...
- root用户无法修改文件权限(lsattr/chattr: i 和 a 属性含义)
今天想在实验室分配的服务器上添加一个普通用户, 所以用root身份登录服务器后执行useradd命令,却提示无法读写 /etc/shadow文件; ls -l /etc/shadow发现什么权限都没有 ...
- form 表单<input type="button" value="登录" onclick="loginSubmit ()"/> 点击提示 Uncaught TypeError: loginSubmit is not a function
在网上搜了一堆东东,仔细看了一下,再加上实验,发现原因出在<form>中. <form method="post"> <button type=&qu ...
- pip3命令报错Fatal error in launcher: Unable to create process using '"d:\old_files\py3.6\python.exe" "E:\py3.6\Scripts\pip3.exe" list'
cmd输入pip3 list命令报错 Fatal error in launcher: Unable to create process using '"d:\old_files\py3.6 ...