SQL Server: How can I use the COUNT clause without GROUPing?

后端 未结 3 1112
猫巷女王i
猫巷女王i 2021-01-15 00:44

I\'m looking get two things from a query, given a set of contraints:

  • The first match
  • The total number of matches

I can get the first ma

3条回答
  •  無奈伤痛
    2021-01-15 01:30

    Well, you can use OVER clause, which is an window function.

    SELECT  TOP (1)
        OrderID, CustID, EmpID,
        COUNT(*) OVER() AS MatchCount
    FROM    Sales.Orders
    WHERE   OrderID % 2 = 1
    ORDER BY OrderID DESC 
    

提交回复
热议问题