同一个表上的内部联接并基于总和列减去日期

时间:2017-11-30 19:31:17

标签: sql sql-server inner-join

我正在使用SQL Server在C#中开发报告。我有一个包含一些文档编号的表(BrojDok),我有该行的日期和一列Saldo,它基于两列(挖掘和挖掘)的SUM。

以下是该查询的示例:

SELECT 
    tblFinansijskiPodaci.Firma, 
    tblFinansijskiPodaci.Konto, tblFinansijskiPodaci.NazivKonta,
    tblFinansijskiPodaci.NazFirme, tblFinansijskiPodaci.BrojDok,
    tblFinansijskiPodaci.DatumVal, tblFinansijskiPodaci.Valuta,
    SUM(tblFinansijskiPodaci.Duguje) AS dug,
    SUM(tblFinansijskiPodaci.Potrazuje)AS pot,
    SUM(IIf([tblFinansijskiPodaci].[Konto] Like '2%',
    [tblFinansijskiPodaci].[Duguje] -[tblFinansijskiPodaci].[Potrazuje],
    [tblFinansijskiPodaci].[Potrazuje] -[tblFinansijskiPodaci].[Duguje])) AS Saldo

结果是:

enter image description here

我需要加入关于BrojDok的表格。

例如,如果BrojDok是1728,则列Saldo应为0. 1379也是如此。

之后我需要一个代表基于此天数的列:如果Saldo小于0,则应为0,

如果Saldo介于0和1之间,那么Saldo应该是BrojDok的日期之间的差异 - 对于BrojDok 1728应该是2016-05-05 - 2016-05-04,

或者如果Saldo大于1,Saldo应该是今天日期和表格中日期之间的差异。

我已经尝试过使用CTE和子查询,但是我的SUM列完全搞砸了。这就是我在CTE中所拥有的:

; WITH CTE AS 
(
    SELECT 
        a.Firma, a.Konto, a.NazivKonta, a.BrojDok,
        SUM(a.Duguje) AS dugA,
        SUM(a.Potrazuje) AS potA,
        SUM(IIF(a.[Konto] LIKE '2%', a.[Duguje] - a.[Potrazuje], a.[Potrazuje] - a.[Duguje])) AS SaldoA,
        SUM(b.Duguje) AS dugB,
        SUM(b.Potrazuje)AS potB,
        SUM(IIf(b.[Konto] Like '2%',b.[Potrazuje] - b.[Duguje], b.[Potrazuje] - b.[Duguje])) AS SaldoB
    FROM 
        tblFinansijskiPodaci a 
    JOIN 
        tblFinansijskiPodaci b ON b.BrojDok = a.BrojDok
    WHERE 
        a.Firma = 1 AND a.Konto = 2040 
        AND a.Partner = 1137 AND b.Firma = 1
        AND b.Konto = 2040 AND b.Partner = 1137
    GROUP BY 
        a.Firma, a.NazFirme, a.Konto, a.NazivKonta, a.BrojDok, a.DatumVal, a.Valuta
)
SELECT 
    Firma, Konto, NazivKonta,
    BrojDok, potA, dugA, potB, dugB,
    SaldoA, SaldoB, SaldoB - SaldoA AS SaldoTotal 
FROM 
    CTE 
ORDER BY 
    Firma

这是我得到的结果:

enter image description here

我真的不知道从哪里开始..可以有人帮忙吗?

这是我想要得到的东西:

enter image description here

如果saldo <0,那么我会在表中用相同的BrojDok减去日期

3 个答案:

答案 0 :(得分:0)

如果a.Konto = 2040 AND b.Konto = 2040那么为什么要求a.[Konto] LIKE '2%'

我认为无论如何你都可以对整数使用LIKE。您需要先将其转换为varchar

CAST(a.[Konto] AS VARCHAR(9)) LIKE '2%'

答案 1 :(得分:0)

我的方法都错了。 我需要在BrojDok上使用OVER PARTITION,之后很容易,使用子查询只需在列上使用IIF来确定要做什么。

答案 2 :(得分:0)

过了一段时间我找到了答案。实际上很简单,我让它变得复杂了。当你正确地进行内部连接,并使用正确的列来表示最大和最小日期时,你得到了你需要的东西。

这是我的查询:

SELECT S2.FinID,S2.Firma,S2.Konto,S2.Partner,S2.NazFirme,S2.NazivKonta,S2.BrojDok,
S2.Valuta,S2.DatumVal,S2.dug,S2.pot,S2.Saldo,S2.MaxDate,S2.MinDate,S2.Saldo,S2.SaldoTotal,
IIF(S2.SaldoTotal>1,1,0) AS KonacnoProba,
IIF(S2.SaldoTotal>1,1,IIF(S2.SaldoTotal>0,DATEDIFF(day,S2.MinDate,S2.MaxDate),
DATEDIFF(day,S2.DatumVal,GETDATE()))) AS KP1,IIF(S2.SaldoTotal<=0,0,
IIF(S2.SaldoTotal<1,DATEDIFF(day,S2.MinDate,S2.MaxDate),
DATEDIFF(day,S2.DatumVal,GETDATE()))) AS Konacno
FROM
   (
     SELECT S1.FinID,S1.Firma,S1.Konto,S1.Partner,S1.NazFirme,S1.NazivKonta,S1.BrojDok,
     S1.Valuta,S1.DatumVal,S1.dug,S1.pot,S1.Saldo,S1.MaxDate,S1.MinDate,
     SUM(S1.Saldo) OVER (PARTITION BY S1.BrojDok ORDER BY S1.FinID) AS SaldoTotal
     FROM
         (
          SELECT t1.FinID, t1.Firma,t1.Konto,t1.Partner,t1.NazFirme,t1.NazivKonta,
          t1.BrojDok,t1.Valuta, t1.DatumVal,Sum(t1.Duguje) AS dug,
          Sum(t1.Potrazuje) AS pot,
          Sum(IIf(t1.[Konto] Like '2%',t1.[Duguje] -t1.[Potrazuje],t1.[Potrazuje]
          -t1.[Duguje])) AS Saldo,
          MAX(t2.DatumVal) as MaxDate,MIN(t2.DatumVal) as MinDate
          FROM tblFinansijskiPodaci t1
          inner join tblFinansijskiPodaci t2 ON t1.BrojDok = t2.BrojDok
          WHERE t1.Firma = 1 
          AND t1.Konto = 2040 AND t1.Partner = 1137 AND t2.Firma = 1 AND t2.Konto = 2040 
          AND t2.Partner = 1137
          GROUP BY t1.FinID, t1.Firma,t1.Konto,t1.Partner,t1.NazivKonta,t1.NazFirme,
          t1.BrojDok,t1.Valuta,t1.DatumVal
         ) AS S1
  ) AS S2