0

我正在检查一个月的初始和最终余额,我想出了一个查询来获取它们中的任何一个,但我需要在同一个查询中显示这两个结果。我不能做子查询,因为我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 THANLESS OR EQUAL THAN但我的查询中需要这两个结果。我缺乏经验让我对此感到头疼。

4

2 回答 2

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
于 2016-08-10T20:49:22.333 回答
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]
于 2016-08-10T20:35:56.307 回答