Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combine two query ?

Author  Topic 

usafelix
Posting Yak Master

165 Posts

Posted - 2015-02-10 : 05:52:54
who can help me write a query can combine two statement of result ?
I am tried to combine it but happened too many repeat trx_no.
----------------------------------------------------------------
select distinct detail.trx_no, sum(detail.redemption_amt) from detail group by trx_no

select distinct header.trx_no , header.sales_total_amt from header
group by trx_no

----------------------------------------------------------------
I expect output is below :-
trx_no , sales_total_amt, detail.redemption_amt
sales_1 $50 $30

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2015-02-10 : 08:42:49
What did you try?Assuming there is only one row for each trx_no in the header table,
SELECT
h.trx_no,
h.sales_total_amt,
SUM(d.redemption_amt) AS redemption_amt
FROM
header h
INNER JOIN detail d ON
d.trx_no = h.trx_no
GROUP BY
h.trx_no,
h.sales_total_amt;
Go to Top of Page

usafelix
Posting Yak Master

165 Posts

Posted - 2015-02-10 : 09:54:53
my expect result is below , pls give one query for below :=

sales header
trx_number = S0001
trx_number = S0002

Sales Detail
trx_number = s0001
item_1 $100
item_2 $200
item_3 $300
trx_number = s0002
item 4 $1000
item 5 $1000
I want the result is below :---
Sales number sales amount
trx_no = s0001 $600
trx_no = s0002 $2000
Go to Top of Page
   

- Advertisement -