Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select statement with aggregate funtion

Author  Topic 

sqlnovice123
Constraint Violating Yak Guru

262 Posts

Posted - 2010-02-25 : 17:22:52
Hi I get the following error for the below query:
Can someone please help?

Column 'pivotal..Company.Company_Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

select
DealerID = dlr.Company_Id,
DealerType = case par.Holding_Company_Type
when 'Anonymous' then 'Regional'
else par.Holding_Company_Type
end,
DumbSort = case par.Holding_Company_Type
when 'Anonymous' then 20
else 30
end,
--TypeOfDealer = CASE WHEN @ThresholdDate < MIN(trd.TradeDate) THEN 'OLD' ELSE 'NEW' END
TypeOfDealer = CASE WHEN '20000505' < MIN(trd.TradeDate) THEN 'OLD' ELSE 'NEW' END

--into #DealerList
from
pivotal..Company dlr
inner join
(
select Company_Id, Holding_Company_Type
from pivotal..Company
where Holding_Company_Type in ('Anonymous', 'Diversity')
and Client_Active_Status = 'Active'
) par
on

dlr.Parent_Company_Id = par.Company_Id
inner join
Trade trd
on trd.CPPivotalCompanyID = dlr.Company_Id
where
Client_Active_Status = 'Active'

raky
Aged Yak Warrior

767 Posts

Posted - 2010-02-26 : 00:57:08
try this

select
DealerID = dlr.Company_Id,
DealerType = case par.Holding_Company_Type
when 'Anonymous' then 'Regional'
else par.Holding_Company_Type
end,
DumbSort = case par.Holding_Company_Type
when 'Anonymous' then 20
else 30
end,
--TypeOfDealer = CASE WHEN @ThresholdDate < MIN(trd.TradeDate) THEN 'OLD' ELSE 'NEW' END
TypeOfDealer = CASE WHEN '20000505' < MIN(trd.TradeDate) THEN 'OLD' ELSE 'NEW' END

--into #DealerList
from
pivotal..Company dlr
inner join
(
select Company_Id, Holding_Company_Type
from pivotal..Company
where Holding_Company_Type in ('Anonymous', 'Diversity')
and Client_Active_Status = 'Active'
) par
on

dlr.Parent_Company_Id = par.Company_Id
inner join
Trade trd
on trd.CPPivotalCompanyID = dlr.Company_Id
where
Client_Active_Status = 'Active'
group by pivotal..Company.Company_Id

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-26 : 08:37:11
if you're aggregating a column then you need to add other columns in group by if you want to use them in select list

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -