1

我有一个按日期范围的销售查询,其中日期范围由用户输入定义。我想按天划分结果。即:假设用户输入的日期范围为 01/01/16 - 01/15/16,我想打破每一天的结果。我正在使用 DATENAME(DD,T1.[DocDate]) 打破它,它有点工作,但结果不准确。我想我必须在 Returns 子查询中使用相同的中断。请参阅下面的完整查询:

谢谢

SELECT
'2016' as 'Year',
t4.remarks as 'Department',
DATENAME(DD,T1.[DocDate]) as 'Day',
sum(t0.[quantity])-(ISNULL(p.quantity,0)) as 'Quantity',
sum(t0.linetotal - t0.linetotal*t1.discprcnt/100)-(ISNULL(p.total,0)) as 'Total',
sum(T0.[GrssProfit])-(ISNULL(p.profit,0)) as 'Profit $',
(sum(T0.[GrssProfit])-(ISNULL(p.profit,0)))/(sum(t0.linetotal - t0.linetotal*t1.discprcnt/100)-(ISNULL(p.total,0)))*100 as 'Profit%'

FROM INV1 T0 with (nolock)
INNER JOIN OINV T1 with (nolock) on t0.docentry = t1.docnum
INNER JOIN OSLP T2 with (nolock) on t0.SlpCode = t2.SlpCode 
LEFT JOIN OHEM T3 with (nolock) on t0.slpcode = t3.SalesPrson 
LEFT JOIN OUDP T4 with (nolock)  on t3.dept = t4.Code

--BEGINS QUERY FOR THE RETURNS-- 
left join (select t9.name as 'dept',sum(t5.quantity) as 'quantity',sum(t5.linetotal - t5.linetotal*t6.discprcnt/100) as 'total',sum(t5.grssprofit) as 'profit'
from [dbo].[rin1] t5 with (nolock)
inner join orin t6 with (nolock) on t5.docentry = t6.docentry
INNER JOIN OSLP T7 with (nolock) on t5.SlpCode = t7.SlpCode 
LEFT JOIN OHEM T8 with (nolock) on t5.slpcode = t8.SalesPrson 
LEFT JOIN OUDP T9 with (nolock) on t8.dept = t9.Code
INNER JOIN OITM T10 with (nolock) on t5.itemcode = t10.itemcode
where t5.docdate between '[%1]' and '[%2]' and t10.invntitem = 'Y'
and (t5.linetotal - (t5.linetotal*t6.discprcnt/100)) <> '0'
group by t9.name) p on p.dept = t4.name
--ENDS QUERY FOR THE RETURNS-- 

WHERE t1.docdate between '[%1]' and '[%2]'
and t4.remarks is not null
and t4.remarks = 'perfume provider'
and (t0.linetotal - (t0.linetotal*t1.discprcnt/100)) <> '0'

group by DATENAME(DD,T1.[DocDate]),t4.remarks,p.quantity,p.total,p.profit
4

1 回答 1

0

您应该使用 UNION 代替您的退货子查询(使用与您的 Invoices 查询相同类型的分组依据)。您的 Group-By 很好,但就像您提到的那样,您的子查询将导致错误数据。

每当您的数据有不同的“起点”时,联盟就是您要走的路。

于 2017-01-05T19:57:08.690 回答