开发者

Simple MS SQL Bug

I am very new to MS SQL and am trying to get all duplicate emails in a table. I found a similar query that works for MySQL but when run it in MS SQL I get the error Invalid column name 'ct'.

The query I开发者_StackOverflow社区 run is:

SELECT   
  [ShipEmail], 
  COUNT(*) as ct

FROM [ShipWorksDefault].[dbo].[Customers] GROUP BY [ShipEmail] HAVING [ct] > 1

I am guessing there is something very simple I am over looking.

Many thanks in advance!


Just change the HAVING clause to not use the column alias:

HAVING count(*) > 1


You can't use the alias in the having clause. Try:

SELECT   
  [ShipEmail], 
  COUNT(*) as ct

FROM [ShipWorksDefault].[dbo].[Customers] GROUP BY [ShipEmail] HAVING COUNT(*) > 1


SELECT   
  [ShipEmail], 
  COUNT(*) as ct
FROM [ShipWorksDefault].[dbo].[Customers] 
GROUP BY [ShipEmail] HAVING COUNT(*) > 1

or

SELECT   
  [ShipEmail], 
  COUNT(*) as ct
FROM [ShipWorksDefault].[dbo].[Customers] 
GROUP BY [ShipEmail] HAVING COUNT([ShipEmail]) > 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜