Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Help performing a count

Help performing a count

Scheduled Pinned Locked Moved Database
help
30 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AdamskiR

    Thanks for your reply. For example In the lookup table - DrugTherapy

    DTID(int) DrugTherapy(varchar)
    1 Drug1
    2 Drug2
    3 Drug3
    4 Drug4

    Drug referrals made - ReferralDrugTherapy

    DTRID(int) ReferralID(int) DTID(int)
    1 1 1
    2 1 1
    3 2 4
    4 2 3
    5 3 2
    6 4 2
    7 5 1

    Main Table CardiacReferrals

    ReferralID(int) Ward(int) Comments(varchar) DateSubmitted(datetime)
    1 1 test 01/01/2008
    2 2 test1 22/01/2008
    3 3 test2 01/02/2008
    4 1 test543 21/02/2008
    5 2 test45 01/02/2008
    6 3 testtgdf 25/02/2008
    7 9 testgdf 05/03/2008
    8 5 testggd 11/03/2008
    9 6 testfg 12/03/2008
    10 4 testgd 07/04/2008

    I have tried the following which works if there is values for the record, but if there isnt nothing is diplayed, what i want is for it to display 0 still if there is nothing there: SELECT DrugTherapy.DrugTherapy, COUNT(ReferralDrugTherapy.DTID) AS Expr1 FROM DrugTherapy INNER JOIN ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR (CardiacReferrals.DateSubmitted IS NULL) GROUP BY DrugTherapy.DrugTherapy Any help much appreciated. :)

    B Offline
    B Offline
    Blue_Boy
    wrote on last edited by
    #4

    COUNT(isnull(ReferralDrugTherapy.DTID,0))


    I Love T-SQL

    A 1 Reply Last reply
    0
    • B Blue_Boy

      COUNT(isnull(ReferralDrugTherapy.DTID,0))


      I Love T-SQL

      A Offline
      A Offline
      AdamskiR
      wrote on last edited by
      #5

      Thanks, but that hasn't worked either im afraid

      B 1 Reply Last reply
      0
      • A AdamskiR

        Thanks, but that hasn't worked either im afraid

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #6

        As I undestand when the table ReferralDrugTherapy have not records you got null value and instead null you want to display zero (0)?


        I Love T-SQL

        A 1 Reply Last reply
        0
        • B Blue_Boy

          As I undestand when the table ReferralDrugTherapy have not records you got null value and instead null you want to display zero (0)?


          I Love T-SQL

          A Offline
          A Offline
          AdamskiR
          wrote on last edited by
          #7

          Yes, so for example, i want to enter 2 range dates in say 01/01/2008 - 01/05/2008 and then to pull back for example

          Drug1 1
          Drug2 3
          Drug3 2
          Drug4 5

          At the moment if i put in a large date range it brings back the records because it has some, but if no record is found for say Drug3 it doesnt get pulled back i might get

          Drug1 1
          Drug4 5

          But what i would like is

          Drug1 1
          Drug2 0
          Drug3 0
          Drug4 5

          B 1 Reply Last reply
          0
          • A AdamskiR

            Yes, so for example, i want to enter 2 range dates in say 01/01/2008 - 01/05/2008 and then to pull back for example

            Drug1 1
            Drug2 3
            Drug3 2
            Drug4 5

            At the moment if i put in a large date range it brings back the records because it has some, but if no record is found for say Drug3 it doesnt get pulled back i might get

            Drug1 1
            Drug4 5

            But what i would like is

            Drug1 1
            Drug2 0
            Drug3 0
            Drug4 5

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #8

            select DrugTherapy.dtid,DrugTherapy.drugtherapy, (select count (dtid) from ReferralDrugTherapy where dtid = DrugTherapy.dtid) from DrugTherapy


            I Love T-SQL

            A 1 Reply Last reply
            0
            • B Blue_Boy

              select DrugTherapy.dtid,DrugTherapy.drugtherapy, (select count (dtid) from ReferralDrugTherapy where dtid = DrugTherapy.dtid) from DrugTherapy


              I Love T-SQL

              A Offline
              A Offline
              AdamskiR
              wrote on last edited by
              #9

              Im slightly confused now, did you mean to add that bit to replace the top like so? select DrugTherapy.dtid,DrugTherapy.drugtherapy, (select count (dtid) from ReferralDrugTherapy where dtid = DrugTherapy.dtid) from DrugTherapy INNER JOIN ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR (CardiacReferrals.DateSubmitted IS NULL) GROUP BY DrugTherapy.DrugTherapy Or something else? That query alone works, but how would i enter my date ranges still? Many thanks :)

              K 1 Reply Last reply
              0
              • A AdamskiR

                Im slightly confused now, did you mean to add that bit to replace the top like so? select DrugTherapy.dtid,DrugTherapy.drugtherapy, (select count (dtid) from ReferralDrugTherapy where dtid = DrugTherapy.dtid) from DrugTherapy INNER JOIN ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR (CardiacReferrals.DateSubmitted IS NULL) GROUP BY DrugTherapy.DrugTherapy Or something else? That query alone works, but how would i enter my date ranges still? Many thanks :)

                K Offline
                K Offline
                Kschuler
                wrote on last edited by
                #10

                How about if you try to make that first join an outer join, like this: SELECT DrugTherapy.DrugTherapy, COUNT(ReferralDrugTherapy.DTID) AS Expr1 FROM DrugTherapy LEFT OUTER JOIN ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR (CardiacReferrals.DateSubmitted IS NULL) GROUP BY DrugTherapy.DrugTherapy An inner join will join two tables together and return only the records where they match in the "ON" portion. A left outer join will join two tables and return the records where they match in the "ON" portion but will also return any records that didn't match in the table on the left of the "LEFT OUTER JOIN" statement. Be aware that this may bring back null values for fields that you reference in the table on the right side of the join. If you want to do some more research on this, I'd suggest searching for the keywords "OUTER JOIN SQL" on google. Hope this helps.

                A 1 Reply Last reply
                0
                • K Kschuler

                  How about if you try to make that first join an outer join, like this: SELECT DrugTherapy.DrugTherapy, COUNT(ReferralDrugTherapy.DTID) AS Expr1 FROM DrugTherapy LEFT OUTER JOIN ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR (CardiacReferrals.DateSubmitted IS NULL) GROUP BY DrugTherapy.DrugTherapy An inner join will join two tables together and return only the records where they match in the "ON" portion. A left outer join will join two tables and return the records where they match in the "ON" portion but will also return any records that didn't match in the table on the left of the "LEFT OUTER JOIN" statement. Be aware that this may bring back null values for fields that you reference in the table on the right side of the join. If you want to do some more research on this, I'd suggest searching for the keywords "OUTER JOIN SQL" on google. Hope this helps.

                  A Offline
                  A Offline
                  AdamskiR
                  wrote on last edited by
                  #11

                  That source didnt do the trick either :sigh: Ive been searching on this but not to much luck, theres plenty on joining the 2 tables but 3 isnt particularly good :confused:

                  K B 2 Replies Last reply
                  0
                  • A AdamskiR

                    That source didnt do the trick either :sigh: Ive been searching on this but not to much luck, theres plenty on joining the 2 tables but 3 isnt particularly good :confused:

                    K Offline
                    K Offline
                    Kschuler
                    wrote on last edited by
                    #12

                    I think I should have put a LEFT OUTER JOIN for both of the joins in your statement. Try that and see if it gets you anywhere.

                    1 Reply Last reply
                    0
                    • A AdamskiR

                      That source didnt do the trick either :sigh: Ive been searching on this but not to much luck, theres plenty on joining the 2 tables but 3 isnt particularly good :confused:

                      B Offline
                      B Offline
                      Blue_Boy
                      wrote on last edited by
                      #13

                      I think I got it (just change date values in according to your format string, instead yyyy-MM-dd set your format) SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN (SELECT TOP 1 t1.DTID FROM DrugTherapy AS t1 WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, dbo.DrugTherapy.DrugTherapy, COUNT(dbo.ReferralDrugTherapy.DTID) AS numbers FROM dbo.ReferralDrugTherapy FULL OUTER JOIN dbo.DrugTherapy ON dbo.ReferralDrugTherapy.DTID = dbo.DrugTherapy.DTID left outer join CardiacReferrals on dbo.ReferralDrugTherapy.referralid = CardiacReferrals.referralid and CardiacReferrals.datesubmitted between '2008-01-01' and '2008-02-21' GROUP BY dbo.ReferralDrugTherapy.DTID, dbo.DrugTherapy.DrugTherapy, dbo.DrugTherapy.DTID


                      I Love T-SQL

                      A 1 Reply Last reply
                      0
                      • B Blue_Boy

                        I think I got it (just change date values in according to your format string, instead yyyy-MM-dd set your format) SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN (SELECT TOP 1 t1.DTID FROM DrugTherapy AS t1 WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, dbo.DrugTherapy.DrugTherapy, COUNT(dbo.ReferralDrugTherapy.DTID) AS numbers FROM dbo.ReferralDrugTherapy FULL OUTER JOIN dbo.DrugTherapy ON dbo.ReferralDrugTherapy.DTID = dbo.DrugTherapy.DTID left outer join CardiacReferrals on dbo.ReferralDrugTherapy.referralid = CardiacReferrals.referralid and CardiacReferrals.datesubmitted between '2008-01-01' and '2008-02-21' GROUP BY dbo.ReferralDrugTherapy.DTID, dbo.DrugTherapy.DrugTherapy, dbo.DrugTherapy.DTID


                        I Love T-SQL

                        A Offline
                        A Offline
                        AdamskiR
                        wrote on last edited by
                        #14

                        Not quite, it doesnt seem to matter what date range you put it, it always pulls back all the results for each drug type. I tried changing it slightly so the dates in a where to SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN (SELECT TOP 1 t1.DTID FROM DrugTherapy AS t1 WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, DrugTherapy.DrugTherapy, COUNT(ReferralDrugTherapy.DTID) AS numbers FROM ReferralDrugTherapy FULL OUTER JOIN DrugTherapy ON ReferralDrugTherapy.DTID = DrugTherapy.DTID LEFT OUTER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) GROUP BY ReferralDrugTherapy.DTID, DrugTherapy.DrugTherapy, DrugTherapy.DTID but all that did was give me the same problem where if it doesnt find anything it brings back nothing instead of 0 values. Argh! :wtf:

                        B 1 Reply Last reply
                        0
                        • A AdamskiR

                          Not quite, it doesnt seem to matter what date range you put it, it always pulls back all the results for each drug type. I tried changing it slightly so the dates in a where to SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN (SELECT TOP 1 t1.DTID FROM DrugTherapy AS t1 WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, DrugTherapy.DrugTherapy, COUNT(ReferralDrugTherapy.DTID) AS numbers FROM ReferralDrugTherapy FULL OUTER JOIN DrugTherapy ON ReferralDrugTherapy.DTID = DrugTherapy.DTID LEFT OUTER JOIN CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) GROUP BY ReferralDrugTherapy.DTID, DrugTherapy.DrugTherapy, DrugTherapy.DTID but all that did was give me the same problem where if it doesnt find anything it brings back nothing instead of 0 values. Argh! :wtf:

                          B Offline
                          B Offline
                          Blue_Boy
                          wrote on last edited by
                          #15

                          write date values which you are giving?


                          I Love T-SQL

                          A 1 Reply Last reply
                          0
                          • B Blue_Boy

                            write date values which you are giving?


                            I Love T-SQL

                            A Offline
                            A Offline
                            AdamskiR
                            wrote on last edited by
                            #16

                            Well say if i put in 01/01/2008 - 01/05/2008 it pulls back records as there are ones within that range. (But also will not display those that dont have anything as 0, just wont display) But say i put in 01/05/2008 - 02/05/2008 then it pulls back nothing

                            B 1 Reply Last reply
                            0
                            • A AdamskiR

                              Well say if i put in 01/01/2008 - 01/05/2008 it pulls back records as there are ones within that range. (But also will not display those that dont have anything as 0, just wont display) But say i put in 01/05/2008 - 02/05/2008 then it pulls back nothing

                              B Offline
                              B Offline
                              Blue_Boy
                              wrote on last edited by
                              #17

                              between dates 01/05/2008 - 02/05/2008 you need this result : DTID DrugTherapy CountNumbers 2 Drug2 0 3 Drug3 0 1 Drug1 5 4 Drug4 1


                              I Love T-SQL

                              A 1 Reply Last reply
                              0
                              • B Blue_Boy

                                between dates 01/05/2008 - 02/05/2008 you need this result : DTID DrugTherapy CountNumbers 2 Drug2 0 3 Drug3 0 1 Drug1 5 4 Drug4 1


                                I Love T-SQL

                                A Offline
                                A Offline
                                AdamskiR
                                wrote on last edited by
                                #18

                                Yes, so if there is none made, it to display a 0 so i can display reports on a page. Thanks

                                B 1 Reply Last reply
                                0
                                • A AdamskiR

                                  Yes, so if there is none made, it to display a 0 so i can display reports on a page. Thanks

                                  B Offline
                                  B Offline
                                  Blue_Boy
                                  wrote on last edited by
                                  #19

                                  change date format to dd/MM/yyyy from regoinal Settings on control panel, then on date parameters ad values like this '01/01/2008' - '02/01/2008' by that changes,query works perfeclty for me here it is SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN (SELECT TOP 1 t1.DTID FROM DrugTherapy AS t1 WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, dbo.DrugTherapy.DrugTherapy, COUNT(dbo.ReferralDrugTherapy.DTID) AS numbers FROM dbo.ReferralDrugTherapy FULL OUTER JOIN dbo.DrugTherapy ON dbo.ReferralDrugTherapy.DTID = dbo.DrugTherapy.DTID left outer join CardiacReferrals on dbo.ReferralDrugTherapy.referralid = CardiacReferrals.referralid and CardiacReferrals.datesubmitted between '01/01/2008' and '01/05/2008' GROUP BY dbo.ReferralDrugTherapy.DTID, dbo.DrugTherapy.DrugTherapy, dbo.DrugTherapy.DTID


                                  I Love T-SQL

                                  A 1 Reply Last reply
                                  0
                                  • B Blue_Boy

                                    change date format to dd/MM/yyyy from regoinal Settings on control panel, then on date parameters ad values like this '01/01/2008' - '02/01/2008' by that changes,query works perfeclty for me here it is SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN (SELECT TOP 1 t1.DTID FROM DrugTherapy AS t1 WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, dbo.DrugTherapy.DrugTherapy, COUNT(dbo.ReferralDrugTherapy.DTID) AS numbers FROM dbo.ReferralDrugTherapy FULL OUTER JOIN dbo.DrugTherapy ON dbo.ReferralDrugTherapy.DTID = dbo.DrugTherapy.DTID left outer join CardiacReferrals on dbo.ReferralDrugTherapy.referralid = CardiacReferrals.referralid and CardiacReferrals.datesubmitted between '01/01/2008' and '01/05/2008' GROUP BY dbo.ReferralDrugTherapy.DTID, dbo.DrugTherapy.DrugTherapy, dbo.DrugTherapy.DTID


                                    I Love T-SQL

                                    A Offline
                                    A Offline
                                    AdamskiR
                                    wrote on last edited by
                                    #20

                                    My date settings are already set to that. The query that you have given me also just gives the total made, no matter what dates you enter you always get the total, it doesnt calculate any differences. Thanks for your help so far!

                                    B 1 Reply Last reply
                                    0
                                    • A AdamskiR

                                      My date settings are already set to that. The query that you have given me also just gives the total made, no matter what dates you enter you always get the total, it doesnt calculate any differences. Thanks for your help so far!

                                      B Offline
                                      B Offline
                                      Blue_Boy
                                      wrote on last edited by
                                      #21

                                      I will lookup later again and I will notify you


                                      I Love T-SQL

                                      A 1 Reply Last reply
                                      0
                                      • B Blue_Boy

                                        I will lookup later again and I will notify you


                                        I Love T-SQL

                                        A Offline
                                        A Offline
                                        AdamskiR
                                        wrote on last edited by
                                        #22

                                        Many thanks, if i crack it in the mean time without blowing my head off ill let you know!

                                        B 2 Replies Last reply
                                        0
                                        • A AdamskiR

                                          Many thanks, if i crack it in the mean time without blowing my head off ill let you know!

                                          B Offline
                                          B Offline
                                          Blue_Boy
                                          wrote on last edited by
                                          #23

                                          ok, if I crack it too I will tell you. On my mind i have solution but I must seriozly deal with it, coz until now I didn't have time to deal with it seriozly. See u later.


                                          I Love T-SQL

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups