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)
 Query Modify @ Immediate help plzz

Author  Topic 

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2012-10-24 : 22:52:01
Can any one help with below query. i am getting error saying that

Msg 8120, Level 16, State 1, Line 1
Column 'AEG_DB.dbo.MRO_TBL_EMCData.Application ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.



select [September 2012 Billing File] = CASE WHEN T2.[Application ID] IS NOT NULL AND T2.[ServerName]
IS NOT NULL AND Sum([Billed This Month]) <> 0 THEN 'Y' ELSE 'N' END
FROM [TBL_Work] T1
LEFT JOIN [TBL_Data] T2 ON (T1.[Application ID] = T2.[Application ID] AND T1.[ServerName] = T2.[ServerName])
WHERE T2.[Data Period] = 'September 2012'

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-10-24 : 23:02:02
Billed This Month is from which table ?

and why use LEFT JOIN to TBL_Data when you have a Where condition on TBL_Data ? it is effectively an INNER JOIN


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-10-24 : 23:06:35
perhaps this is what you want
select 	[September 2012 Billing File] = CASE WHEN T2.[Application ID] IS NOT NULL 
AND T2.[ServerName] IS NOT NULL
AND [Billed This Month] <> 0 THEN 'Y' ELSE 'N' END
FROM [AEG_DB].[dbo].[TBL_Work] T1
LEFT JOIN
(
SELECT [Application ID], [ServerName], [Billed This Month] = SUM([Billed This Month])
FROM [TBL_Data]
WHERE [Data Period] = 'September 2012'
GROUP BY [Application ID], [ServerName]
) T2 ON T1.[Application ID] = T2.[Application ID]
AND T1.[ServerName] = T2.[ServerName]



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2012-10-24 : 23:21:30
they need billing from this table
[AEG_DB].[dbo].[TBL_Work]
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-10-25 : 01:22:48
this ?
select 	[September 2012 Billing File] = CASE 	WHEN 	EXISTS
(
SELECT *
FROM [TBL_Data] T2
WHERE T2.[Application ID] = T1.[Application ID]
AND T2.[ServerName] = T1.ServerName
)
AND T1.[Billed This Month] <> 0
THEN 'Y'
ELSE 'N'
END
FROM [AEG_DB].[dbo].[TBL_Work] T1



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2012-10-25 : 13:58:15
Awesome...Thank you so much now the query running fyn
I appreciate your help...thanks for sql team
Go to Top of Page
   

- Advertisement -