SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to combine two tables when one table is in different database and this table will determine the...


How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table.

Author
Message
marksquall
marksquall
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 206
Dear members,
Hello, a pleasant day to everyone.
I would want to ask help about this query that I need for reporting.
Let’s for example I have a database named DBMain, and I want to combine two tables by means of their key named InvTrackNo, eventually here is the query:

SELECT
A.ConsInvNumber AS [Consolidated Invoice No],
A.InvTrackNo AS [Invoice TrackNo],
B.SaleOrderNo AS [Sales OrderNo],
B.OrderLine AS [Sales Order LineNo],
A.CtrlNo

FROM DBMain.dbo.ConsInvoiceDetail AS A INNER JOIN
DBMain.dbo.IndInvDetail AS B ON A.InvTrackNo = B.InvTrackNo
WHERE A.ConsInvNumber=980029768



And here is the output of my main query:

Consolidated InvoiceNo Invoice TrackNo Sales OrderNo Sales Order LineNo CtrlNo
980029768 0901164982 162194751 100 407633405
980029768 0901164982 162194751 200 407633405
980029768 0901164982 162194751 100 407633405
980029768 0901164982 162194751 200 407633405
980029768 0901192336 161891209 40 0
980029768 0901192337 161891209 100 0
980029768 0901192338 161891209 160 0
980029768 0901204388 162044081 100 406488527
980029768 0901204389 162044081 200 406488589


I noticed that some Control Number (A.CtrlNo) are zeros, my superior said I should look for the archive table and somehow “combine” them to this first query.

I tried to query first the archive table named OrderSummaryArchive that is located in a different database named DBArchive:


SELECT CtrlNo,
SalesOrderNo,
SalesOrderLineNo
FROM OrderSummaryArchive
WHERE SalesOrderNo = 161891209



And here is the output of my second query:

CtrlNo SalesOrderNo SalesOrderLineNo
405290666 161891209 20
405290666 161891209 40
405290669 161891209 60
405290669 161891209 80
405290669 161891209 100
405290674 161891209 120
405290674 161891209 140
405290674 161891209 160



As you can see, the highlighted result will complete my first query, I just do not how how to combine them if the second table is in another database, and how to check the appropriate CtrlNo for each record.


My expected result would be:


ConsInvoiceNo InvoiceTrackNo Sales OrderNo Sales Order LineNo CtrlNo CameFromArchive
980029768 0901164982 162194751 100 407633405 0
980029768 0901164982 162194751 200 407633405 0
980029768 0901164982 162194751 100 407633405 0
980029768 0901164982 162194751 200 407633405 0
980029768 0901192336 161891209 40 405290666 1
980029768 0901192337 161891209 100 405290669 1
980029768 0901192338 161891209 160 405290674 1
980029768 0901204388 162044081 100 406488527 0
980029768 0901204389 162044081 200 406488589 0



The report should include an additional column called CameFromArchive so that the database administrator would determine what CtrlNo came from the archive table (0-NO 1-YES).


I hope someone could help me fix my first query. Thank you.


Respectfully Yours,

Mark Squall

________________________________
"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26352 Visits: 17556
This is somewhat of a shot in the dark because we don't have ddl or sample data to work with.


SELECT
A.ConsInvNumber AS [Consolidated Invoice No],
A.InvTrackNo AS [Invoice TrackNo],
B.SaleOrderNo AS [Sales OrderNo],
B.OrderLine AS [Sales Order LineNo],
case when A.CtrlNo = 0 then osa.CtrlNo else a.CtrlNo end as CtrlNo
FROM DBMain.dbo.ConsInvoiceDetail AS A INNER JOIN
DBMain.dbo.IndInvDetail AS B ON A.InvTrackNo = B.InvTrackNo
Left Join (select top 1 CtrlNo from DBArchive.dbo.OrderSummaryArchive where SalesOrderNo = B.SaleOrderNo) osa
WHERE A.ConsInvNumber=980029768



I would suggest that when aliasing your tables you don't use A, B, C. Instead use some sort of abbreviation of the table name. It makes debugging a lot easier and anybody looking at your code doesn't have to scratch out their eyeballs trying to figure out which table certain columns come from.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
marksquall
marksquall
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 206
Hello Sean. Thank you for the sample query, it works just fine (with some minor modifications). Awesome! :-)
I apologize to you (and to the readers) for making my aliases so short, actually I am just in a "hurry" that is why I used short aliases. In my query here, I (really) used full table abbreviation. (silly me). Crazy w00t :-D

Just one last thing, how would I add an additional column called CameFromArchive so that the database administrator would determine what CtrlNo came from the archive table (0-NO 1-YES)?

Thank you so much and more power!


Respectfully Yours,

Mark Squall

________________________________
"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20
Geomorian
Geomorian
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 223
marksquall (1/14/2013Just one last thing, how would I add an additional column called CameFromArchive so that the database administrator would determine what CtrlNo came from the archive table (0-NO 1-YES)?


I think you can use a case just like the one that was allready included in the query posted by Sean, like so :

SELECT
A.ConsInvNumber AS [Consolidated Invoice No],
A.InvTrackNo AS [Invoice TrackNo],
B.SaleOrderNo AS [Sales OrderNo],
B.OrderLine AS [Sales Order LineNo],
case when A.CtrlNo = 0 then osa.CtrlNo else a.CtrlNo end as CtrlNo,
case when OSA.CtrlNo is NULL then 'No' else 'Yes' end as CameFromArchive
FROM DBMain.dbo.ConsInvoiceDetail AS A INNER JOIN
DBMain.dbo.IndInvDetail AS B ON A.InvTrackNo = B.InvTrackNo
Left Join (select top 1 CtrlNo from DBArchive.dbo.OrderSummaryArchive where SalesOrderNo = B.SaleOrderNo) osa
WHERE A.ConsInvNumber=980029768



EDIT: forgot a comma

MCSA, MCITP
SQL Server / Windows Server (2008)
marksquall
marksquall
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 206
Hello Geomorian. That was really cool. I am thinking it has be another technique, like for example, using a variable (BIT type) to set to either 1 or 0, then "convert" it to "YES" or "NO". (I guess I am just making my life too hard, silly me.). But for the record, is this possible? If yes, can someone give a little example?

Well anyway, this is straight to the point, and it is awesome! Cool

Thank you Geomorian and Sean Lange. :-)

Warm regards,

Mark Squall

________________________________
"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search