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
 Functions in Where statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

steve_joecool
Starting Member

USA
12 Posts

Posted - 07/02/2014 :  17:05:52  Show Profile  Reply with Quote
Hello,

I am having a little trouble trying to figure this one out.

My query is this

SELECT DISTINCT fcompany
,SUM(fnetprice) As "TotalPrice"
,MAX(CONVERT(DATE,fduedate)) AS "LastPurchase"
FROM SalesHistory
Group by fcompany


I would like to filter out of the results 2 things..

#1 I would like ONLY the results where "TotalPrice" > 1000
#2 I would like "LastPurchase" only to be the system sate -365 days
(I know I can achieve that like This: CONVERT(DATE, fduedate) > dateadd(day,-365,getdate())

But neither of the 2 conditions I can add in a WHERE / AND clauses.

Is there any other way I can achieve what I'm looking for?

tkizer
Almighty SQL Goddess

USA
36942 Posts

Posted - 07/02/2014 :  17:08:40  Show Profile  Visit tkizer's Homepage  Reply with Quote
SELECT DISTINCT fcompany
,SUM(fnetprice) As "TotalPrice"
,MAX(CONVERT(DATE,fduedate)) AS "LastPurchase"
FROM SalesHistory
where fduedate>getdate()-365
Group by fcompany
HAVING SUM(fnetprice) > 1000

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

steve_joecool
Starting Member

USA
12 Posts

Posted - 07/02/2014 :  17:11:33  Show Profile  Reply with Quote
Thank you :-)
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
36942 Posts

Posted - 07/02/2014 :  17:12:03  Show Profile  Visit tkizer's Homepage  Reply with Quote


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

SwePeso
Patron Saint of Lost Yaks

Sweden
30242 Posts

Posted - 07/02/2014 :  17:20:26  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Why both DISTINCT and GROUP BY?



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
36942 Posts

Posted - 07/02/2014 :  17:21:12  Show Profile  Visit tkizer's Homepage  Reply with Quote
quote:
Originally posted by SwePeso

Why both DISTINCT and GROUP BY?




I usually catch those and remove the DISTINCT. Missed it this time!

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

tkizer
Almighty SQL Goddess

USA
36942 Posts

Posted - 07/02/2014 :  17:21:21  Show Profile  Visit tkizer's Homepage  Reply with Quote
SELECT fcompany
,SUM(fnetprice) As "TotalPrice"
,MAX(CONVERT(DATE,fduedate)) AS "LastPurchase"
FROM SalesHistory
where fduedate>getdate()-365
Group by fcompany
HAVING SUM(fnetprice) > 1000

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

steve_joecool
Starting Member

USA
12 Posts

Posted - 07/02/2014 :  19:03:42  Show Profile  Reply with Quote
Question:

Seems like this query is filtering all the INDIVIDUAL records where fduedate>getdate()-365 and HAVING SUM(fnetprice) > 1000

But!

I really would like to just not show that fcompany in the summary.

Example: What I did, I took the summaries (SUM, Group by, MAX) and took a sample of the data


fcompany custno fprice date
TCI POWDER COATING 50565 532.6 8/1/2013
TCI POWDER COATING 50565 532.6 8/1/2013
TCI POWDER COATING 50565 406.3 7/17/2013
TCI POWDER COATING 50565 406.3 7/17/2013
TCI POWDER COATING 50565 14 7/11/2013
TCI POWDER COATING 50565 532.6 7/9/2013

TCI POWDER COATING 50565 325.04 7/2/2013
TCI POWDER COATING 50565 325.04 7/2/2013

and it's filterin out the records in red... I would like for that company not show at all in the summary (when I insert again the group by, etc)

Does that make sense?
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
36942 Posts

Posted - 07/02/2014 :  19:07:31  Show Profile  Visit tkizer's Homepage  Reply with Quote
I didn't follow it.

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

steve_joecool
Starting Member

USA
12 Posts

Posted - 07/03/2014 :  10:45:50  Show Profile  Reply with Quote
OK, let me rephrase my question...

The ultimate goal is to only show accounts that don't have sales history in over one year and have bought more than $1000.

When I run this, the query is only taking out the individual records in the past year, but it's still showing the account in the summary

So, in the small data sample I took, it's Showing that TCI POWDER COATING has bought only $325.04 x 2 (the last 2 rows, because the fduedate>getdate()-365 condition holds true) but in reality, all I want is to discard that account from my report altogether because it does have sales history in the past year.

Does it make sense now?
Go to Top of Page

steve_joecool
Starting Member

USA
12 Posts

Posted - 07/03/2014 :  10:48:01  Show Profile  Reply with Quote
I was thinking a subquery might work here? I'm having trouble finding a logic for this :-(
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
36942 Posts

Posted - 07/03/2014 :  12:03:27  Show Profile  Visit tkizer's Homepage  Reply with Quote
http://www.sqlservercentral.com/articles/Best+Practices/61537/

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
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.07 seconds. Powered By: Snitz Forums 2000