简体   繁体   English

如何使用一条T-SQL语句获取计数和值?

[英]How to get count and value using one t-sql statement?

Here is what i am trying to get:- 这是我想要得到的:

SELECT 
  Column1, 
  Count(Column1), 
  Count(Column2)
FROM Table1

I know this query is invalid. 我知道此查询无效。 But is there any way I can all the values of Column1 and Count of Column1 and Count of Column2. 但是有什么办法可以使Column1的所有值以及Column1的计数和Column2的计数。

The OVER clause modifies the aggregate range to allow the query to happen as you want OVER子句修改聚合范围以允许查询根据需要发生

SELECT 
  Column1, 
  Count(Column1) OVER (), 
  Count(Column2) OVER ()
FROM Table1

Edit: 编辑:

above for SQL Server 2005+ 以上适用于SQL Server 2005+

Edit 2: 编辑2:

The CROSS JOIN/COUNT solution in SQL Server 2000 is not reliable under load. SQL Server 2000中的CROSS JOIN / COUNT解决方案在负载下不可靠。

Try this from multiple connections, note @@ROWCOUNT never equals T2.cnt in connection 2 从多个连接尝试此操作,请注意在连接2中@@ ROWCOUNT决不等于T2.cnt

--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 into dbo.test_table default values;

--connection 2 
select T2.cnt, T1.id_field, T1.filler
from dbo.test_table T1
cross join (select COUNT(*) as cnt from dbo.test_table) T2
SELECT @@ROWCOUNT

select T2.cnt, T1.id_field, T1.filler
from dbo.test_table T1
cross join (select COUNT(*) as cnt from dbo.test_table) T2
SELECT @@ROWCOUNT

select T2.cnt, T1.id_field, T1.filler
from dbo.test_table T1
cross join (select COUNT(*) as cnt from dbo.test_table) T2
SELECT @@ROWCOUNT

Edit3: cyberkiwi stated the test is wrong. Edit3:cyberkiwi表示测试错误。 Rubbish. 垃圾。

If you forget @@ROWCOUNT, you can see cnt is different to the row count in SSMS "grid mode" 如果您忘记了@@ ROWCOUNT,则可以看到cnt与SSMS“网格模式”中的行数不同

Now, you use SET TRANSACTION ISOLATION LEVEL SERIALIZABLE and it does give correct results, but if you are busy enough to get inconsistent results do you really want to run this? 现在,您可以使用SET TRANSACTION ISOLATION LEVEL SERIALIZABLE并提供正确的结果,但是如果您忙于获得不一致的结果,您真的要运行此程序吗?

Use a temp table to ensure a consistent dataset if you are running SQL Server 2000. 如果您正在运行SQL Server 2000,请使用临时表来确保数据集的一致性。

Try: 尝试:

SELECT column1,(SELECT COUNT(Column1),COUNT(Column2)) AS CNT FROM Table1

That should get you what you want, then when you run your fetch command you use it like you would normally. 那应该可以为您提供所需的东西,然后在运行fetch命令时像平常一样使用它。

Eg in php, $rows = mysql_fetch_assoc(mysql_query($query)); echo $rows['CNT']; 例如在php中, $rows = mysql_fetch_assoc(mysql_query($query)); echo $rows['CNT']; $rows = mysql_fetch_assoc(mysql_query($query)); echo $rows['CNT'];

The question, as asked, does not make sense. 所提出的问题没有道理。 Do you mean that you want the counts of individual combinations of values of Col1 and Col2? 您是说要对Col1和Col2的值的各个组合进行计数吗? If so: 如果是这样的话:

select column1,column2,count(*)
  from table1
 group by column1, column2

If this is not what you want please try to give some sample data, what the inputs might look like and what your desired output would be. 如果这不是您想要的,请尝试提供一些示例数据,输入可能是什么样子以及所需的输出是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM