SQL CROSS JOIN question - c#

I am having a bit of trouble with my SQL query.
I have two tables:
Table1
id guid title D0 D1 D2
-----------------------------------------
1 guid1 Title1 0.123 -0.235 0.789
2 guid2 Title2 -0.343 0.435 0.459
3 guid3 Title3 0.243 -0.267 -0.934
...
100 guid4 Title100 -0.423 0.955 0.029
and Table 2 (note it has the same schema, just different data).
id guid title D0 D1 D2
----------------------------------------
1 guid1 Title1 0.233 -0.436 -0.389
2 guid2 Title2 -0.343 0.235 0.789
3 guid3 Title3 0.573 -0.067 -0.124
...
100 guid4 Title100 -0.343 0.155 0.005
I am trying to figure out how to write a SELECT statement which returns all the titles WHERE all the combinations of ABS(Table1_D0*Table2_D0)+ABS(Table1_D1*Table2_D1)+ABS(Table1_D2*Table2_D2) are less than a thresholded value (hard coded probably).
So far I am trying to use a CROSS JOIN, but I am not sure if this is the correct approach.
Does this make sense? Table1, row1 against all the rows of Table2, then Table1, row2 against all the rows of Table2.
If it matters, I am using MS SQL.
Many thanks!
Brett

SELECT t1.title
FROM Table1 t1
CROSS JOIN table2 t2
WHERE ABS(t1.D0*t2.D0)+ABS(t1.D1*t2.D1)+ABS(t1.D2*t2.D2)<10

SELECT *
FROM Table1 a inner join
Table2 b on a.Id=b.Id
where ABS(a.D0*b.D0)+ABS(a.D1*b.D1)+ABS(a.D2*b.D2)<=#Value

CROSS JOIN is the right choice and CROSS JOIN is just the same as no join at all (see Snowbear answer).

select t1o.title
from Table1 t1o
where not exists
(
-- none of the cross-joined rows for t1o must be above the threshold
select t1.title
from Table1 t1
cross join Table2 t2
where t1.id = t1o.id -- only the cross joined rows for the current t1o row
-- inverted b/c not exists
and abs(t1.D0*t2.D0)+abs(t1.D1*t2.D1)+abs(t1.D2*t2.D2) > 10
)

Related

How to Select all records from table 1 and exclude matching records from table 2 using LINQ

I want to select all records from table 1 and if there any match between table 1 and table 2 then I have to exclude that records.
Note: The requirement is I have to use left outer join for that..
For example,
table 1 table 2
1 1
2 2
3
Output should be 3
I have written query in SQL but I want this in SQL LINQ like from a in dbContext.Table1....
select t1.*
from table1 t1
left outer join table2 t2 on t1.ID = t2.ID and t1.Code = t2.Code
where s.ID is null
How to solve this?
var result = (from primer in one
join primer2 in two on primer.id equals primer2.id into gj
where gj.Count()==0
select new
{
primer.id
}).ToList();
Where ONE - it is first table, TWO - it is second table.
Instead of
...select new
{
primer.id
}...
You can use
...
select primer
...

Function in SQL to diff values from 2 different tables and put the value to a third table

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

(SQL) Select and return all from more than 2 tables that have a common field where a specific value is present in all tables

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

Joining multiple tables with one to many relationship

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

How to create algorithm or formula to get the combinations?

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

Categories