an ugly way of counting

  • select count(DISTINCT

    convert(nvarchar,DD.fk_intDocumentID)+ 'zzz' + convert(nvarchar,DD.fk_intUserID)

    )

    from dbo.tbl_DocumentDownloads DD

    where ......

    the above select statement counts the number of times a user downloads a document (not including repeat downloads of the same document, hence the DISTINCT keyword).

    the presense of the zzz in the above statement is to avoid an error due to the combination of two numbers eg combining 23 and 67 would be the same as combining 2 and 367 thus yielding an error

    all of this works fine but surely there must be a prettier way of doing this count????

    Edited by - eamonroche on 12/02/2003 05:17:53 AM

  • I would guess that by putting

    select DISTINCT DD.fk_intDocumentID ,DD.fk_intUserID

    from dbo.tbl_DocumentDownloads DD

    where ......

    in a view and than counting the number of records in the view would be cleaner and faster too

    Regards

    Kurt - just waiting on a reply on my topic 😉

    X


    X

  • Suggestion:

    select count(*) from

    (

    select 1 as Num

    from dbo.tbl_DocumentDownloads DD

    where ......

    group by

    DD.fk_intDocumentID,

    DD.fk_intUserID

    )as A

  • thanks davidt. works fine and looks more effiecient.

  • No Worries

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply