我正在检查一个月的初始和最终余额,我已经提出了查询以获取其中任何一个,但我需要在同一个查询中显示这两个结果。我无法进行子查询,因为我在查询中使用GROUP BY
,据我所知,在进行子查询时我无法使用它。我很感激有经验的SQL用户提供的任何指针或建议。
DECLARE @Year INT SET @Year = 2016
DECLARE @Month varchar(4) SET @Month = '07'
SELECT T2.[Segment_0]+'-'+T2.[Segment_1]+'-'+T2.[Segment_2] Account,
T2.[AcctName],
SUM(T0.[Debit])-SUM(T0.[Credit]) Balance
FROM JDT1 T0
INNER JOIN OJDT T1
ON T1.[TransId] = T0.[TransId]
INNER JOIN oact T2
ON T2.[AcctCode] = T0.[Account]
where t0.RefDate >= @From and convert(varchar(7), T0.RefDate ,111) < LTRIM(str(@Year)) + '/' + @Month --Initial
--where t0.RefDate >= @From and convert(varchar(7), T0.RefDate ,111) <= LTRIM(str(@Year)) + '/' + @Month --Final
GROUP BY T2.[Segment_0]+'-'+T2.[Segment_1]+'-'+T2.[Segment_2],T2.[AcctName]
order by T2.[Segment_0]+'-'+T2.[Segment_1]+'-'+T2.[Segment_2]
正如您所看到的那样只有LESS THAN
和LESS OR EQUAL THAN
问题,但我在查询中需要两个结果。我缺乏经验让我对此感到震惊。
答案 0 :(得分:1)
也许这会有用..我相信如果我们对数据结构有更好的了解,我们可以提出一些可行的子查询
DECLARE @Year INT
SET @Year = 2016
DECLARE @Month VARCHAR(4)
SET @Month = '07'
SELECT T2.[Segment_0] + '-' + T2.[Segment_1] + '-' + T2.[Segment_2] Account,
T2.[AcctName],
SUM(CASE WHEN CONVERT(VARCHAR(7),T0.RefDate,111) < LTRIM(STR(@Year)) + '/' + @Month THEN T0.[Debit] END) -
SUM(CASE WHEN CONVERT(VARCHAR(7),T0.RefDate,111) < LTRIM(STR(@Year)) + '/' + @Month THEN T0.[Credit] END) AS Intitial,
SUM(CASE WHEN convert(varchar(7), T0.RefDate ,111) <= LTRIM(str(@Year)) + '/' + @Month THEN T0.[Debit] END) -
SUM(CASE WHEN convert(varchar(7), T0.RefDate ,111) <= LTRIM(str(@Year)) + '/' + @Month THEN T0.[Credit] END) AS Final
FROM JDT1 T0
INNER JOIN OJDT T1 ON T1.[TransId] = T0.[TransId]
INNER JOIN oact T2 ON T2.[AcctCode] = T0.[Account]
WHERE t0.RefDate >= @From
GROUP BY T2.[Segment_0] + '-' + T2.[Segment_1] + '-' + T2.[Segment_2],
T2.[AcctName]
ORDER BY T2.[Segment_0] + '-' + T2.[Segment_1] + '-' + T2.[Segment_2]
如果使用CTE清理一些,可能会更有意义
DECLARE @Year INT
SET @Year = 2016
DECLARE @Month VARCHAR(4)
SET @Month = '07'
DECLARE @YearMonth VARCHAR(7) = LTRIM(STR(@Year)) + '/' + @Month
;WITH cte AS
(
SELECT T2.[Segment_0] + '-' + T2.[Segment_1] + '-' + T2.[Segment_2] [Account],
T2.[AcctName],
CONVERT(VARCHAR(7),T0.RefDate,111) [RefDate],
T0.[Debit],
T0.[Credit]
FROM JDT1 T0
INNER JOIN OJDT T1 ON T1.[TransId] = T0.[TransId]
INNER JOIN oact T2 ON T2.[AcctCode] = T0.[Account]
WHERE t0.RefDate >= @From
)
SELECT Account,
AcctName,
SUM(CASE WHEN RefDate < @YearMonth THEN [Debit] END) - SUM(CASE WHEN RefDate < @YearMonth THEN [Credit] END) AS Initial,
SUM(CASE WHEN RefDate <= @YearMonth THEN [Debit] END) - SUM(CASE WHEN RefDate <= @YearMonth THEN [Credit] END) AS Final,
FROM cte
GROUP BY Account,
AcctName
ORDER BY Account
答案 1 :(得分:0)
如果您需要两者,请使用<=
。 <=
将获得<
所有的行,加上一些......所以使用<
是多余的。除了这个操作数,你的where子句是相同的。
修改强>
试试这个......
SELECT
T2.[Segment_0]+'-'+T2.[Segment_1]+'-'+T2.[Segment_2] Cuenta,
T2.[AcctName],
ISNULL((select SUM([Debit]) - SUM([Credit]) from JDT1 where TransId = T0.TransID and RefDate >= @From and convert(varchar(7), RefDate ,111) < LTRIM(str(@Year)) + '/' + @Month),0) as [Initial Saldo Final],
ISNULL((select SUM([Debit]) - SUM([Credit]) from JDT1 where TransId = T0.TransID and RefDate >= @From and convert(varchar(7), RefDate ,111) <= LTRIM(str(@Year)) + '/' + @Month),0) as [Final Saldo Final]
FROM
JDT1 T0
INNER JOIN OJDT T1 ON
T1.[TransId] = T0.[TransId]
INNER JOIN oact T2 ON
T2.[AcctCode] = T0.[Account]
order by T2.[Segment_0]+'-'+T2.[Segment_1]+'-'+T2.[Segment_2]