Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2013-10-14 : 07:29:45
|
Hi,I need help please.I need to return per idno and companyno where the total is negative. If I have a companyNo where the total for one row is positive but for another row it's negative then it's not relevant.To know if sum is negative or positive i need to do the followingsum(compensation+insurance)*factoringcreate table #payments (id int identity (1,1) not null ,idno int, compensation float, insurance float,factoring int, companyno int)insert into #payments (idno,compensation,insurance,factoring,companyno) values (502592 , 624.75 ,375,-1,3456)insert into #payments (idno,compensation,insurance,factoring,companyno) values (502592 , 124.75 ,275,-1 ,3456)insert into #payments (idno,compensation,insurance,factoring,companyno) values (797147 , 124.75 ,275, 1,5678)insert into #payments (idno,compensation,insurance,factoring,companyno) values (797147 , 124.75 ,275, -1,5678)insert into #payments (idno,compensation,insurance,factoring,companyno) values (461588 , 874.65 ,525,-1 ,78909)insert into #payments (idno,compensation,insurance,factoring,companyno) values (461588 , 874.65 ,525,1,126789 ) OUTPUTid idno compensation insurance factoring companyno1 502592 624.75 375 -1 34562 502592 124.75 275 -1 34565 461588 874.65 525 -1 78909 |
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2013-10-14 : 07:43:46
|
Select * From TableName Where Id In (1,2,5)veeranjaneyulu |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-14 : 08:48:54
|
Add a having clause like shown below:SELECT companyno,SUM((compensation+insurance)*factoring)FROM #payments GROUP BY companynoHAVING SUM((compensation+insurance)*factoring) < 0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-14 : 09:08:29
|
I think what you need is thisSELECT idno,compensation,insurance,factoring,companynoFROM(SELECT idno,compensation,insurance,factoring,companyno,SUM(CASE WHEN (compensation+insurance)*factoring > 0 THEN 1 ELSE 0 END) OVER (PARTITION BY CompanyNo) AS PosSumCntFROM #payments )tWHERE PosSumCnt = 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2013-10-14 : 09:23:40
|
quote: Originally posted by visakh16 I think what you need is thisSELECT idno,compensation,insurance,factoring,companynoFROM(SELECT idno,compensation,insurance,factoring,companyno,SUM(CASE WHEN (compensation+insurance)*factoring > 0 THEN 1 ELSE 0 END) OVER (PARTITION BY CompanyNo) AS PosSumCntFROM #payments )tWHERE PosSumCnt = 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Thanks but I don't need to return companies that also have positive values. If a company has a positive and a negative value than i mustn't return it. I must only show companies that have negative values only. |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2013-10-14 : 09:28:18
|
also it is important that i show the idno |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-14 : 09:32:06
|
quote: Originally posted by collie
quote: Originally posted by visakh16 I think what you need is thisSELECT idno,compensation,insurance,factoring,companynoFROM(SELECT idno,compensation,insurance,factoring,companyno,SUM(CASE WHEN (compensation+insurance)*factoring > 0 THEN 1 ELSE 0 END) OVER (PARTITION BY CompanyNo) AS PosSumCntFROM #payments )tWHERE PosSumCnt = 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Thanks but I don't need to return companies that also have positive values. If a company has a positive and a negative value than i mustn't return it. I must only show companies that have negative values only.
the given query will only show cases where company has only negative values------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2013-10-14 : 14:04:38
|
visakh16 you are correct. For some reason when i first ran it i received incorrect results but it was my mistake.Thanks so much for the help as always :)Thanks everyone that helped :) |
|
|
|
|
|
|
|