根据SQL中的特定单元格值重复一行

时间:2018-01-01 07:20:57

标签: sql sql-server sql-server-2014

我有一张这样的桌子。

year   month    TenDays   TotalPerTenDays
------------------------------------------
96     9         3         12
96     10        1         15
96     10        3         22
96     11        1         2
96     11        2         10
96     11        3         1
96     12        1         13

我正在尝试使用此结果进行查询,但是......

year   month    TenDays   TotalPerTenDays
------------------------------------------
96     9         1         0
96     9         2         0
96     9         3         12
96     10        1         15
96     10        2         0
96     10        3         22
96     11        1         2
96     11        2         10
96     11        3         1
96     12        1         13
96     12        2         0
96     12        3         0

当前查询

SELECT dbo.DateTable.Year,
       dbo.DateTable.Month,
       dbo.DateTable.TenDays,
       Sum(dbo.ChequeItemTreasurer.ChequeTreasurer) AS TrTotalMonth
FROM   dbo.DateTable
       LEFT OUTER JOIN dbo.ChequeItemTreasurer
                    ON dbo.DateTable.ShamsiDateLong = dbo.ChequeItemTreasurer.ChequeDateTreasurer
GROUP  BY dbo.DateTable.Year,
          dbo.DateTable.Month,
          dbo.DateTable.TenDays
ORDER  BY dbo.DateTable.Year,
          dbo.DateTable.Month,
          dbo.DateTable.TenDays 

请你帮帮我吗? (SQL 2014) 感谢

3 个答案:

答案 0 :(得分:4)

像这样的东西

SELECT a.year,
       b.month,
       tc.TenDays,
       COALESCE(b.TotalPerTenDays, 0)
FROM   (SELECT DISTINCT year,month
        FROM   yourtable) a
       CROSS JOIN (VALUES (1),(2),(3)) tc (TenDays)
       LEFT JOIN yourtable b
              ON a.year = b.year
                 AND a.month = b.month
                 AND tc.TenDays = b.TenDays 

您的原始查询应转换为类似

的内容
SELECT dt.Year,
       dt.Month,
       tc.TenDays,
       Sum(ct.ChequeTreasurer) AS TrTotalMonth
FROM   (select distinct Year, Month, ShamsiDateLong from dbo.DateTable) dt
       CROSS JOIN (VALUES (1),(2),(3)) tc (TenDays)
       LEFT OUTER JOIN dbo.ChequeItemTreasurer ct
                    ON dt.ShamsiDateLong = ct.ChequeDateTreasurer
GROUP  BY dt.Year,
          dt.Month,
          tc.TenDays
ORDER  BY dt.Year,
          dt.Month,
          tc.TenDays 

开始使用alias名称,它使查询更具可读性。

答案 1 :(得分:2)

使用数字/计数表。

; with 
qry as
(
    -- your existing query
    SELECT  dbo.DateTable.Year, 
        dbo.DateTable.Month, 
        dbo.DateTable.TenDays, 
        SUM(dbo.ChequeItemTreasurer.ChequeTreasurer) AS TrTotalMonth 
    FROM    dbo.DateTable 
        LEFT OUTER JOIN dbo.ChequeItemTreasurer     
                     ON dbo.DateTable.ShamsiDateLong = dbo.ChequeItemTreasurer.ChequeDateTreasurer 
    GROUP BY dbo.DateTable.Year, dbo.DateTable.Month, dbo.DateTable.TenDays 
    ORDER BY dbo.DateTable.Year, dbo.DateTable.Month, dbo.DateTable.TenDays
)
select  q.Year, q.Month, TenDays, TrTotalMonth
from    qry q

union all

select  q.Year, q.Month, n.n as TenDays, TrTotalMonth = 0
from    qry q
        cross join num n    -- num is a number / tally table
where   q.tendays   >= n.n
and not exists
    (
        select  *
        from    qry x
        where   x.year      = q.Year
        and x.Month     = q.Month
        and x.TenDays   = n.n
    )
order by Year, Month, TenDays

答案 2 :(得分:1)

请试试这个,我做对了:)

   SELECT Table2.year,
               Table2.month,
               Table2.TenDays,
               ISNULL(YourTable.TotalPerTenDays,0)
  FROM (SELECT 
 DISTINCT Year,Month,Table1.TenDays
FROM   (SELECT 1 AS TenDays UNION ALL SELECT 2 UNION ALL SELECT 3) AS Table1
CROSS JOIN YourTable)  AS Table2
 LEFT JOIN YourTable
            ON(YourTable.year         =             Table2.year
            AND YourTable.month =               Table2.month
            AND YourTable.TenDays = Table2.TenDays)