是不是我的Sql脚本有问题,我里面要实现这样一个思路,统计3个值,再将这3个值加起来,展现在报表中,脚本如下: 是不是我的Sql脚本有问题,我里面要实现这样一个思路,统计3个值,再将这3个值加起来,展现在报表中,脚本如下: DECLARE @begin_date AS DATETIME DECLARE @end_date AS DATETIME DECLARE @end_date2 AS DATETIME SET @begin_date='2012-01-01' SET @end_date='2012-01-31' SET @end_date2=GETDATE() --DECLARE @end_date2 DATETIME --SET @end_date2 = GETDATE() ---所有有交易的会员 DECLARE @a_num AS INT DECLARE @b_num AS INT DECLARE @c_num AS INT SET @a_num=0; SET @b_num=0; SET @c_num=0; --所有有交易有会员资料的 SELECT @a_num = COUNT(distinct a.account_number ) from account a left join declaration d on a.account_id=d.account_id left join place on d.place_id=place.place_id and place.active_flag=1 left join admincity ad ON ad.admincity_id = place.admincity_id where 1=1 and (a.is_test_account!=1 or a.is_test_account is null) --and place.active_flag=1 and ad.used_flag=1 AND (d.is_destroyed_flag IS NULL OR is_destroyed_flag != 1) --AND d.purchase_date<@end_date2 AND CONVERT(CHAR(10),d.purchase_date,120)<CONVERT(CHAR(10),@end_date2 ,120) AND d.processed <> 'Q' ---
--有交易没有会员资料的 select @b_num = COUNT(distinct d.temp_ref_account_number) from declaration d left join place on d.place_id=place.place_id and place.active_flag=1 left join admincity ad ON ad.admincity_id = place.admincity_id where 1=1 AND d.temp_ref_account_number IS NOT NULL AND d.account_id IS NULL and place.active_flag=1 and ad.used_flag=1 AND (d.is_destroyed_flag IS NULL OR is_destroyed_flag != 1) --AND d.purchase_date<@end_date2 AND CONVERT(CHAR(10),d.purchase_date,120)<CONVERT(CHAR(10),@end_date2 ,120) AND d.processed <> 'Q'---; --有交易没有会员资料的 select @c_num = COUNT(DISTINCT t.account_code ) from transaction_import t left join place on t.place_code=place.place_code and place.active_flag=1 left join admincity ad ON ad.admincity_id = place.admincity_id where t.process_status <> 1 AND t.process_status <> 10 and t.account_code in (select account_number from card_no_gen where t.account_code=account_number) and t.account_code not in (select membershipID from testmember) --AND t.transaction_date<@end_date2; AND CONVERT(CHAR(10),t.transaction_date,120)< CONVERT(CHAR(10),@end_date2 ,120);
WITH cte AS --有交易的会员 ( SELECT '有交易的会员' AS Member_Type,(@a_num+@b_num+@c_num) AS Total,'' AS 'Member%' ) SELECT * FROM cte