I have subquery that calculates some sums.And based on the value in column Saldo, I wanna manipulate and subtract some date columns. My problem is that I get duplicate rows. I want to have one row of each FinID,and if the BrojDok is the same,I want to subtract the dates based on a value from Saldo column. My datediff is working. My isare duplicate rows. How can I manage that?
I tried with DISTINCT,but it just gives me the first row unique.
FinID is the PK of my table,and BrojDok is the field that I need to do the JOIN.
This is my query:
SELECT P2.FinID, P2.Firma, P2.BrojDok, P2.DatumVal,P2.Saldo ,P2.SaldoTotal2, IIF(P2.SaldoTotal2<0,0,IIF(P2.SaldoTotal2<1,(DATEDIFF(DAY,P2.DatumVal, b.DatumVal)),0)) AS NumberOfDays FROM ( SELECT P1.FinID, P1.Firma,P1.BrojDok,P1.DatumVal,P1.Saldo,P1.SaldoTotal, IIF(P1.SaldoTotal<0,0,IIF(P1.SaldoTotal>1,1,0)) AS SaldoTotal1, IIF(P1.SaldoTotal<0,0,IIF(P1.SaldoTotal<1,0,1)) AS SaldoTotal2 FROM ( SELECT P.FinID,P.Firma,P.BrojDok,P.DatumVal,P.Saldo , SUM(Saldo) OVER (PARTITION BY BrojDok ORDER BY FinID) AS SaldoTotal FROM ( SELECT a.FinID, a.Firma, a.Konto,a.NazivKonta, a.NazFirme, a.BrojDok, a.DatumVal,a.Valuta, Sum(IIf(a.[Konto] Like '2%',a.[Duguje] -a.[Potrazuje],a.[Potrazuje] -a.[Duguje])) AS Saldo FROM tblFinansijskiPodaci a WHERE a.Firma = 1 AND a.Konto = 2040 AND a.Partner = 1137 GROUP BY a.FinID,a.Firma,a.NazFirme,a.Konto,a.NazivKonta,a.BrojDok, a.DatumVal,a.Valuta,a.Duguje,a.Potrazuje ) AS P GROUP BY FinID,Firma,BrojDok,Saldo,DatumVal ) AS P1 GROUP BY P1.FinID,P1.Firma,P1.BrojDok,P1.DatumVal,P1.Saldo,P1.SaldoTotal ) AS P2 INNER JOIN tblFinansijskiPodaci b ON b.BrojDok=P2.BrojDok GROUP BY P2.FinID,P2.Firma,P2.BrojDok,P2.Saldo,P2.SaldoTotal,P2.SaldoTotal1,P2.SaldoTotal2,P2.DatumVal ORDER BY BrojDok
This is my result: https://i.stack.imgur.com/Hzlke.png
This is what I would like to get for the BrojDok 1379.
https://i.stack.imgur.com/YyS1c.png But this are results without inner join,so my Number of Days are not working.
I need to subtract dates based on value from Saldo column.
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!