2017-03-09 123 views
1

我有兩個表,銷售和電話如下:組通過日期

**Sales** 
CUST_ID   INT primary key, 
CUST_NM   Varchar(40), 
Sale_date   Datetime2, 
SALES    Money); 
CUST_ID CUST_NM   Sale_date    SALES 
1   Dom  2015-01-01 15:00:02.3000000 10.00 
2   Brian 2015-01-02 15:00:02.3000000 12.00 
3   Stu  2015-01-03 15:00:02.3000000 21.00 
4   John  2015-01-04 15:00:02.3000000 41.00 
5   Jack  2015-01-05 15:00:02.3000000 51.00 
6   Jill  2015-01-05 15:00:02.3000000 61.00 
7   Steve 2015-01-04 15:00:02.3000000 16.00 
8   Stacey 2015-01-03 15:00:02.3000000 19.00 
9   Lacey 2015-01-03 15:00:02.3000000 30.00 

呼叫

NAME    Varchar(40), 
CALL_DATE   Date, 
TOTAL_CALLS  INT 


NAME  CALL_DATE TOTAL_CALLS 
Dom  2015-01-01 2 
Brian  2015-01-02 4 
Stu  2015-01-03 3 
John  2015-01-04 5 
Jack  2015-01-05 6 
Jill  2015-01-05 10 
Steve  2015-01-04 8 
Stacey  2015-01-03 7 
Lacey  2015-01-03 9 

我想寫的SELECT語句帶回日期,銷售總額,以及來自銷售和呼叫的全部呼叫,並在日期加入。

這是我寫的,我認爲它應該是正確的,但不知何故,我沒有得到正確的輸出。

select Calls.CALL_DATE, sum(Sales.SALES) as gross_sale, sum(Calls.TOTAL_CALLS) as gross_total_calls 
from Sales 
join 
Calls 
on convert (date,sales.Sale_date)=calls.CALL_DATE 
group by Calls.CALL_DATE 
order by Calls.CALL_DATE 

我得到的輸出是

CALL_DATE gross_sale gross_total_calls 

2015-01-01 10.00  2 
2015-01-02 12.00  4 
2015-01-03 210.00  57 
2015-01-04 114.00  26 
2015-01-05 224.00  32 

我要去哪裏錯了?

+0

感謝您編輯問題@john Cappelletti – Gompu

回答

1

您正在生成每天的笛卡爾積。您需要在join之前進行彙總。或者,你可以用union all和聚集做到這一點:

select dte, sum(sales) as sales, sum(calls) as total_calls 
from ((select cast(s.sale_date as date) as dte, sales, 0 as calls 
     from sales s 
    ) union all 
     (select call_date, 0, total_calls as calls 
     from calls c 
    ) 
    ) sc 
group by dte 
order by dte; 
+0

嗨戈登,感謝您的答覆。我收到錯誤「無效的列名'TOTAL_CALLS'。」對於該行:「select dte,sum(SALES)as gross_sales,sum(TOTAL_CALLS)as gross_total_calls」 – Gompu

+0

@Gompu。 。 。在'union all'的查詢* second *中使用別名不起作用。我修復了查詢。 –

0

由戈登使用表變量來創建可運行測試腳本建議的替代方法。 請注意額外的兩行數據和允許所有數據返回的FULL OUTER JOIN。

declare @Sales table (CUST_ID INT primary key, CUST_NM Varchar(40), Sale_date Datetime2,SALES Money); 
insert into @Sales (CUST_ID, CUST_NM, Sale_date, SALES) 
select 1, 'Dom', '2015-01-01 15:00:02.3000000', 10.00 union 
select 2, 'Brian', '2015-01-02 15:00:02.3000000', 12.00 union 
select 3, 'Stu', '2015-01-03 15:00:02.3000000', 21.00 union 
select 4, 'John', '2015-01-04 15:00:02.3000000', 41.00 union 
select 5, 'Jack', '2015-01-05 15:00:02.3000000', 51.00 union 
select 6, 'Jill', '2015-01-05 15:00:02.3000000', 61.00 union 
select 7, 'Steve', '2015-01-04 15:00:02.3000000', 16.00 union 
select 8, 'Stacey', '2015-01-03 15:00:02.3000000', 19.00 union 
select 9, 'Lacey', '2015-01-03 15:00:02.3000000', 30.00 union 
select 10, 'Tom', '2015-01-07 15:00:02.3000000', 1.00 

declare @Calls table (NAME Varchar(40), CALL_DATE Date, TOTAL_CALLS INT) 
insert into @Calls (NAME, CALL_DATE, TOTAL_CALLS) 
select 'Dom', '2015-01-01', 2 union 
select 'Brian', '2015-01-02', 4 union 
select 'Stu', '2015-01-03', 3 union 
select 'John', '2015-01-04', 5 union 
select 'Jack', '2015-01-05', 6 union 
select 'Jill', '2015-01-05', 10 union 
select 'Steve', '2015-01-04', 8 union 
select 'Stacey', '2015-01-03', 7 union 
select 'Lacey', '2015-01-03', 9 union 
select 'Tom', '2015-01-06', 1 

select * from @Sales 
select * from @Calls 

select ISNULL (a.CALL_DATE, b.CALL_DATE) as CALL_DATE, gross_sale, TOTAL_CALLS 
from 
(select convert(date, Sale_date) as CALL_DATE, sum(SALES) as gross_sale 
    from @Sales 
    group by convert(date, Sale_date) 
) a 
full outer join 
(select CALL_DATE, SUM(TOTAL_CALLS) as TOTAL_CALLS 
    from @Calls 
    group by CALL_DATE 
) b on a.CALL_DATE = b.CALL_DATE 
order by a.CALL_DATE 
+0

很酷。感謝T D Potts。這工作! – Gompu