在连接查询中使用count(*)的SL错误

时间:2012-01-10 19:40:04

标签: sql sql-server sql-server-2005

我试图在存储过程中使用以下查询来获取Promotion表和Countnt_Views表中count(*)的所有内容。但我从'。'得到错误“关键字'附近的语法不正确”。这是在SQl Server 2005中。我喜欢这是如何解决的。我究竟做错了什么。谢谢你的帮助!

SELECT *, (count(*) from Merchant_Views) FROM dbo.Promotion
INNER JOIN dbo.Merchant_Location ON dbo.Promotion.LocationID = dbo.Merchant_Location.LocationID
INNER JOIN dbo.Merchant_Views MV ON MV.LocationID = dbo.Promotion.LocationID
and dbo.Merchant_Views.ClickDate >= dbo.Promotion.startDate 
and dbo.Merchant_Views.ClickDate <=  dbo.Promotion.expireDate
and dbo.Merchant_Views.Status <> 'printed' 
WHERE dbo.Promotion.MerchantID=@MerchantID AND PostType=@PostType

谢谢!

6 个答案:

答案 0 :(得分:1)

您在COUNT(*)

之前缺少SELECT
SELECT *, ViewCnt
FROM dbo.Promotion p
     INNER JOIN dbo.Merchant_Location ml ON p.LocationID = ml.LocationID
     INNER JOIN dbo.Merchant_Views mv ON mv.LocationID = p.LocationID
     INNER JOIN 
     (SELECT p.ID, COUNT(*) AS ViewCnt
      FROM dbo.Promotion p
           INNER JOIN dbo.Merchant_Views mv ON mv.LocationID = p.LocationID
      GROUP BY p.Id) t ON t.ID = p.ID
WHERE p.MerchantID= @MerchantID AND PostType=@PostType
  AND MV.ClickDate >= p.startDate 
  AND MV.ClickDate <=  p.expireDate
  AND MV.Status <> 'printed' 

答案 1 :(得分:1)

SELECT *, (SELECT count(*) from Merchant_Views) FROM dbo.Promotion P
INNER JOIN dbo.Merchant_Location ML ON P.LocationID = ML.LocationID
INNER JOIN dbo.Merchant_Views MV ON MV.LocationID = P.LocationID
and MV.ClickDate >= P.startDate 
and MV.ClickDate <=  P.expireDate
and MV.Status <> 'printed' 
WHERE P.MerchantID=@MerchantID AND PostType=@PostType

修改

你正在寻找这样的东西吗?

SELECT count(P.PromotionID) AS COUNT  --,   MV.column1, mv.column2 --and so on..
FROM dbo.Promotion P
INNER JOIN dbo.Merchant_Location ML ON P.LocationID = ML.LocationID
INNER JOIN dbo.Merchant_Views MV ON MV.LocationID = P.LocationID
and MV.ClickDate >= P.startDate 
and MV.ClickDate <=  P.expireDate
and MV.Status <> 'printed' 
WHERE P.MerchantID=@MerchantID AND PostType=@PostType
--group by --  MV.column1, mv.column2 --and so on..

答案 2 :(得分:1)

是你真的想在这里使用一个聚合(count())组。

SELECT p.field,p.field2.p.field3,ml.f1,ml.f2,ml.f3,mv.f1,mv.f2,mv.f3, count(mv.*) FROM dbo.Promotion P
INNER JOIN dbo.Merchant_Location ML ON P.LocationID = ML.LocationID
INNER JOIN dbo.Merchant_Views MV ON MV.LocationID = P.LocationID
and MV.ClickDate >= P.startDate 
and MV.ClickDate <=  P.expireDate
and MV.Status <> 'printed' 
WHERE P.MerchantID=@MerchantID AND PostType=@PostType
group by p.field,p.field2.p.field3,ml.f1,ml.f2,ml.f3,mv.f1,mv.f2,mv.f3

答案 3 :(得分:0)

您需要在内部查询上使用SELECT。

SELECT *, (SELECT count(*) FROM Merchant_Views) FROM dbo.Promotion
...

答案 4 :(得分:0)

我认为这是因为选择

中的这个条款
(count(*) from Merchant_Views)

答案 5 :(得分:0)

你在内部SELECT中错过了一个SELECT:

SELECT *, (SELECT count(*) from Merchant_Views) FROM dbo.Promotion
INNER JOIN dbo.Merchant_Location ON dbo.Promotion.LocationID = dbo.Merchant_Location.LocationID
INNER JOIN dbo.Merchant_Views MV ON MV.LocationID = dbo.Promotion.LocationID
and dbo.Merchant_Views.ClickDate >= dbo.Promotion.startDate 
and dbo.Merchant_Views.ClickDate <=  dbo.Promotion.expireDate
and dbo.Merchant_Views.Status <> 'printed' 
WHERE dbo.Promotion.MerchantID=@MerchantID AND PostType=@PostType