954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How To use Intersect

Hi Guys,

I have some confusing part in the sql command

the question is
i have 2 different query :
a)

Select SUM(T1.Amount+T2.Amount) as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID

b)

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?

nokomoli
Light Poster
26 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 
select sum(Total)
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select Amount as Total FROM TABLE3 
    ) X
huangzhi
Light Poster
48 posts since Feb 2008
Reputation Points: 10
Solved Threads: 13
 

HI Huang Zhi,

Thanks your help you.
This statement is work!!!

nokomoli
Light Poster
26 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 
select sum(Total)
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select Amount as Total FROM TABLE3 
    ) 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 :

select sum(Total) as [Amount]
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select Amount as Total FROM TABLE3 
    ) X
Where Amount is not null

That is because i don't want to view a result with result is null value.

nokomoli
Light Poster
26 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Use this command

select sum(Total) as [Amount]
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select Amount as Total FROM TABLE3 Where Amount is not null
    ) X


or

select sum(Total) as [Amount]
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select isNull(Amount, 0) as Total FROM TABLE3    
  ) X
huangzhi
Light Poster
48 posts since Feb 2008
Reputation Points: 10
Solved Threads: 13
 
select sum(Total)
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select Amount as Total FROM TABLE3 
    ) X


Thanks Help but i think u misunderstood my question. anyway i had found out the answer

select sum(Total) as [Total Amount]
  from 
    (
    select T1.Amount+T2.Amount as Total From TABLE1 T1 INNER TABLE2 T2 ON T1.ID = T2.ID
    union all
    select Amount as Total FROM TABLE3 
    ) X Having (sum(Total)> 0)
nokomoli
Light Poster
26 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You