我正在编写一份报告,总计按天分组的一组特定交易,但总数不正确。在下面列出的代码中,没有生成错误,但每日总数减少了11。
SELECT
'1,*'+char(13)+char(10)
+'80,1006062'+char(13)+char(10)
+'100,10'+char(13)+char(10)
+'2405,'+cast(sum(trans.QUANTITY) as varchar(18))+char(13)+char(10) --Census events --as varchar(10)
+'9999,'+cast(count(distinct trans.TX_ID) as varchar(18))+char(13)+char(10) --Count of Records (for analysis/ validation only)
+'2420,'+format(trans.SERVICE_DATE,'M/d/yyyy') --as service date of Observation Procedure
FROM PAT_ENC_HSP hsp
inner join HSP_TRANSACTIONS trans on hsp.HSP_ACCOUNT_ID=trans.HSP_ACCOUNT_ID
WHERE TRANS.TX_TYPE_HA_C = '1' AND-- Billed procedures
datediff(day,trans.SERVICE_DATE,cast(CURRENT_TIMESTAMP as date)) between 7 and 7 AND
PROC_ID in ('90068','94788','94790','94792','94794','10240')
group by format(trans.SERVICE_DATE,'M/d/yyyy')
order by format(trans.SERVICE_DATE,'M/d/yyyy')
这会产生结果......
1,*80,1006062 100,10 2405,305.000 9999,90 2420,4/25/2016
我通过将此查询的结果导出到Excel中来仔细检查了我的总计,Excel也在4月25日生成了90条记录。但是,总数量是“294”而不是“305”
SELECT DISTINCT TRANS.TX_TYPE_HA_C, TRANS.PROCEDURE_DESC, TRANS.PROC_ID, TRANS.DEPARTMENT, TRANS.QUANTITY, TRANS.SERVICE_DATE, TRANS.TX_ID, TRANS.TX_POST_DATE
FROM PAT_ENC_HSP HSP LEFT OUTER JOIN HSP_TRANSACTIONS TRANS on
HSP.HSP_ACCOUNT_ID=TRANS.HSP_ACCOUNT_ID
WHERE PROC_ID in ('90068','94788','94790','94792','94794','10240')
AND TRANS.TX_TYPE_HA_C = '1'
AND TRANS.SERVICE_DATE ={ts '2016-04-25 00:00:00'}
ORDER BY TRANS.TX_ID
我不知道哪些是正确的,我在Crystal Reports中重新创建了相同的查询,并再次收到294值。不幸的是,我必须使用初始查询中的格式将格式化结果上传到另一个应用程序。我不确定为什么所有三种方法的总值都不一样,我假设我在“cast(sum”语句中做错了。