sql 返回日期范围内订单的收入,转换,非眩光和抛光状态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 返回日期范围内订单的收入,转换,非眩光和抛光状态相关的知识,希望对你有一定的参考价值。

-- This is almost certainly NOT helpful to other offices
-- Our method of packaging is unique within the industry from what we can tell
select
CONVERT(VARCHAR(10), orders.orderdate, 101) as 'Date',
orders.patientid as 'Pt ID',
orders.orderid as 'OrderID',
(select firstname from employee where employee.employeeid = orders.orderedbyemployeeid) as 'Sold By',
(select isnull(sum(paymentamount), 0) from payments where payments.orderid = orders.orderid and paymenttype not in ('Write-Off')) as 'Revenue',
(select case when EXISTS (select orderid from orderitems where itemid in (10181,10195) and orderitems.orderid = orders.orderid) Then 'NO' ELSE '' END) AS 'Transitions',
(select case when EXISTS (select orderid from orderitems where itemid in (10183,10194,10350) and orderitems.orderid = orders.orderid) Then 'NO' ELSE '' END) AS 'NonGlare',
(select case when EXISTS (select orderid from orderitems where itemid in (10182,10196) and orderitems.orderid = orders.orderid) Then 'NO' ELSE '' END) AS 'Polish'
from order_detail, orders
where ordertypeid in (1026) --Spectacle Lens
and orders.orderid = order_detail.orderid
and orderstatusid not in (24,26,2944,1141,69) --Excludes bad orders
and orderdate between '2016-02-01' and '2016-03-01'
and orders.orderedbyemployeeid in (select employeeid from employee where active = 'Yes')
and remake_orderid is null
order by orderdate asc
WITH OPTICAL_CTE
AS
(
select E.firstname
	,o.orderid --AS Orders																--Count orders
	,p.paymentamount
	,(select case when EXISTS 
		(select orderitems.orderid 
			from orderitems
			where itemid in (10181,10195) and orderitems.orderid = o.orderid) 
		Then '0' ELSE '1' END) AS 'Transitions'
	,(select case when EXISTS 
		(select orderitems.orderid 
			from orderitems 
			where itemid in (10183,10194,10350) and orderitems.orderid = o.orderid) 
		Then '0' ELSE '1' END) AS 'NonGlare'
from orders o
LEFT OUTER JOIN employee E on e.employeeid = o.employeeid
LEFT OUTER JOIN payments p on p.orderid = o.orderid
where CAST(o.orderdate as date) BETWEEN @DateFrom and @DateTo												--DATE OF ORDER
AND p.orderid = o.orderid
and p.paymenttype not in ('Write-Off')														--Exclude Write-Off
AND o.employeeid in 
	(select employeeid from employee where active = 'Yes' and security_group_id = '1002')	--Only ACTIVE OPTICAL employees
and o.orderstatusid not in (24,26,2944,1141,69) --Excludes bad orders
AND o.ordertypeid IN (1026)
AND o.remake_orderid IS NULL --Exclude Remakes
AND (																						--Excludes Quotes
		isNull(o.orderstatusid, 0) NOT IN (
			SELECT domainid
			FROM domain
			WHERE isQuote = 'Yes'
				AND domaintypeid = 86
			)
		))

SELECT firstname
	,SUM(paymentamount) as Total_Revenue
	,SUM(paymentamount) / COUNT(orderid) as '$ Avg'
	,SUM(CAST(Transitions as FLOAT)) AS Transitions_Qty
	,SUM(CAST(Transitions as FLOAT)) / COUNT(orderid)*100 AS '%TRANS'
	,SUM(CAST(NonGlare AS FLOAT)) AS NonGlare_Qty
	,SUM(CAST(NonGlare AS FLOAT)) / COUNT(orderid) *100 AS '%NG'
	,COUNT(orderid) AS OrderCount
FROM OPTICAL_CTE
GROUP BY firstname
WITH OPTICAL_CTE
AS
(
select E.firstname
	,o.orderid --AS Orders																--Count orders
	,p.paymentamount
	,(select case when EXISTS 
		(select orderitems.orderid 
			from orderitems
			where itemid in (10181,10195) and orderitems.orderid = o.orderid) 
		Then '0' ELSE '1' END) AS 'Transitions'
	,(select case when EXISTS 
		(select orderitems.orderid 
			from orderitems 
			where itemid in (10183,10194,10350) and orderitems.orderid = o.orderid) 
		Then '0' ELSE '1' END) AS 'NonGlare'
from orders o
LEFT OUTER JOIN employee E on e.employeeid = o.employeeid
LEFT OUTER JOIN payments p on p.orderid = o.orderid
where CAST(o.orderdate as date) BETWEEN @DateFrom and @DateTo												--DATE OF ORDER
AND p.orderid = o.orderid
and p.paymenttype not in ('Write-Off')														--Exclude Write-Off
AND o.employeeid in 
	(select employeeid from employee where active = 'Yes' and security_group_id = '1002')	--Only ACTIVE OPTICAL employees
and o.orderstatusid not in (24,26,2944,1141,69) --Excludes bad orders
AND ISNULL(insurance_plan_id,'') not in (289,488,1064,487) --Excludes Medicaid 289Medicaid 488Elements 1064EyePay 487Elements but INCLUDES no insurance via isNull
AND o.ordertypeid IN (1026) --Only F&L Orders
AND o.remake_orderid IS NULL --Exclude Remakes
AND (																						--Excludes Quotes
		isNull(o.orderstatusid, 0) NOT IN (
			SELECT domainid
			FROM domain
			WHERE isQuote = 'Yes'
				AND domaintypeid = 86
			)
		))

SELECT firstname
	,SUM(paymentamount) as Total_Revenue
	,SUM(paymentamount) / COUNT(orderid) as '$ Avg'
	,SUM(CAST(Transitions as FLOAT)) AS Transitions_Qty
	,SUM(CAST(Transitions as FLOAT)) / COUNT(orderid)*100 AS '%TRANS'
	,SUM(CAST(NonGlare AS FLOAT)) AS NonGlare_Qty
	,SUM(CAST(NonGlare AS FLOAT)) / COUNT(orderid) *100 AS '%NG'
	,COUNT(orderid) AS OrderCount
FROM OPTICAL_CTE
GROUP BY firstname

以上是关于sql 返回日期范围内订单的收入,转换,非眩光和抛光状态的主要内容,如果未能解决你的问题,请参考以下文章

SQL中项目收入的日期范围计算?

最近 24 小时的日期格式和获取结果表

检索日期范围内订单的亚马逊付款列表

MySQL 计算两个参数

SQL:如何显示给定范围内的所有日期?

mysql选择范围内的日期时间对象