我有一个包含销售变动的表。我需要编写一份报告查询,以比较今天与去年同一天的销售额,以及两次查询之间的不同情况。 随附的是书面询问的图片,但我无法将其合并
当分支数相等时,我需要第二次查询的结果与第一次查询相同
DECLARE @LastChangeDate as date
DECLARE @LastChangeDate2 as date
SET @LastChangeDate = GETDATE()
set @LastChangeDate2 = DATEADD(YEAR, -1, GETDATE())
select transdate As 'Date' ,
DATENAME(dw,GETDATE()) as 'Day' ,
dit.NAME as 'Store',
stot.TARGET as 'Target',
sum ( NETAMOUNT ) *-1 As 'Sales ' ,
sum ( NETAMOUNT ) *-1 / stot.TARGET As 'Diff -T',
( CAST(SUM(NETAMOUNT) * - 1 AS DECIMAL(18,2)) / count(DISTINCT RECEIPTID) ) As 'AVG. Receipt'
from RETAILTRANSACTIONSALESTRANS as trans
inner join RETAILCHANNELTABLE rt on trans.STORE = rt.STORENUMBER inner join DIRPARTYTABLE dit on rt.OMOPERATINGUNITID = dit.RECID
inner join StoreTargets stot on trans.STORE = stot.STORENUMBER
where TRANSDATE = @LastChangeDate and trans.RECEIPTID != '' and trans.TRANSACTIONSTATUS = 0 and trans.DATAAREAID = 'KHPH' and trans.TRANSTIME > 28000
group by trans.TRANSDATE , dit.NAME ,stot.TARGET
order by [Sales ]
select
sum ( NETAMOUNT ) *-1 As 'Sales 2' ,
( CAST(SUM(NETAMOUNT) * - 1 AS DECIMAL(18,2)) / count(DISTINCT RECEIPTID) ) As 'AVG. Receipt2'
from RETAILTRANSACTIONSALESTRANS as trans2
where TRANSDATE = @LastChangeDate2 and trans2.RECEIPTID != '' and trans2.TRANSACTIONSTATUS = 2 and trans2.DATAAREAID = 'KHPH' and trans2.TRANSTIME > 28000
group by trans2.STORE