开发者

Simple MS SQL Bug

开发者 https://www.devze.com 2023-01-30 14:17 出处:网络
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\'.

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

精彩评论

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

关注公众号