sql server - How to get count and value using one t-sql statement? -
here trying get:-
select column1, count(column1), count(column2) table1
i know query invalid. there way can values of column1 , count of column1 , count of column2.
the on clause modifies aggregate range allow query happen want
select column1, count(column1) on (), count(column2) on () table1
edit:
above sql server 2005+
edit 2:
the cross join/count solution in sql server 2000 not reliable under load.
try multiple connections, note @@rowcount never equals t2.cnt in connection 2
--connection 1 set nocount on; drop table dbo.test_table; go create table dbo.test_table ( id_field uniqueidentifier not null default(newid()), filler char(2000) not null default('a') ); go create unique clustered index idx_id_fld on dbo.test_table(id_field); go while 1 = 1 insert dbo.test_table default values; --connection 2 select t2.cnt, t1.id_field, t1.filler dbo.test_table t1 cross join (select count(*) cnt dbo.test_table) t2 select @@rowcount select t2.cnt, t1.id_field, t1.filler dbo.test_table t1 cross join (select count(*) cnt dbo.test_table) t2 select @@rowcount select t2.cnt, t1.id_field, t1.filler dbo.test_table t1 cross join (select count(*) cnt dbo.test_table) t2 select @@rowcount
edit3: cyberkiwi stated test wrong. rubbish.
if forget @@rowcount, can see cnt
different row count in ssms "grid mode"
now, use set transaction isolation level serializable
, give correct results, if busy enough inconsistent results want run this?
use temp table ensure consistent dataset if running sql server 2000.
Comments
Post a Comment