获取不同SQL Server之后的记录总数

时间:2016-05-13 10:03:05

标签: sql sql-server tsql sql-server-2014

我有一个连接到3个表的查询。没有" DISTINCT"总记录是331,但是Distinct总数是113.我想得到113总数,这是不同记录的总数。我使用了Count但是它给了我不唯一记录的总数。请帮我搞清楚不同的记录。这是我的疑问。

  

没有明显(331条记录)

SELECT  
uf.OrigFileName,
uf.CreatedOn,
sdiTran.Status,
sdiFS.FileName,
sdiFile.ArchiveLogID,
COUNT(*) over() as totalRows
FROM [SDI].dbo.UploadedFile uf 
inner join [SDI].dbo.SDIFile sdiFile on uf.UploadedFullFileName = sdiFile.OriginalName 
left join [SDI].dbo.SDITransaction sdiTran on sdiFile.ID = sdiTran.SDIFileID 
inner join [SDI].dbo.SDIFSArchive sdiFS on sdiFile.ID = sdiFS.SDIFileID
WHERE uf.CommunityID = '7cc67de8-e5c2-4055-958b-f604c6a40cf1' 
AND uf.OrganizationID='e5750df1-0409-46b6-9aba-7f07be7c890c'

enter image description here

  

After Distinct(113条记录)。 totalRows应该只有113,因为我得到的是不同的记录。

SELECT distinct
uf.OrigFileName,
uf.CreatedOn,
sdiTran.Status,
sdiFS.FileName,
sdiFile.ArchiveLogID,
COUNT(*) over() as totalRows
FROM [SDI].dbo.UploadedFile uf 
inner join [SDI].dbo.SDIFile sdiFile on uf.UploadedFullFileName = sdiFile.OriginalName 
left join [SDI].dbo.SDITransaction sdiTran on sdiFile.ID = sdiTran.SDIFileID 
inner join [SDI].dbo.SDIFSArchive sdiFS on sdiFile.ID = sdiFS.SDIFileID
WHERE uf.CommunityID = '7cc67de8-e5c2-4055-958b-f604c6a40cf1' 
AND uf.OrganizationID='e5750df1-0409-46b6-9aba-7f07be7c890c'

enter image description here

3 个答案:

答案 0 :(得分:2)

您可以在子查询中使用distinct:

SELECT *
    , COUNT(*) over() as totalRows
FROM (
    SELECT DISTINCT
    uf.OrigFileName,
    uf.CreatedOn,
    sdiTran.Status,
    sdiFS.FileName,
    sdiFile.ArchiveLogID
    FROM [SDI].dbo.UploadedFile uf 
    inner join [SDI].dbo.SDIFile sdiFile on uf.UploadedFullFileName = sdiFile.OriginalName 
    left join [SDI].dbo.SDITransaction sdiTran on sdiFile.ID = sdiTran.SDIFileID 
    inner join [SDI].dbo.SDIFSArchive sdiFS on sdiFile.ID = sdiFS.SDIFileID
    WHERE uf.CommunityID = '7cc67de8-e5c2-4055-958b-f604c6a40cf1' 
    AND uf.OrganizationID='e5750df1-0409-46b6-9aba-7f07be7c890c'
) A

答案 1 :(得分:0)

如果你想在每一行中拥有331行,每次计数为113,你可以使用技巧。使用row_number()枚举具有相同值的行,然后使用窗口函数计算数字为1时的行:

SELECT t.*,
       SUM(CASE WHEN seqnum = 1 THEN 1 ELSE 0 END) OVER () as CountDistinct
FROM (SELECT uf.OrigFileName, uf.CreatedOn, sdiTran.Status, sdiFS.FileName, 
             sdiFile.ArchiveLogID,
             ROW_NUMBER() OVER (PARTITION BY uf.OrigFileName, uf.CreatedOn, sdiTran.Status, sdiFS.FileName, sdiFile.ArchiveLogID
                              ORDER BY (SELECT NULL)) as seqnum
      FROM [SDI].dbo.UploadedFile uf inner join
           [SDI].dbo.SDIFile sdiFile
           on uf.UploadedFullFileName = sdiFile.OriginalName left join
           [SDI].dbo.SDITransaction sdiTran
           on sdiFile.ID = sdiTran.SDIFileID inner join
           [SDI].dbo.SDIFSArchive sdiFS on sdiFile.ID = sdiFS.SDIFileID
      WHERE uf.CommunityID = '7cc67de8-e5c2-4055-958b-f604c6a40cf1' AND
            uf.OrganizationID='e5750df1-0409-46b6-9aba-7f07be7c890c'
     ) t;

如果您只想要记录不同的记录,可以使用COUNT(DISTINCT)。因为SQL Server不允许多个参数,所以子查询可能是最简单的方法:

SELECT COUNT(*)
FROM (SELECT DISTINCT uf.OrigFileName, uf.CreatedOn, sdiTran.Status, sdiFS.FileName, 
             sdiFile.ArchiveLogID
      FROM [SDI].dbo.UploadedFile uf inner join
           [SDI].dbo.SDIFile sdiFile
           on uf.UploadedFullFileName = sdiFile.OriginalName left join
           [SDI].dbo.SDITransaction sdiTran
           on sdiFile.ID = sdiTran.SDIFileID inner join
           [SDI].dbo.SDIFSArchive sdiFS on sdiFile.ID = sdiFS.SDIFileID
      WHERE uf.CommunityID = '7cc67de8-e5c2-4055-958b-f604c6a40cf1' AND
            uf.OrganizationID='e5750df1-0409-46b6-9aba-7f07be7c890c'
     ) t;

答案 2 :(得分:0)

  1. 在count()函数中使用distinct与唯一列,例如 select count(DISTINCT columnName)作为计数。并且不要使用over()子句 虽然得到了明显的计数。

      -
  2. 列表项

    SELECT  
    uf.OrigFileName ,
    uf.CreatedOn,
    sdiTran.Status,
    sdiFS.FileName,
    sdiFile.ArchiveLogID,
    COUNT(DISTINCT uf.OrigFileName) as totalRows
    FROM [SDI].dbo.UploadedFile uf 
    inner join [SDI].dbo.SDIFile sdiFile on uf.UploadedFullFileName = sdiFile.OriginalName 
    left join [SDI].dbo.SDITransaction sdiTran on sdiFile.ID = sdiTran.SDIFileID 
    inner join [SDI].dbo.SDIFSArchive sdiFS on sdiFile.ID = sdiFS.SDIFileID
    WHERE uf.CommunityID = '7cc67de8-e5c2-4055-958b-f604c6a40cf1' 
    AND uf.OrganizationID='e5750df1-0409-46b6-9aba-7f07be7c890c'