I have three tables (table1, table2, table3).
In table1 I have different values (some of them are same but it's ok), in table2 I also have different values (some of them are same but it's ok).
Now I want to create a function which is going to sum the same values of table1 sum the values from table2 and do a diff between the same values and insert that value in a specific column of table3.
table1:
KodikosBarcode total
1 14
2 18
table2:
KodikosBarcode total
1 1
2 2
table3 should have to be like this
KodikosBarcode total
1 13
2 16
Can someone help?
then table3 will have
table3:
KodikosBarcode total
1 13
2 16
3 1
Like this? (If MSSQL) but hopefully you get the idea anyway...
INSERT Table3
(SomeColumn)
SELECT
(SELECT Sum(SomeColumnFromTable1) FROM Table1) -
(SELECT Sum(SomeColumnFromTable2) FROM Table2)
To update you would do a similar thing;
UPDATE Table3
SET SomeColumn = (SELECT
(SELECT Sum(SomeColumnFromTable1) FROM Table1) -
(SELECT Sum(SomeColumnFromTable2) FROM Table2))
WHERE (whatever row you want to update... you didn't tell us)
for SQL Server:
insert into table 3
(
KodikosBarcode
, Diff_Value
)
select
t1.KodikosBarcode
t1.total - t2.total
from
table1 as t1
inner join table2 as t2
on t2.KodikosBarcode = t1.KodikosBarcode
This assumes that you only want the difference between rows where the bar code exists in both tables.
If you're doing an update to existing rows in the target table, you would use:
update t3 set
t3.total = t1.total - t2.total
from table3 as t3
inner join table1 as t1
on t1.KodikosBarcode = t3.KodikosBarcode
inner join table2 as t2
on t2.KodikosBarcode = t3.KodikosBarcode
As above, I would like the result to be one row of all the columns from each table where the value was met. The common field in all tables is the primary key. Any type of UNION does not work as the amount of columns is not the same in each table. I'm using an Access database so FULL OUTER JOINS are not supported. I'm using this query in an application in C#, Visual Studio and for some reason can't get do an inner join without it giving me a syntax error either. I'd ideally like to have this done in one query.
For example lets say I have the tables:
Table1
userID name age
1 Bob 24
2 John 19
Table2
userID col1 col2 col3
1 fd sd gh
...
Table3
userID col4 col5 col6 col7
1 ff hg fd et
...
And I want the result:
userID name age col1 col2 col3 col4 col5 col6 col7
1 Bob 24 fd sd gh ff hg fd et
Perhaps the closest I've got:
SELECT * FROM table1 AS b
INNER JOIN table2 AS c ON c.userID = b.userID
INNER JOIN table3 AS m ON m.userID = b.userID
WHERE userID = 1;
The error I get:
An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Additional information: Syntax error (missing operator) in query
expression 'c.userID = b.userID INNER JOIN table3 AS m ON
m.userID = b.UserI'.
I should add that I want the query to be adaptable (let's say I don't know the columns but I know it has userID)
Left joins should work in ms-access:
select *
from Table1 t1
left join Table2 t2
on t1.userID = t2.userID
left join Table3 t3
on t1.userID = t3.userID
This should work in Acces:
Select T1.userID, T1.name, T1.age, T2.col1, T2.col2, T2.col3, T3.col4, T3.col5, T3.col6, T3.col7 from Table1 T1
left join Table2 T2 on T2.userID=T1.userID
left join Table3 T3 on T3.userID=T1.userID
Or if you only want records present in the first table use an inner join:
Select T1.userID, T1.name, T1.age, T2.col1, T2.col2, T2.col3, T3.col4, T3.col5, T3.col6, T3.col7 from Table1 T1
inner join Table2 T2 on T2.userID=T1.userID
inner join Table3 T3 on T3.userID=T1.userID
So in the end I was close.
In MS-Access, when joining more than 2 tables you must use parentheses (around the first join?). Second of all, in my where condition I didn't specify a table for userID. See my amended query below:
SELECT * FROM table1 AS b
(INNER JOIN table2 AS c ON c.userID = b.userID)
INNER JOIN table3 AS m ON m.userID = b.userID
WHERE b.userID = 1;
Select
t1.userID
,t1.name
,tb1.age
,tb2.col1
,tb2.col2
,tb2.col3
,tb3.col4
,tb3.col5
,tb3.col6
,tb3.col7
FROM table1 tb1
INNER JOIN table2 tb2 ON tb1.userID=tb2.userID
INNER JOIN table3 tb3 ON tb1.userID=tb3.userID
WHERE --some conditions if u desire.
I think your problem in where condition.you did not specify the table.
So change.
Where userId=1
To be
Where b.userId=1
I have 3 tables:
maintable(id, serialno, col3, col4, col5, ..., col10)
table1(t1_id, serialno, t1_type, t1_color)
table2(t2_id, serialno, t2_base, t2_price)
maintable's Primary Key is id and serialno is UNIQUE.
Table1's Primary Key is t1_id and table2's is t2_id.
Table1 and Table2 serialno are Foreign Keys that reference MainTable's serialno.
maintable has a one to many relationship with both table1 and table2.
What I want to do is join these 3 tables in a DataTable.
I first thought that it would be simple and I tried: "SELECT * FROM maintable INNER JOIN table1 ON maintable.serialno = table1.serialno INNER JOIN table2 ON maintable.serialno= table2.serialno WHERE maintable.id = 200";
The problem with the result is that if table1 has 3 rows and table2 has 4 rows then my DataTable becomes 12 rows(3x4). What I want to do in this instance is just get 4 rows.
table1 and table2 columns don't have anything to do with each other and they only have to match maintable's serialno.
In case that I'm not being understood, I want to select the rows of table1 and table2 that match maintable's serialno and add them to the right of maintable without them getting duplicated.
Edit: Sorry, I had written accountno instead of serialno in some cases.
SELECT * FROM
maintable m
INNER JOIN (
SELECT t1.serialno, t1.t1_type, t1.t1_color, null as t2_base, null as t2_price
FROM table1 t1
UNION
SELECT t2.serialno, null as t1_type, null as t1_color, t2.t2_base, t2.t2_price
FROM table2 t2
) t ON m.serialno = t.serialno
ORDER BY m.serialno
This will do what you're asking for: return number of rows in t1 + number of rows in t2, rather than rows in t1 x number of rows in t2. Fiddle. This may not perform so hot if you have a large amount of data.
Now that you know how it's done, don't do it.
The real question is why is this a requirement? What are you really trying to accomplish here? This is not a meaningful way to combine the data from the two child tables, given their relationships. T1 and t2 are different tables and not keyed to each other for a reason: they aren't meant to combine their data like this.
The only new data I can imagine extracting from this kind of query is the total count of rows in both t1 and t2 for a given serial number. But there are much better ways to get this information than selecting the rows like this. If you need both t1 and t2 data and duplicates are throwing you off, odds are good that you should be making two separate SELECT statements instead of trying to combine everything.
SELECT
maintable.accountno
FROM
maintable
INNER JOIN table1 ON
maintable.accountno = table1.accountno
INNER JOIN table2 ON
maintable.accountno = table2.accountno
WHERE
maintable.id = 200
GROUP BY
maintable.accountno
i want to create an algorithm or formula that gives me the following combinations below. I have manually printed out all the combinations for the example containing 4 tables with respective values.
This is not permutation because i need the combinations to always follow the unique format
Table1,Table2,Table3,Table4 values
So how can i achieve this in SQL script or through C# code (VB.Net code can also work)
Note: the solution contain all the possible 48 combinations i need for my problem.
Problem
Table1 Table2 Table3
Table4
a1 b1
c1 d1
a2 b2
c2 d2
a3
c3
c4
Solution
a1,b1,c1,d1
a1,b1,c1,d2
a1,b1,c2,d1
a1,b1,c2,d2
a1,b1,c3,d1
a1,b1,c3,d2
a1,b1,c4,d1
a1,b1,c4,d2
a1,b2,c1,d1
a1,b2,c1,d2
a1,b2,c2,d1
a1,b2,c2,d2
a1,b2,c3,d1
a1,b2,c3,d2
a1,b2,c4,d1
a1,b2,c4,d2
a2,b1,c1,d1
a2,b1,c1,d2
a2,b1,c2,d1
a2,b1,c2,d2
a2,b1,c3,d1
a2,b1,c3,d2
a2,b1,c4,d1
a2,b1,c4,d2
a2,b2,c1,d1
a2,b2,c1,d2
a2,b2,c2,d1
a2,b2,c2,d2
a2,b2,c3,d1
a2,b2,c3,d2
a2,b2,c4,d1
a2,b2,c4,d2
a3,b1,c1,d1
a3,b1,c1,d2
a3,b1,c2,d1
a3,b1,c2,d2
a3,b1,c3,d1
a3,b1,c3,d2
a3,b1,c4,d1
a3,b1,c4,d2
a3,b2,c1,d1
a3,b2,c1,d2
a3,b2,c2,d1
a3,b2,c2,d2
a3,b2,c3,d1
a3,b2,c3,d2
a3,b2,c4,d1
a3,b2,c4,d2
This is called Cartesian product.
select *
from
table1
cross join table2
cross join table3
cross join table4
Same thing:
select *
from
table1, table2, table3, table4
Use CROSS JOIN (rather then the usual INNER or LEFT OUTER JOINs)
select *
from
table1
cross join table2
cross join table3
cross join table4
or the implied JOIN as per GSerg's answer ...from table1, table2, table3, table4