Solved

sql server 'where' clause using outer sum.

Posted on 2016-09-05
2
29 Views
Last Modified: 2016-09-05
I have my query returning the rows. I would like to add a where clause but dont know how to do that with this query

use Dictionary

	select sum(CountOfword) as TotalWordCount, word, Cat, sum(CountOfTagword) as TotalTagWordCount
from
(


select count(D.clientcodewordposition) AS CountOfword, D.word, count(TWT.clientcodewordposition) AS CountOfTagword, vc.VehCategory as Cat
	FROM TblWords as D LEFT JOIN TblWordTags AS TWT ON (D.ClientCodeWordPosition = TWT.ClientCodeWordPosition) LEFT JOIN [TblVehCat] as VC ON D.ClientCode = vc.ClientCode
	where D.word='Auto' 
	GROUP BY D.Word, vc.VehCategory
)a
***** tried here WHERE TotalWordCount>TotalTagWordCount ***-- THIS IS THE PART i DONT UNDERSTAND
GROUP BY Word, Cat
ORDER BY Word;

Open in new window

ee1.JPG
0
Comment
Question by:PeterBaileyUk
2 Comments
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41784850
What you want is to filter by aggregate functions so you'll need a HAVING instead of WHERE:
use Dictionary

select sum(CountOfword) as TotalWordCount, word, Cat, sum(CountOfTagword) as TotalTagWordCount
from
(
select count(D.clientcodewordposition) AS CountOfword, D.word, count(TWT.clientcodewordposition) AS CountOfTagword, vc.VehCategory as Cat
	FROM TblWords as D LEFT JOIN TblWordTags AS TWT ON (D.ClientCodeWordPosition = TWT.ClientCodeWordPosition) LEFT JOIN [TblVehCat] as VC ON D.ClientCode = vc.ClientCode
	where D.word='Auto' 
	GROUP BY D.Word, vc.VehCategory
)a
GROUP BY Word, Cat
HAVING sum(CountOfword) >sum(CountOfTagword) 
ORDER BY Word;

Open in new window

0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41784895
great stuff thank you.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 66
sql server query from excel 3 57
SQL Server memory sizing - reallocation 16 40
Need SQL that flips  value 5 19
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now