简体   繁体   中英

SQL Server : SELECT with DISTINCT on variable multiple columns

I need to count the number of distinct items from this table but the distinct is over many columns stocked in a variable. This is 2 req.

Req 1:

SELECT COUNT(DISTINCT (CHECKSUM(code_paiement,id_emetteur,id_liaison)))
FROM [DB1].[dbo].[Vo_Fait]

Req 2:

declare @var nvarchar(4000) = 'code_paiement, id_emetteur, id_liaison'

SELECT COUNT(DISTINCT (CHECKSUM(@var)))
FROM [DB1].[dbo].[Vo_Fait]

But the result of this 2 req is different !

  • Result req 1 : 45205
  • Result req 2 : 1

In "req2" you're taking the checksum of the string 'code_paiement,id_emetteur,id_liaison', which will always be the same, and counting the distinct values of it, which will always be 1.

Think of it this way: your code reduces to:

SELECT COUNT(DISTINCT (CHECKSUM('code_paiement,id_emetteur,id_liaison')))
FROM [DB1].[dbo].[Vo_Fait]

...which is the same as:

SELECT COUNT(DISTINCT (-1998057055))
FROM [DB1].[dbo].[Vo_Fait]

So you're counting the number of distinct occurrences of the number -1998057055 for every row in the table; as the value is the same for every row, there is only one distinct occurrence.

If you really need to build your SQL code dynamically (generally you should avoid this if possible by changing your design), then you should use something like exec or sp_executesql . A trivial example:

DECLARE @var NVARCHAR(4000) = 'code_paiement,id_emetteur,id_liaison';
DECLARE @sql NVARCHAR(4000)='SELECT COUNT(DISTINCT(CHECKSUM(' + @var + '))) FROM [DB1].[dbo].[Vo_Fait]';
EXEC (@sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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