+ Reply to Thread
Results 1 to 2 of 2

Thread: Nested Selected issue

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    45

    Nested Selected issue

    I have trouble getting result using that Nested select Query :
    Code:
    SELECT l.*,
    Credit = (
    CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
      sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
     ELSE
     sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4) 
     END 
    )
    FROM live l INNER JOIN master m on m.ClientID = L.ClientID 
    WHERE ClientID = 12345
    And also that query :
    Code:
    SELECT l.*,
    (SELECT
    CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
      sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
     ELSE
     sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4) 
     END 
    ) AS Credit
    FROM live l INNER JOIN master m on m.ClientID = L.ClientID 
    WHERE ClientID = 12345

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    40
    You need to add some selects before getting the sum of your sub-queries :
    Code:
    SELECT l.*,
    Credit = (
       CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
         (SELECT sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4))
       ELSE
         (SELECT sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4))
       END 
    ) 
    FROM live l 
        INNER JOIN master m on m.ClientID = L.ClientID 
    WHERE ClientID = 12345

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts