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)
 Query + Group By

Author  Topic 

Santal_Maluko
Starting Member

14 Posts

Posted - 2007-06-22 : 09:53:26
Greetings to you all,
I have a little problem here.

I have this query

SELECT D.domainid AS departid, C.name AS Company, C.companyid AS Companyid, D.name AS departdesc, COUNT(E.empcode) AS employees,
SUM(E.monthdev) AS monthdev, SUM(E.totaldev) AS totaldev, S.shift_descr AS shiftdesc
FROM tb_T5_ShiftDomain AS SD INNER JOIN
tb_S57_Domains AS D ON SD.domainid = D.domainid INNER JOIN
tb_T5_Shifts AS S ON SD.shiftid = S.shift_id LEFT OUTER JOIN
tb_S57_Companies AS C INNER JOIN
tb_S57_CompanyDomains AS CD ON C.companyid = CD.companyid ON D.domainid = CD.domainid LEFT OUTER JOIN
tb_T5_MainView AS E ON D.domainid = E.departmentid RIGHT OUTER JOIN
tb_T5_AuxInfo AS A ON E.empcode = A.userid
WHERE (A.dismissed = 0)
GROUP BY D.domainid, C.companyid, S.shift_descr


It gives an error:
Column 'tb_S57_Companies.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I know that I should be grouping by companies.name and domains.name too... but if I do so... it will not show the companies with the same name

How can I work around this problem

Diogo Alves

efscl
Starting Member

4 Posts

Posted - 2007-06-22 : 09:55:12
use two queries with a join

the first is the group by (D.domainid, C.companyid, S.shift_descr) - than join with the second select to get the other informations too

hope this helps
Sebastian
Go to Top of Page

Santal_Maluko
Starting Member

14 Posts

Posted - 2007-06-22 : 10:05:04
Could you show me a little example?

Diogo Alves
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-06-25 : 01:40:07
Unless u specify the column in group by u can not take single value from the group.
Anyway u r grouping by company id , u can use max(c.name)

--------------------------------------------------
S.Ahamed
Go to Top of Page
   

- Advertisement -