How To use Intersect

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 26
Reputation: nokomoli is an unknown quantity at this point 
Solved Threads: 0
nokomoli nokomoli is offline Offline
Light Poster

How To use Intersect

 
0
  #1
Nov 13th, 2008
Hi Guys,

I have some confusing part in the sql command

the question is
i have 2 different query :
a)
  1. SELECT SUM(T1.Amount+T2.Amount) as Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
b)
  1. SELECT Sum(Amount) as Total FROM TABLE3

the result if a) query is 100 and result of b) query is 50
But i wanna show the result is 150 (That means add query a) result and query b result.)
So what should i do?

I use INTERSECT to combine both query, but it shows "syntax error"
have any one to help me?
Last edited by peter_budo; Nov 13th, 2008 at 7:46 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster

Re: How To use Intersect

 
0
  #2
Nov 13th, 2008
  1. SELECT sum(Total)
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount AS Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. union ALL
  6. SELECT Amount AS Total FROM TABLE3
  7. ) X
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: nokomoli is an unknown quantity at this point 
Solved Threads: 0
nokomoli nokomoli is offline Offline
Light Poster

Re: How To use Intersect

 
0
  #3
Nov 13th, 2008
HI Huang Zhi,

Thanks your help you.
This statement is work!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: nokomoli is an unknown quantity at this point 
Solved Threads: 0
nokomoli nokomoli is offline Offline
Light Poster

Re: How To use Intersect

 
0
  #4
Nov 14th, 2008
Originally Posted by huangzhi View Post
  1. SELECT sum(Total)
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount AS Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. union ALL
  6. SELECT Amount AS Total FROM TABLE3
  7. ) X
Really thanks for ur help.
But Now i have new question about this
refered by to ur answer, if let say i wanna to add condition inside this statement, is it possible?
cox when i try :
  1. SELECT sum(Total) as [Amount]
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount as Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. UNION all
  6. SELECT Amount as Total FROM TABLE3
  7. ) X
  8. WHERE Amount IS NOT NULL

That is because i don't want to view a result with result is null value.
Last edited by peter_budo; Nov 17th, 2008 at 3:01 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster

Re: How To use Intersect

 
0
  #5
Nov 15th, 2008
Use this command
  1. SELECT sum(Total) AS [Amount]
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount AS Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. union ALL
  6. SELECT Amount AS Total FROM TABLE3 WHERE Amount IS NOT NULL
  7. ) X

or

  1. SELECT sum(Total) AS [Amount]
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount AS Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. union ALL
  6. SELECT isNull(Amount, 0) AS Total FROM TABLE3
  7. ) X
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: nokomoli is an unknown quantity at this point 
Solved Threads: 0
nokomoli nokomoli is offline Offline
Light Poster

Re: How To use Intersect

 
0
  #6
Nov 17th, 2008
Originally Posted by huangzhi View Post
  1. SELECT sum(Total)
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount AS Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. union ALL
  6. SELECT Amount AS Total FROM TABLE3
  7. ) X
Thanks Help but i think u misunderstood my question. anyway i had found out the answer
  1. SELECT sum(Total) as [Total Amount]
  2. FROM
  3. (
  4. SELECT T1.Amount+T2.Amount as Total FROM TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
  5. UNION all
  6. SELECT Amount as Total FROM TABLE3
  7. ) X HAVING (sum(Total)> 0)
Last edited by peter_budo; Nov 18th, 2008 at 6:27 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC