我通常在子查询中使用COUNT来获取所需的数字,但在这种情况下,我需要一些帮助,因为查询包含太多参数。
SELECT a.[QueueID]
,a.[CouponID]
,a.[ListingID]
,a.[User_ID]
,b.[CouponID]
,b.[ListingID]
,b.[CouponActive]
,b.[CouponExpire]
,b.[IsDeleted]
,c.[ListingID]
,c.[TypeID]
,c.[LevelID]
,@passedUserID as User_ID
FROM CouponQueue a
JOIN Coupon b
on a.CouponID = b.CouponID
JOIN Listing c
on b.ListingID = c.ListingID
WHERE (a.[User_ID] = @passedUserID)
AND (b.[CouponActive] = 1)
AND (b.[IsDeleted] = 0)
AND (b.[CouponExpire] > DATEADD(dd, -1, GETDATE()) OR b.[CouponExpire] IS NULL)
因此,假设此查询返回7行的结果。我需要的只是我的VIEW的这个数字。所以我想将最终结果限制为一行,以便最终得到:
[TotalCount] <-- Field name
[7] <-- Result
但不是7行数据..我只需要从上面的查询计数。仍然插手并试图学习。我看了几个其他的例子,但我没有找到一个具备所有条件的......这就是搞砸了我的原因。请帮忙!
非常感谢你!
答案 0 :(得分:3)
这对你有用吗?
select count(*) as TotalCOunt from (
SELECT a.[QueueID] /*
,a.[CouponID]
,a.[ListingID]
,a.[User_ID]
,b.[CouponID]
,b.[ListingID]
,b.[CouponActive]
,b.[CouponExpire]
,b.[IsDeleted]
,c.[ListingID]
,c.[TypeID]
,c.[LevelID]
,@passedUserID as User_ID */
FROM CouponQueue a
JOIN Coupon b
on a.CouponID = b.CouponID
JOIN Listing c
on b.ListingID = c.ListingID
WHERE (a.[User_ID] = @passedUserID)
AND (b.[CouponActive] = 1)
AND (b.[IsDeleted] = 0)
AND (b.[CouponExpire] > DATEADD(dd, -1, GETDATE()) OR b.[CouponExpire] IS NULL)
) t
您可以删除计数列。它们实际上并不是必需的。
答案 1 :(得分:0)
应该只能添加COUNT(*):
SELECT COUNT(*) as TotalCount
FROM CouponQueue a
JOIN Coupon b
on a.CouponID = b.CouponID
JOIN Listing c
on b.ListingID = c.ListingID
WHERE (a.[User_ID] = @passedUserID)
AND (b.[CouponActive] = 1)
AND (b.[IsDeleted] = 0)
AND (b.[CouponExpire] > DATEADD(dd, -1, GETDATE()) OR b.[CouponExpire] IS NULL)
祝你好运。
答案 2 :(得分:0)
您可以将以下示例放在SQL Developer上以进行计数:
SELECT count(*) as totalCount (*-open a parentheses - your original query -close the parentheses*)