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 2008 Forums
 Transact-SQL (2008)
 Problem with GROUP BY

Author  Topic 

nietzky
Yak Posting Veteran

75 Posts

Posted - 2013-09-13 : 15:42:58
I am trying to show the data using this query:
Desired result:
ID_CON Build_PLANNER_SID Build_PLANNER APP_DEV_MANAGER_SID APP_DEV_MANAGER OPS_MANAGER_SID OPS_MANAGER

Query:

select *
from openquery([TEST],

'
SELECT
AC.Construction_ID as ID_CON,
--AC.CONTACTSID as CONTACT_SID,
--AC.CONTACTTYPE as CONTACT_TYPE,
--SYSDATE AS LAST_REFRESHED,
MAX(CASE WHEN AC.CONTACTTYPE = ''Build Planner'' THEN AC.CONTACTSID END) AS Build_PLANNER_SID,
MAX(CASE WHEN AC.CONTACTTYPE = ''Build Planner'' THEN AC.CONTACTTYPE END) AS Build_PLANNER,
MAX(CASE WHEN AC.CONTACTTYPE = ''Construction Development Manager'' THEN AC.CONTACTSID END) AS APP_DEV_MANAGER_SID,
MAX(CASE WHEN AC.CONTACTTYPE = ''Construction Development Manager'' THEN AC.CONTACTTYPE END) AS APP_DEV_MANAGER,
MAX(CASE WHEN AC.CONTACTTYPE = ''OPS Manager'' THEN AC.CONTACTSID END) AS OPS_MANAGER_SID,
MAX(CASE WHEN AC.CONTACTTYPE = ''OPS Manager'' THEN AC.CONTACTTYPE END) AS OPS_MANAGER

FROM DM_PLACE.APP_CONTACTS AC
WHERE AC.CONTACTTYPE = ''Construction Development Manager''
OR AC.CONTACTTYPE = ''Build Planner''
OR AC.CONTACTTYPE =''OPS Manager''
GROUP BY
AC.Construction_ID

')

however I am not getting the same number of records if I just to run the query like this, this query returns more records than the first one, I would think I am going to get the smae number of ID_CONs (unique ones) or even bigger number:




select *
from openquery([TEST],

'
SELECT DISTINCT
AC.Construction_ID as ID_CON,
AC.CONTACTSID as CONTACT_SID,
AC.CONTACTTYPE as CONTACT_TYPE,
SYSDATE AS LAST_REFRESHED

FROM DM_PLACE.APP_CONTACTS AC
WHERE AC.CONTACTTYPE = ''Construction Development Manager''


')

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-09-13 : 15:53:00
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -