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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Two select statements same table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Martin.Bergius
Starting Member

3 Posts

Posted - 08/21/2014 :  02:56:36  Show Profile  Reply with Quote
Hi,
I'm trying to get customerno,salestotal and profittotal from each customer from same table in same query.

I have no problem to get the values one by one like this(in two different queries):

Query 1:

SELECT AcTr.R4, SUM(AcAm*-1)
FROM AcTr
WHERE (AcTr.AcYr='2013') AND (AcTr.AcPr='11') AND (AcTr.AcNo>='3000' And AcTr.AcNo<='3999')
GROUP BY AcTr.R4
Result:
CustId SalesTotal(SUM(AcAm*-1)
CustA 100.000
CustB 200.000
CustC 100.000

Query 2:

SELECT AcTr.R4, SUM(AcAm*-1)
FROM AcTr
WHERE (AcTr.AcYr='2013') AND (AcTr.AcPr='11') AND (AcTr.AcNo>='3000' And AcTr.AcNo<='8999')
GROUP BY AcTr.R4
Result:
CustId ProfitTotal(SUM(AcAm*-1)
CustA 10.000
CustB 20.000
CustC 10.000

As you can see the only difference is the accounts in the where clause.


Is it possible to have everything in one query and get a result like this:

Customer SalesTotal ProfitTotal
CustA x y
CustB x y
CustC x y

Thanks in advance

/Martin

stepson
Constraint Violating Yak Guru

Romania
434 Posts

Posted - 08/21/2014 :  03:03:04  Show Profile  Reply with Quote

SELECT AcTr.R4
	, SUM(CASE WHEN AcTr.AcNo<='3999' THEN AcAm*-1 ELSE 0 END) as SalesTotal
	, SUM(AcAm*-1) asa ProfitTotal
FROM AcTr
WHERE (AcTr.AcYr='2013') AND (AcTr.AcPr='11') AND (AcTr.AcNo>='3000' And AcTr.AcNo<='8999')
GROUP BY AcTr.R4



sabinWeb MCP
Go to Top of Page

Martin.Bergius
Starting Member

3 Posts

Posted - 08/21/2014 :  04:37:16  Show Profile  Reply with Quote
ok, thanks.
And if i want to include another table with conditions?
I want to include customername ( Actor.Nm ) and want to get only the customers that are registrered/created 2013.(Actor.CreDt>='20130101')

Tried with:

SELECT AcTr.R4, Actor.Nm, SUM(CASE WHEN AcTr.AcNo<='3999' THEN AcAm*-1 ELSE 0 END) , SUM(AcAm*-1)
FROM F0001.dbo.AcTr AcTr, F0001.dbo.Actor Actor
WHERE (Actor.CustNo = AcTr.R4) AND (Actor.CreDt>='20140101') AND (AcTr.AcYr='2013') AND (AcTr.AcNo>='3000' AND AcTr.AcNo<='8999')
GROUP BY AcTr.R4

But getting error msg: ( Wrong columnexpression('SUM(CASE WHEN AcTr.AcNo<='3999' THEN AcAm*-1 ELSE 0 END)'
Go to Top of Page

stepson
Constraint Violating Yak Guru

Romania
434 Posts

Posted - 08/21/2014 :  04:43:04  Show Profile  Reply with Quote

SELECT AcTr.R4
,Actor.Nm
,SUM(CASE WHEN AcTr.AcNo <= '3999' THEN AcTr.AcAm*-1 ELSE 0 END) 
,SUM(AcTr.AcAm*-1)
 
FROM F0001.dbo.AcTr AcTr, F0001.dbo.Actor Actor
WHERE (Actor.CustNo = AcTr.R4) AND (Actor.CreDt>='20140101') AND (AcTr.AcYr='2013') AND (AcTr.AcNo>='3000' AND AcTr.AcNo<='8999')
GROUP BY AcTr.R4 
,Actor.Nm


What is the relation between AcTr and Actor ? the join field ?

Can you post some sample data ? for testing ...


sabinWeb MCP
Go to Top of Page

Martin.Bergius
Starting Member

3 Posts

Posted - 08/21/2014 :  05:19:28  Show Profile  Reply with Quote
Many Thanks. Actor.Nm in Group was the solution ;-)
Go to Top of Page

stepson
Constraint Violating Yak Guru

Romania
434 Posts

Posted - 08/21/2014 :  07:17:06  Show Profile  Reply with Quote
Your welcome!


sabinWeb MCP
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