尝试为同事写一个查询,但是我遇到了无限的执行问题。我有一种感觉我错过了某个地方的加入,但我无法弄清楚在哪里。我确定它是一个相对简单的问题,但是sql最不稳定而不是我的强项!
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[GET_DAILY_WEB_SALES] @Company varchar(4), @Finyear int , @ShopNum int
AS
BEGIN
SET NOCOUNT ON ;
select dv.date, dv.sty_ret_type, season, dv.sty_pdt_type, isnull(sale.salesvalue,0) SalesValue, isnull(sale.refundvalue,0) RefundValue, isnull((sale.salesvalue + sale.refundvalue),0) GrossSales, isnull(sale.salesunits,0) SalesUnits, isnull(sale.refundunits,0) RefundUnits ,
isnull((sale.salesunits + sale.refundunits),0) as TotalUnits, sale.transactions
from
(select d.date_id , d.date, dv.sty_pdt_type, dv.sty_ret_type, dv.season from cx_dates d, cx_val_ret_type, cx_styles dv
where d.finyear=@finyear ) dv left outer join
(
SELECT s.sty_ret_type,si.date_id , sum(case when si.linevalue < 0 then si.linevalue else 0 end ) AS refundvalue, sum(case when si.linevalue > 0 then si.linevalue else 0 end ) AS salesvalue,
sum(case when si.linevalue < 0 then si.linequantity else 0 end ) AS refundunits, sum(case when si.linevalue > 0 then si.linequantity else 0 end ) AS salesunits , count(distinct sales_id) as Transactions
from cx_styles s inner join
cx_sales_items si on s.style_id=si.style_id inner join
cx_dates d on si.date_id=d.date_id
where
si.shop_num=@ShopNum and
si.kpi=1
AND s.season = @Company
and d.finyear=@finyear
GROUP BY si.date_id , s.sty_ret_type ) sale
on dv.date_id=sale.date_id
and dv.sty_ret_type=sale.sty_ret_type
ORDER BY dv.date,season,dv.sty_ret_type
END
任何帮助或建议都会很棒!
答案 0 :(得分:1)
您的第一个内部选择(dv)从3个表(cx_dates,cx_val_ret_type和cx_styles)中选择,但未指定任何连接参数。