Monday, March 19, 2012

Combine 2 select statment result in one table

Hi All,
I am new in the Fourm.
I have a one Problem. I have a two select stament that are return 2-2 column that are in the differnent table.
I want that return in one table......

Ex I am writing 2 select statment below..
1. select count(product.ProductID) as productCount,AffiliateHits.affiliateID,product.Name as [Name],product.ProductID
from AffiliateHits,product
where AffiliateID like 'SELF'--@.affiliateID
and product.ProductID=AffiliateHits.ProductID
group by product.ProductID,AffiliateHits.affiliateID,product.Name

2.
select count(*)as OrderCount,orderdetail.productid from [order],OrderDetail
where [order].sourceID like @.affiliateID and
[order].addedon between @.fromDate AND @.toDate AND
[order].orderID=OrderDetail.orderid AND
OrderDetail.ProductID in (select distinct(productid)from AffiliateHits where AffiliateID like @.affiliateID)
group by orderdetail.ProductID

Plz Reply asap.

Thankx

Two ways...UNION ALL, or CROSS JOIN. UNION can be used to take two sets and combine them, column by column. So:

select column1
from table1

UNION ALL

select column2
from table2

This might produce a result like

column1
-
1
2

I don't think this is what you need. Cross join

select <columnNames>
from (select column1
from table1)
CROSS JOIN --or possibly join, if there is some relationship between the sets and multiple rows
( select column2
from table2)
--ON --if these are multi row sets (you might JOIN ON productId)

This might produce a set like (if each set returned one row)

column1 column2
--
1 2

I cant tell from your group by if either of these make sense, because your two sets have different group by clauses. Does this help? Can you simplify your example so one of us can make you a functional (with data) example?

No comments:

Post a Comment