我使用下面的代码:根据月份和帐户名称获取数据。
SELECT
CONVERT(Date,JD.CreatedDateTime) as Dated,
DATENAME(Month,JD.CreatedDateTime) as [Month],
A.AccountName as [Account_Name],
Sum(JD.credit + JD.Debit) as Amount
FROM AccountGroup AG
INNER JOIN AccountType AT ON AG.AccountGroupID=AT.AccountGroupID
Right join Account A on A.AccountTypeID = AT.AccountTypeID
Inner join JournalMasterDetail JD on JD.AccountID = A.AccountID
where AG.AccountGroupID =4 and JD.CreatedDateTime >=DATEADD(month,-12,DATEADD(day,DATEDIFF(day,0,GETDATE()),0))
group by
A.AccountName,
JD.CreatedDateTime
但AccountName的子组无效...
我得到了这个结果:
期望的结果是:
Dated Month Account_Name Amount
2014-03-01 February Sales 910.00
2014-03-01 February Services 350.00
2014-03-06 March Sales 69:99
2014-03-06 March Cash Discount Given 44.99
答案 0 :(得分:0)
问题是您按日期时间字段进行分组,但仅显示日期部分。条目可以是相同的日期但是不同的时间。因为这些是不同的值。您需要按日期分组:
group by A.AccountName, CONVERT(Date,JD.CreatedDateTime)
或在CTE中准备查询,然后分组:
with cte as
(
SELECT CONVERT(Date,JD.CreatedDateTime) as Dated, ...
)
select Dated, ... from cte group by Dated, ...