Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I have two tables.First table has two rows but different products for perticular invoice.
second table has single entry for that invoice for debit or credit entry.


so i want result both rows from first table and single data row(column) from second table.


InvoiceNum PartNum Credit Debit
154788 CF-000005 0 326
154788 CF-000002


how?

[Added from comment]
First Table
______________________________
InvoiceNum.     PartNum
154788	        CF-000005
154788          CF-000002


_____________________
Second Table
ARInvoiceNum.   BookCreditAmount.  BookDebitAmount
154788.         0.000.             326.000


What I have tried:

select InvoiceNum,PartNum,(select top 1 BookCreditAmount ),(select top 1 BookDebitAmount )
from GLJrnDtl h left join InvcDtl d on h.ARInvoiceNum=d.InvoiceNum
where h.ARInvoiceNum=154788 and SegValue1=46804
Posted
Updated 16-Feb-16 20:37pm
v2
Comments
Tomas Takac 17-Feb-16 4:11am    
It doesn't make much sense to me. Why do you need this?
dharan1990 17-Feb-16 4:17am    
Can you please try applying row number on both the tables and apply any of the outer join based on those row number which should resolve.

You can try the below code it should work for your expected output.
select A.inv,a.partnum,isnull(b.Credit,0) as 'Credit',isnull(b.debit,0) as debit 
from(
    SELECT inv,partnum,row_number() over (partition by inv order by inv) as row_num
    FROM #temp1)A
left join
    (SELECT inv,Credit,debit,row_number() over (partition by inv order by inv) as row_num
    FROM #temp2)B
on  A.row_num=B.row_num and a.inv=b.inv
ORDER BY A.inv
 
Share this answer
 
when subselecting: (select top 1 BookCreditAmount ),(select top 1 BookDebitAmount )
You will always get nothing. There is NO FROM clause.

So your query should look something like:
SQL
select h.InvoiceNum,h.PartNum, d.BookCreditAmount, d.BookDebitAmount 
from GLJrnDtl as h 
left join InvcDtl d on 
h.ARInvoiceNum=d.InvoiceNum
where h.ARInvoiceNum=154788 
and h.SegValue1=46804

That leaves you with the point that any line > 1 will show the amounts too.
So you need to check on Row_NUMBER() thus query might be:
SQL
select InvoiceNum, PartNum, BookCreditAmount, BookDebitAmount
FROM 
(
	select ROW_NUMBER() OVER (ORDER BY h.ARInvoiceNum) AS ROWNR,
		h.InvoiceNum,h.PartNum, d.BookCreditAmount, d.BookDebitAmount 
	from GLJrnDtl as h 
	left join InvcDtl d on 	h.ARInvoiceNum=d.InvoiceNum
	
) as row1
WHERE rownr=1
and row1.ARInvoiceNum=154788 
	and row1.SegValue1=46804
UNION
select InvoiceNum, PartNum, NULL, NULL
FROM 
(
	select ROW_NUMBER() OVER (ORDER BY h.ARInvoiceNum) AS ROWNR,
		h.InvoiceNum,h.PartNum, d.BookCreditAmount, d.BookDebitAmount 
	from GLJrnDtl as h 
	left join InvcDtl d on 	h.ARInvoiceNum=d.InvoiceNum
) as rowOther
where rownr > 1 and rowOther.ARInvoiceNum=154788 
and rowOther.SegValue1=46804
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900