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 Combine 2 different queries Into one table

Hi Guys ,

i have faced a sql command problem. That is currently i have 2 different table

a) Select ID, Name from Table1 where ID >=1 and ID ID <=3
ID | Name
1 | Alan
2 | Amy
3 | Andrew
b) Select ID,Sum(Points) as [Total] from Table2 where
ID >=1 and ID <=3
[No Record From ID > =1 until ID <=3]

My question is how to combine both queries so i can make my table result at below:
ID | Name | Total
1 | Alan | 0
2 | Amy | 0
3 | Andrew | 0

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

I think this is what you want. Hard to say without the table structures or an accurate description.

Select ID, Name,
(
  Select Sum(Points)
  From Table2
  Where Table2.ID = Table1.ID
) As Total
From Table1
Where ID >= 1 and ID <= 3
Order By ID
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

I think this is what you want. Hard to say without the table structures or an accurate description.

Select ID, Name,
(
  Select Sum(Points)
  From Table2
  Where Table2.ID = Table1.ID
) As Total
From Table1
Where ID >= 1 and ID <= 3
Order By ID

Thanks for your helping. I get your means and i can get what i want. Thank you very much

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

Please mark this thread as solved if you are happy with the solution.

Thanks

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

1.

SELECT pos_cashreceiptdetail.LocationCode,pos_cashreceiptpayment.TerminalCode,pos_cashreceiptpayment.ReceiptDate,
pos_cashreceiptdetail.Description,pos_cashreceiptpayment.ReceiptNo,SUM(TotalCost),SUM(TotalPrice)
FROM pos_cashreceiptdetail,pos_cashreceiptpayment
WHERE pos_cashreceiptdetail.Description LIKE 'Raw%'
AND pos_cashreceiptdetail.LocationCode = pos_cashreceiptpayment.LocationCode
AND pos_cashreceiptdetail.ReceiptDate = pos_cashreceiptpayment.ReceiptDate
AND pos_cashreceiptdetail.ReceiptDate BETWEEN '2010-10-06' AND '2010-10-20'
GROUP BY pos_cashreceiptpayment.ReceiptDate,pos_cashreceiptdetail.ReceiptNo


2.

SELECT pos_cashreceiptdetail.LocationCode,pos_cashreceiptpayment.TerminalCode,pos_cashreceiptpayment.ReceiptDate,
pos_cashreceiptdetail.ShortDescription,pos_cashreceiptpayment.ReceiptNo,SUM(Totalprice)
FROM pos_cashreceiptdetail,pos_cashreceiptpayment
WHERE pos_cashreceiptdetail.ShortDescription LIKE 'Food Stall%'
AND pos_cashreceiptdetail.ReceiptDate = pos_cashreceiptpayment.ReceiptDate
AND pos_cashreceiptdetail.ReceiptNo = pos_cashreceiptpayment.ReceiptNo
AND pos_cashreceiptdetail.ReceiptDate BETWEEN '2010-10-06' AND '2010-10-20'
GROUP BY pos_cashreceiptpayment.ReceiptDate


This my problem. How to combine this query? This query come from one same existing table. Please help me then...

Thanks in advance yeah.. cheers

derozza
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Hi a have 4 tables

Table A
ID | Data 1
---------------
1 | A

Table B
ID | Data 2
-----------------
1 | B
1 | C

Table C
ID | Data 3
----------------
1 | D
1 | E
1 | F

Table D
ID | Data 4
----------------
1 | G
1 | H

I want to have this result

Table Result
ID | Data 1 | Data 2 | Data 3 | Data 4
-----------------------------------------------------------------------
1 | A | B | D | G
| | C | E | H
| | | F |

Pls help.....

gennesis
Newbie Poster
14 posts since Jan 2012
Reputation Points: 8
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You