【问题标题】:Group by then Sort (SQL Server)分组然后排序(SQL Server)
【发布时间】:2017-12-25 11:25:53
【问题描述】:

跟上之前的question

我有这个问题:

SELECT  Acc.DocTLItem.TLRef ,
        Acc.DocTLItem.Debit AS deb,
        Acc.DocTLItem.Credit AS cred,
        info.MiladiToShamsi(Acc.DocTLItem.StartDocDate) Date,
        Acc.TL.TLCode ,
        Acc.DocTLItem.DocTLHeaderRef ,
        Acc.DocTLHeader.Num
FROM    Acc.DocTLItem
        INNER JOIN Acc.TL ON Acc.DocTLItem.TLRef = Acc.TL.Id
        INNER JOIN Acc.DocTLHeader ON Acc.DocTLItem.DocTLHeaderRef = Acc.DocTLHeader.Id
        ORDER BY ( CASE WHEN debit > 0 THEN 0  ELSE 1 END ) ,
        Acc.TL.TLCode ,
        debit

结果:

 TLRef  deb cred        Date    TLCode  DocTLHeaderRef  Num
    --------------------------------------------------------------------------
    44   1  0       1396/09/12  111     16           2
    44   1  0       1396/09/21  111     18           4
    28   13 0       1396/09/11  982     15           1
    28   10 0       1396/09/19  982     17           3
    44   0  10      1396/09/19  111     17           3
    44   0  1       1396/09/21  111     18           4
    44   0  9       1396/09/11  111     15           1
    44   0  1       1396/09/12  111     16           2

如何按日期分组然后按日期排序?

我需要生成一个这样的结果集,即债务首先出现,然后在所有按日期分组之后按 TLCode 列排序。

预期结果:

  TLRef deb cred    Date    TLCode  DocTLHeaderRef  Num
    --------------------------------------------------------------------------------
    44  1    0  1396/09/12  111         16           2
    28  13   0  1396/09/11  982         15           1
    28  10   0  1396/09/19  982         17           3
    44  0    9  1396/09/11  111         15           1
    44  0    1  1396/09/12  111         16           2
    44  0    10 1396/09/19  111         17           3
    Sum 24   20             

    44  1   0   1396/09/21  111         18           4
    44  0   1   1396/09/21  111         18           4
    Sum 1   1   

【问题讨论】:

  • 预期结果是什么?
  • 此查询按日期分组?
  • 鉴于您发布的 SQL,您的 Sum 行是不可能的。编辑您的问题。

标签: sql sql-server tsql


【解决方案1】:

可能以下查询块可以帮助您: 此查询将分 4 个步骤进行:

--1. Create a temporary table that we can take as base table (#TMP)
Select *
INTO #TMP
From
(
Select 44 as TLRef, 1 as deb, 0 as cred, '1396/09/12' as Date, 111 as TLCode, 16 as DocTLHeaderRef, 2 as Num Union All
Select 44 as TLRef, 1 as deb, 0 as cred, '1396/09/21' as Date, 111 as TLCode, 18 as DocTLHeaderRef, 4 as Num Union All
Select 28 as TLRef, 13 as deb, 0 as cred, '1396/09/11' as Date, 982 as TLCode, 15 as DocTLHeaderRef, 1 as Num Union All
Select 28 as TLRef, 10 as deb, 0 as cred, '1396/09/19' as Date, 982 as TLCode, 17 as DocTLHeaderRef, 3 as Num Union All
Select 44 as TLRef, 0 as deb, 10 as cred, '1396/09/19' as Date, 111 as TLCode, 17 as DocTLHeaderRef, 3 as Num Union All
Select 44 as TLRef, 0 as deb, 1 as cred, '1396/09/21' as Date, 111 as TLCode, 18 as DocTLHeaderRef, 4 as Num Union All
Select 44 as TLRef, 0 as deb, 9 as cred, '1396/09/11' as Date, 111 as TLCode, 15 as DocTLHeaderRef, 1 as Num Union All
Select 44 as TLRef, 0 as deb, 1 as cred, '1396/09/12' as Date, 111 as TLCode, 16 as DocTLHeaderRef, 2 as Num
) X

--2. Group table by "Date" and select sum of "deb", "cred" columns and insert result in another temporary table (#TMP2)
Select null as TLRef, SUM(deb) as deb, SUM(cred) as cred, Date, null as TLCode, null as DocTLHeaderRef, null as Num 
INTO #TMP2
From #TMP 
GROUP BY Date

--3. Union both tables to resulting table gets both detail and grouped data.
Select *
From
(
    Select *, 0 as IsDetail From #TMP
    Union All
    Select *, 1 as IsDetail From #TMP2
) X
Order By Date,IsDetail

--4. Drop both temporary table
DROP TABLE #TMP
DROP TABLE #TMP2

【讨论】:

  • 需要解释吗?
  • 查询分 4 步进行: 1. 创建一个临时表,我们可以将其作为基表 (#TMP) 2. 按“日期”对表进行分组并选择“deb”、“cred”的总和列并在另一个临时表中插入结果 (#TMP2) 3. 将两个表联合到结果表获取详细信息和分组数据。 4. 删除两个临时表
  • 把它放在答案好友中:)
【解决方案2】:

你可以试试这个排序。

;WITH CTE AS (
    SELECT  Acc.DocTLItem.TLRef ,
            Acc.DocTLItem.Debit AS deb,
            Acc.DocTLItem.Credit AS cred,
            info.MiladiToShamsi(Acc.DocTLItem.StartDocDate) Date,
            Acc.TL.TLCode ,
            Acc.DocTLItem.DocTLHeaderRef ,
            Acc.DocTLHeader.Num,
            ROW_NUMBER() OVER(PARTITION BY Acc.DocTLItem.Debi, Acc.DocTLItem.Credit, Acc.TL.TLCode  ORDER BY Acc.DocTLItem.StartDocDate ) AS RN
    FROM    Acc.DocTLItem
            INNER JOIN Acc.TL ON Acc.DocTLItem.TLRef = Acc.TL.Id
            INNER JOIN Acc.DocTLHeader ON Acc.DocTLItem.DocTLHeaderRef = Acc.DocTLHeader.Id

)
SELECT * FROM CTE
ORDER BY 
    RN,
    ( CASE WHEN deb > 0 THEN 0  ELSE 1 END ) ,
    TLCode ,
    [Date],
    deb

【讨论】:

    猜你喜欢
    • 2016-10-15
    • 2018-01-29
    • 2019-07-23
    • 2012-10-04
    • 1970-01-01
    • 2021-09-03
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多