• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

T-SQL question

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    T-SQL question

    Hi guys

    I have a view (A) looking into another view (B) and counting the number of records in (B) and reporting the number of rows in (B) in (A).
    when there are 2 records in (B) the output of the view is as follows

    Column1 | Column2 | Column3 | Column4
    something|something| 2|something

    when there are 0 records in (B) want the output as
    Column1 | Column2 | Column3 | Column4
    something|something| 0|something

    but I just get an empty result screen in MS SQL Server Management Studio

    do you guys need something I could add to the T-SQL of view (A) that would write the output columns with a 0 in Column3 if there are no records in view (A)?
    "Condoms should come with a free pack of earplugs."

    #2
    Have you tried left joins & the IsNull function ?
    How did this happen? Who's to blame? Well certainly there are those more responsible than others, and they will be held accountable, but again truth be told, if you're looking for the guilty, you need only look into a mirror.

    Follow me on Twitter - LinkedIn Profile - The HAB blog - New Blog: Mad Cameron
    Xeno points: +5 - Asperger rating: 36 - Paranoid Schizophrenic rating: 44%

    "We hang the petty thieves and appoint the great ones to high office" - Aesop

    Comment


      #3
      Originally posted by HairyArsedBloke View Post
      Have you tried left joins & the IsNull function ?
      I am trying the ISNULL function now. I will tell you the result in circa 15 min as that is how long this query takes.
      "Condoms should come with a free pack of earplugs."

      Comment


        #4
        Remember to alter the join too or you'll still get nowt!

        Comment


          #5
          Use a case statement with an isnull and the count. That way you can control the count and return a 0 if the join is null
          What happens in General, stays in General.
          You know what they say about assumptions!

          Comment


            #6
            Originally posted by Durbs View Post
            Remember to alter the join too or you'll still get nowt!
            thanks guys - it works now.

            where do I send the beers?
            "Condoms should come with a free pack of earplugs."

            Comment


              #7
              Originally posted by Durbs View Post
              Remember to alter the join too or you'll still get nowt!
              how do you know what join I have?

              I am very impressed
              "Condoms should come with a free pack of earplugs."

              Comment


                #8
                Originally posted by ThomasSoerensen View Post
                how do you know what join I have?
                Its chilly out here on your balcony, any chance of passing some cheese on toast out (dash of Lea & Perrins).

                Cos if you'd used a left join originally your result would have been

                Column1 | Column2 | Column3 | Column4
                something|something| NULL|something

                But as you said you were getting a blank query window it suggested you were using an INNER which would return nothing instead of a NULL.
                Last edited by Durbs; 16 February 2010, 20:55.

                Comment


                  #9
                  Originally posted by Durbs View Post
                  Its chilly out here on your balcony, any chance of passing some cheese on toast out (dash of Lea & Perrins).

                  Cos if you'd used a left join originally your result would have been

                  Column1 | Column2 | Column3 | Column4
                  something|something| NULL|something

                  But as you said you were getting a blank query window it suggested you were using an INNER which would return nothing instead of a NULL.
                  Geek!!!!!
                  Very impressed.
                  "Condoms should come with a free pack of earplugs."

                  Comment


                    #10
                    Originally posted by ThomasSoerensen View Post
                    I am trying the ISNULL function now. I will tell you the result in circa 15 min as that is how long this query takes.
                    15 minutes!!!!!!

                    I hope your query is processing a few 10's of million rows or the server is extremely under specced the only other option is that you've written some really bad SQL
                    Coffee's for closers

                    Comment

                    Working...
                    X