How to select all columns, and a count(*) in the same query

后端 未结 2 961
北荒
北荒 2020-12-28 13:23

In often use in TSQL the following query :

SELECT COUNT(*), * 
FROM CUSTOMER c 
WHERE c.Name like \'foo%\';

When I try to execute this que

相关标签:
2条回答
  • 2020-12-28 14:10

    This will perform better:

    SELECT COUNT(*) OVER (), c.*
    FROM CUSTOMER c 
    WHERE c.Name like 'foo%';
    
    0 讨论(0)
  • 2020-12-28 14:23

    One approach is to do something like the following. This will result in a count(*) result for each line. But beware, there is a Cartesianjoin; if you have many rows like 'foo%' this will perform badly.

    select a.cntr, c.*
    from CUSTOMER c 
       , (select count(*) cntr
         from customer b
         where b.name like 'foo%' ) a
    where c.name like 'foo%'
    
    0 讨论(0)
提交回复
热议问题