Attempting to get a query written for a colleague, however im running into an infinite execution issue. I have a feeling ive missed a join somewhere but i cant figure out where. Im sure its a relatively simple issue, but sql is most definetley not my forte!

 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

any help or suggestions would be fantastic!

有帮助吗?

解决方案

Your first inner select (dv) selects from 3 tables (cx_dates, cx_val_ret_type and cx_styles) but does not specify any joining parameters.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top