Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get an error "Invalid object name 'ttt'".
What's the correct query?

SQL
Select 
AgencyName,
PerName,
Branch,
(Select SUM(ttt.OfferCount) from ttt where ttt.Satate=5) as Done,
(Select SUM(ttt.OfferCount) from ttt where ttt.State!=5) as NotDone
from 
(select TB_Agency.AgencyName, TB_Personel.PerName, TB_Offer.State, count(TB_Offer.Branch) as OfferCount,
TB_Offer.State
from TB_Agency

join TB_Personel on TB_Agency.AgencyID= TB_Personel.AgencyID

join TB_Offer on TB_Personel.PerID = TB_Offer.PerID

GROUP BY TB_Acente.AgencyName, TB_Personel.PerName, TB_Offer.Branch,TB_Offer.State
) 
as
ttt
Posted
Updated 16-Sep-15 4:53am
v2
Comments
FrankNight 17-Sep-15 3:29am    
subqueries syntax is subject to change across database server. What DB Server are you using?

Try to run the subquery alone... I don't think it works as you expect:
TB_Offer.State is present twice... it's correct?!?
In the group by expression there is also the target of the aggregation function..

select
TB_Agency.AgencyName,
TB_Personel.PerName,
TB_Offer.State,
count(TB_Offer.Branch) as OfferCount,
//TB_Offer.State (?!?)
from
TB_Agency
join TB_Personel on TB_Agency.AgencyID= TB_Personel.AgencyID
join TB_Offer on TB_Personel.PerID = TB_Offer.PerID
GROUP BY
TB_Acente.AgencyName,
TB_Personel.PerName,
//TB_Offer.Branch, (?!?)
TB_Offer.State

You can't select from the results group!

Look at your code: it boils down to:
SQL
SELECT branch, (SELECT COUNT(*) FROM ttt) FROM MyTable AS ttt

So in order to select the second term, it must in each case evaluate all rows. But in order to evaluate all rows, it first needs to evaluate the second term.

I'm not sure exactly what results you want from your data, so I can;t make a concrete solution, but that certainly isn't it!

It's possible that you just need to move the "AS ttt" inside the bracket, but...
 
Share this answer
 
Comments
amagitech 16-Sep-15 10:31am    
Select
ttt.AgencyName,
ttt.PerName,
ttt.Branch,
from
(select TB_Agency.AgencyName, TB_Personel.PerName, TB_Offer.State, count(TB_Offer.Branch) as OfferCount,
TB_Offer.State
from TB_Agency

join TB_Personel on TB_Agency.AgencyID= TB_Personel.AgencyID

join TB_Offer on TB_Personel.PerID = TB_Offer.PerID

GROUP BY TB_Acente.AgencyName, TB_Personel.PerName, TB_Offer.Branch,TB_Offer.State
)
as
ttt



has worked I can İmagine difference between.
I want to do this.
http://i62.tinypic.com/1z3q9l1.jpg
I would possibly look at modifying the code to the following

SQL
Select
AgencyName,
PerName,
Branch,
(SUM(case when ttt.State = 5 then ttt.OfferCount else 0 end) as Done,
(SUM(case when ttt.State != 5 then ttt.OfferCount else 0 end) as NotDone
from
(select TB_Agency.AgencyName, TB_Personel.PerName, TB_Offer.State, count(TB_Offer.Branch) as OfferCount,
TB_Offer.State
from TB_Agency

join TB_Personel on TB_Agency.AgencyID= TB_Personel.AgencyID

join TB_Offer on TB_Personel.PerID = TB_Offer.PerID

GROUP BY TB_Acente.AgencyName, TB_Personel.PerName, TB_Offer.Branch,TB_Offer.State
)
as
ttt


The only other way I could think of doing such a query would be to use Common Table Expression, but then I have made an assumption that you are running Sql Server
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900