473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Attach 2 Tables (Complex Joins)

Mahdis
7
Hi,

I have a small problem of the following Tables, I have MyTable1 (for example) and I want to attach content of following MyTable2 to MyTable1, but MyTable2 have the Data just 1 week ago, and it should be in appropriate columns against that columns in MyTable1, pl tel me what can I do?


MyTable 1 :

Id_Date Id_Branch NewBalance
07/08/2012 1270 160000
07/08/2012 1654 170000
07/08/2012 3046 160000
---------------------------------------
11/08/2012 2046 140000
11/08/2012 4395 170000
11/08/2012 1720 160000

MyTable 2 :

Id_Date OldBalance
18/08/2012 140000
18/08/2012 170000
18/08/2012 160000

It should be some thing like this(MyTable 3) :


Id_Date Id_Branch NewBalance OldBalance
07/08/2012 1270 160000 -
07/08/2012 1654 170000 -
07/08/2012 3046 160000 -
---------------------------------------------------
11/08/2012 2046 140000 160000
11/08/2012 4395 170000 170000
11/08/2012 1720 160000 160000
---------------------------------------------------
18/08/2012 3046 110000 140000
18/08/2012 3046 160000 170000
18/08/2012 3046 150000 160000


Thanks a lot.....
Nov 29 '12 #1
8 1955
Rabbit
12,516 Expert Mod 8TB
You have duplicate dates. How do you know which one to join to?
Nov 29 '12 #2
Mahdis
7
@Rabbit
Oh Sorry I’d forgotten to explain that, MyTable 1 have some other Columns too, and with them makes primary key(like Id_Code+IdCountry) and also, weekly, new data is added to and NewBalance is changed to OldBalance for new week, so I should Show OldBalance, next to the Id_date, Id_Branch, Id_Code, IdCountry and NewBalance to GridView in asp.net
Nov 29 '12 #3
Rabbit
12,516 Expert Mod 8TB
So table 1 has a primary key composed of id_code and idcountry. Does table 2 have the same thing?

If so, outer join the two tables together and you will get the result you want.
Nov 29 '12 #4
Mahdis
7
@Rabbit
No, as a matter of fact I should say MyTable 2 comes out of the MyTAble 1 after every week, that means, after 1 week, actually I have new data like third Table as shown(MyTable3), that should be added to end of MyTable 1(for example named MyTable 2) and NewBalance should be changed to OldBalance with new rows, it means in real I haven’t MyTable2 and when I’d wanted to solve it I’d supposed that (maybe it is wrong because I ‘m beginner in Sql Server)


MyTable 1 :

Id_Date Id_Branch NewBalance
--------------------------------------
07/08/2012 1270 160000
07/08/2012 1654 170000
07/08/2012 3046 160000
---------------------------------------
11/08/2012 2046 140000
11/08/2012 4395 170000
11/08/2012 1720 160000

MyTable 2(New Week) :

Id_Date OldBalance
--------------------------------
18/08/2012 140000
18/08/2012 170000
18/08/2012 160000

It should be some thing like this(MyTable 3) :


Id_Date Id_Branch NewBalance OldBalance
---------------------------------------------------
07/08/2012 1270 160000 -
07/08/2012 1654 170000 -
07/08/2012 3046 160000 -
---------------------------------------------------
11/08/2012 2046 140000 160000
11/08/2012 4395 170000 170000
11/08/2012 1720 160000 160000
---------------------------------------------------
18/08/2012 3046 110000 140000
18/08/2012 3046 160000 170000
18/08/2012 3046 150000 160000
Nov 29 '12 #5
Rabbit
12,516 Expert Mod 8TB
I have no idea what you just said.
Nov 29 '12 #6
Mahdis
7
@Rabbit
So When I have a table that every week should be updated in part of table (just some columns) that results from 1 week ago, so what should I do, could u pl just tell me that? Thank u so much.....


Id_date Id_Branch NewBalance($) OldBalance($)
07/08/2012 2014 140000
07/08/2012 1354 120000
07/08/2012 1014 170000
---------------------------------------------------------
11/08/2012 2014 180000 140000
11/08/2012 1354 110000 120000
11/08/2012 1014 150000 170000
---------------------------------------------------------
18/08/2012 2014 170000 180000
18/08/2012 1354 130000 110000
18/08/2012 1014 190000 150000
Nov 29 '12 #7
ck9663
2,878 Expert 2GB
Are Id_Branch and Id_Date unique? Do you have the dates exactly a week apart even across different months?


~~ CK
Nov 30 '12 #8
Rabbit
12,516 Expert Mod 8TB
In addition to those questions, is each new file always linked to the most recent date in the table?
Nov 30 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Greg Gillis | last post by:
I need some help. Here is the problem. Table_1 (gl_Train_KeyIdeas) +----------------------+----------------+-------------------+---------------+ | KeyIdea_ID | Unit_ID | Group_ID | ...
5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
4
by: Sri | last post by:
I am writing a download process in which i have a condition where i need to join four tables. Each table have lot of data say around 300000 recs. my question is when i am doing the joins on...
1
by: Prem | last post by:
Hi All Database Gurus, I am trying to write code which will produce all the possible valid queries, given tables and join information for tables. Right now i am just trying to construct all the...
0
by: Alpenk | last post by:
I'm trying to update an empty field in a table using the update statment. The table has a number of joins and the update statement comes up with an error when I enter the main table to update. should...
0
by: hotgazpacho | last post by:
I have a query where I need to join several tables, one of which is variable (otherwise I'd just hand-code the query). The main table is called media, and has fields common to all media in my...
10
by: Philip Sherman | last post by:
SELECT columns FROM t1 JOIN t2
4
by: gillianbrooks91 | last post by:
Forgive me for asking this question, I've trawled through nearly every available post on this subject that I can find for a few weeks now but nothing quite points me in the right direction. I'm...
1
by: Ana RM | last post by:
Mark.Powell@eds.com (Mark D Powell) wrote in message news:<2687bb95.0308010642.1fc4ff1f@posting.google.com>... Hi Mark, Thanks por answer me. I do not think it is important thw warehouse...
6
tuxalot
by: tuxalot | last post by:
I've tested many ways to accomplish this, but none of the code I've found works quite right. I am using the code written by Dev Ashish but it replicates some of the functionality I already have. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.