SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to make multi conditional QUERY?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

machine
Starting Member

1 Posts

Posted - 08/21/2014 :  16:03:53  Show Profile  Reply with Quote
SELECT
`loanac`.`id`,
`loanac`.`name`,
`loanac`.`lacc`,
SUM(`loantrans`.`in`) as totalin,
SUM(`loantrans`.`out`) as totalout

FROM loanac, loantrans
WHERE `loanac`.`lacc`=`loantrans`.`account`
GROUP BY `loanac`.`lacc`

Here is my query above which is working fine to show all accounts, BUT I need to find accounts where SUM(out) > SUM (in) instead of all accounts

I've tried to add condition using AND but its showing error, can anyone help?

Here is the db link in case if u want to take a look
https://www.dropbox.com/sh/o0ro7r23ztnpfky/AABrfLYUB5DUk-FRvPegHZF_a

Machine

tkizer
Almighty SQL Goddess

USA
37143 Posts

Posted - 08/21/2014 :  16:13:56  Show Profile  Visit tkizer's Homepage  Reply with Quote

; 
WITH CTE (id, name, lacc, totalin, totalout)
AS
(
	SELECT 
		loanac.id, 
		loanac.name, 
		loanac.lacc, 
		SUM(loantrans.in) as totalin, 
		SUM(loantrans.out) as totalout
	FROM loanac
	JOIN loantrans ON loanac.lacc=loantrans.account
	GROUP BY loanac.lacc
)
SELECT *
FROM CTE
WHERE totalout > totalin


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

bitsmed
Constraint Violating Yak Guru

406 Posts

Posted - 08/21/2014 :  16:55:15  Show Profile  Reply with Quote
Alternative:

SELECT 
`loanac`.`id`, 
`loanac`.`name`, 
`loanac`.`lacc`, 
SUM(`loantrans`.`in`) as totalin, 
SUM(`loantrans`.`out`) as totalout

FROM loanac
INNER JOIN loantrans ON `loanac`.`lacc`=`loantrans`.`account`
GROUP BY `loanac`.`lacc`
HAVING SUM(`loantrans`.`out`)>SUM(`loantrans`.`out`)
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37143 Posts

Posted - 08/21/2014 :  17:04:35  Show Profile  Visit tkizer's Homepage  Reply with Quote
Oh yes HAVING!

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

bitsmed
Constraint Violating Yak Guru

406 Posts

Posted - 08/21/2014 :  17:14:27  Show Profile  Reply with Quote
Correction to my last post. Out should be greater than In, and not like I wrote Out greater than Out, which is rubbish.

quote:
Originally posted by bitsmed

Alternative:

SELECT 
`loanac`.`id`, 
`loanac`.`name`, 
`loanac`.`lacc`, 
SUM(`loantrans`.`in`) as totalin, 
SUM(`loantrans`.`out`) as totalout

FROM loanac
INNER JOIN loantrans ON `loanac`.`lacc`=`loantrans`.`account`
GROUP BY `loanac`.`lacc`
HAVING SUM(`loantrans`.`out`)>SUM(`loantrans`.`outin`)


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000