简体   繁体   中英

SQL Server how to define other column values based on one column on subquery inner join?

I have a query that uses inner join on the same table and calculates some balance. I am using partition over for this. And depending on the balance value, it is subtracting some dates.

My query works fine, but I have a problem. If I have balance days at the join to be zero at the end, I need all columns with the same BrojDoK (column that the inner join is based on) to be zero.

This is the result what I have so far:

在此处输入图片说明

What I need is that in this case, for BrojDok = 648 , since the BalanceTotal at the end is 0, I need the BalanceDays for FinID = 5856 also to be zero.

I would like to get something like this:

在此处输入图片说明

Can I somehow calculate another column that would do this?

I was looking the update method, but I couldn't manage to do anything with it.

This is my query so far:

SELECT 
    S2.FinID, S2.Firma, S2.Konto, S2.Partner,
    S2.BrojDok, S2.DatumVal, S2.pot, S2.dug,
    S2.Balance, S2.BalanceTotal,
    IIF(S2.BalanceTotal > 0,
        IIF(S2.BalanceTotal < 1, DATEDIFF(DAY, S2.MinDate, S2.MaxDate),
                                 DATEDIFF(DAY, S2.DatumVal, GETDATE())),
    IIF(S2.BalanceTotal = 0, 0, 0)) AS BalanceDays
FROM
    (SELECT 
         S1.FinID, S1.Firma, S1.NazFirme, S1.Konto, S1.NazivKonta,
         S1.Partner, S1.NazivPartnera, S1.BrojDok, S1.DatumVal, 
         S1.pot, S1.dug, S1.Balance, S1.MaxDate, S1.MinDate,
         SUM(S1.Balance) OVER (PARTITION BY S1.BrojDok ORDER BY S1.FinID) AS BalanceTotal
     FROM
         (SELECT 
              t1.FinID, t1.Firma, t1.NazFirme, t1.Konto, t1.NazivKonta, 
              t1.Partner, t1.NazivPartnera, t1.BrojDok, 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 Balance,
              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 = 1102
              AND t2.Firma = 1 AND t2.Konto = 2040 AND t2.Partner = 1102
         GROUP BY 
             t1.FinID, t1.Firma, t1.NazFirme, t1.Konto, t1.NazivKonta,
             t1.Partner, t1.NazivPartnera, t1.BrojDok, t1.DatumVal) AS S1
   ) AS S2
ORDER BY 
    BrojDok

since the BalanceTotal at the end is 0, I need the BalanceDays for FinID = 5856 also to be zero.

In this line:

IIF(S2.BalanceTotal = 0, 0, 0)) AS BalanceDays

Replace S2.BalanceTotal with a subquery that checks the BalanceTotal "at the end".

If someone needs something like this, I have found the solution.I just add the column that calculates the MIN OVER PARTITION BY.

In my case:

MIN(S3.BalanceDays) OVER (PARTITION BY S3.BrojDok) AS BalanceDays1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM