我需要一个帮助来获取记录计数,我有3个表,如下所述,并根据Transaction_Id和该事务ID从文件创建日期为基础的标题拉加入这些表下面是示例数据。
Table Names and columns and data
Entry - ID,Transaction_ID,Dfi_Acct_Nbr,Individual_Name
ID, Transaction_ID, Dfi_Acct_Nbr, Individual_Name
'1' '108' 'test_Dfi_Acct_Nbr' 'test_Name'
'2' '110' '23456789' 'Test Name'
'3' '113' '099898' 'RK'
Addenda - ID,Transaction_ID,Return_Description
ID, Transaction_ID, Return_Description
'9' '113'. 'T Desc'
'10' '108' 'test_Description'
'11' '110' '041152667'
Batch - ID,Transaction_ID,Standard_Code
ID, Transaction_ID, Standard_Code
'5' '108' ''
'6' '110' 'WEB'
Header - ID,Transaction_ID,File_Creation_Date
ID, Transaction_ID, File_Creation_Date
'15' '115' '220315'
'16' '110' '220513'
'18' '113' '220315'
'19' '108' '220514'
下面是我的疑问
SELECT COUNT(e.Transaction_ID) as TotalCount,
e.Dfi_Acct_Nbr,e.Individual_Name,a.Return_Description,h.Standard_Code
FROM ENTRY e
JOIN ADDENDA a ON e.Transaction_ID = a.Transaction_ID
JOIN BATCH h ON h.ransaction_ID = a.Transaction_ID
WHERE e.Transaction_ID in (SELECT Transaction_Id
FROM HEADER WHERE File_Creation_Date BETWEEN '220512' AND '220514')
GROUP BY e.Dfi_Acct_Nbr,e.Individual_Name, a.Return_Description, h.Standard_Code
我得到的输出如下
TotalCount, Dfi_Acct_Nbr, Individual_Name, Return_Description, Standard__Code
'1', 'test_Dfi_Acct_Nbr','test_Name', 'test_Description', ''
'1', '23456789',' 'Test Name', '041152667', 'WEB'
由于我的查询提取了2个记录,因此TotalCount应该显示为2
TotalCount, Dfi_Acct_Nbr, Individual_Name, Return_Description, Standard__Code
'2', 'test_Dfi_Acct_Nbr','test_Name', 'test_Description', ''
'2', '23456789', 'Test Name',' '041152667', 'WEB'
1条答案
按热度按时间yhxst69z1#
SQL使用
group by
,例如:GROUP BY e.区分账号,e.个人名称,a.退货说明,h.标准编码
COUNT(e.Transaction_ID) as TotalCount
在每个组内计数,而不是返回的总行数。