Upon doing Table t1 inner join table t2, all Boolean values are retuned as TRUE. How do I fix it? - sql

SELECT e.*
FROM Table1 m
INNER JOIN Table2 e ON m.table_id = e.table_id
WHERE m.format_type_id = 3
ORDER BY e.rank desc;
The above Query is a Join between 2 tables.
Expected ResultSet : All fields of Table 2(hence e.*)
Expected:
Table 2 ID
(boolean) header
1
0
2
1
I get the results when I:
remove order by ,the values are coming in properly
or I mention all column names like e.column1, e.column2...
The problem is that all Boolean column values are only coming in as true. The type of the column is BIT in the DB
Output:
Table 2 ID
(boolean) header
1
1
2
1
Have tried this as well: How map a bit type in Mysql to hibernate?

Related

Postgres: Select rows that do not relate in some particular way to other rows in the same table

Is there a way to select rows from a table that, as an example, do not have child rows in the same table (determined by the value in the column parent)?
So, here is a simple example:
id
value
parent
1
v1
null
2
v2
null
3
v3
1
As a query result of selecting all the necessary rows from the table there should be rows with ids 2 and 3.
The row with id === 1 has child row with id === 3 since the last has value 1 in the column parent.
A self join will do the job :
SELECT T1.*
FROM MyTable AS T1
LEFT OUTER JOIN MyTable AS T2
ON T1.id = T2.parent
WHERE T2.id IS NULL
This is call "semi anti join".
There is many solution with operators like : NOT IN, NOT EXISTS, EXCEPT...

Delete a record from a table referring to 2 or more columns from another table

Consider having 2 tables as follows.
Table 1:
Unit
SKU number
Active
A
1
Y
B
2
Y
c
3
Y
Table 2:
Unit
SKU number
description
X
4
Apple
B
2
Mango
Y
5
Grapes
z
6
Banana
I wanted to delete record B,2,Y from table 1 by referring to table 2 where values in columns Unit and SKU number match.
I tried using the following query but it didn't seem to work
DELETE FROM table1
WHERE (Unit, SKU_Number) IN (SELECT Unit, SKU_Number FROM table2);
The error was
An expression of non-boolean type specified in a context where a condition is expected, near ','
Can someone please help me understand what I am doing wrong here or help me rewrite the SQL query to achieve the required objective?
You could use similar logic with exists:
DELETE
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.Unit = t1.Unit AND t2.SKU_Number = t1.SKU_Number);
You can try using this query, assuming Unit of Table 1 is unique:
DELETE FROM table1
WHERE table1.Unit IN (
SELECT table1.Unit
FROM table1
LEFT JOIN table2 ON table1.Unit = table2.Unit
AND table1.SKU_Number = table2.SKU_Number
)
If unit is not an unique field, simply replace it with whichever field is unique, or with primary key of Table 1.
You can use inner join for delete:
DELETE t1
FROM table1 t1
INNER JOIN table2 t2
ON t1.unit=t2.unit and t1.SKU_Number = t2.SKU.Number

Duplicate rows in left join

I have 2 tables. There are about 100000 of null in one column, other values are integer, total values are about 200000. Another table has only the integer value. When I use the left join on this column, it gave me a lot of duplicates rows. Is it ok to use left join here?
Table 1:
Column 1
2
3
5
null
null
Table 2:
Column 1
1
2
3
so on
Your example is really odd. Why would anyone have null values in an ID field? But anyway.
If you need fields from table 2 in the resultset as you say above then you must use an INNER JOIN not a LEFT JOIN
Something like:
SELECT DISTINCT a.id, a.name, b.someOtherField
FROM Table1 a
INNER JOIN Table2 b ON a.id = b.id
Please note: Since only the ID field of table 1 has null values there will be no records selected from table 1 with id IS NULL because they have no equivalent in table 2. Adding the DISTINCT keyword helps in case this query would still produce duplicates.

Presto - Concat multiple tables using unique identifier

I have multiple tables in the following format:
table users -
ID lang
1 EN
2 EN
3 DE
table A -
ID event1 event2
1 5 1
2 null 1
3 11 null
table B -
ID event1 event10
1 2 1
3 2 null
so after concat/join the tables on ID column my final table would look like this:
final_table -
ID lang A_event1 A_event2 B_event1 B_event10
1 EN 5 1 2 1
2 EN null 1 null null
3 DE 11 null 2 null
So I have multiple issues here, first how to properly do the joins so that aliases would match table names and have final unique column names even though the events have same naming inside the columns, and also I would like all the missing values would also have null values (like table B that does not have user ID = 2).
My tries so far were not successful as the column names would be duplicated without unique IDs and missing values were not filled with nulls properly.
example for what I already tried:
select t1.*, t2.*, t3.*
from users t1
left join
A t2
using (ID)
left join
B t3
using (ID)
I can construct the query programmatically to provide flexability, but I would like to know the proper syntax for such case.
Thanks.
Your attempt with two left joins looks quite good. I would, however suggest not using the using(id) syntax to join the tables: with 3 tables involved, it is ambiguous to which id column you are referring, which could lead to missing records in the resultset:
select
u.id,
u.lang,
ta.event1 A_event1,
ta.event2 A_event2,
tb.event1 B_event1,
tb.event110 B_event10
from users u
left join tableA ta on ta.id = u.id
left join tableB tb on tb.id = u.id
I don't see how this query would generate duplicate ids in the resultset (as long as the ids are unique in each table, as shown in your sample data).
If the non-id columns in the tables were unique, then you could express this as:
select *
from users u left join
A
using (ID) left join
B
using (ID);
The id means the same thing in the three tables, so it is appropriate to use using. In fact, using is very handy when working with outer joins (although more so with full join).
I'm not a big fan of using select *. And it is not appropriate in this case because the columns are not unique. So a fine way to write the query is:
select u.*,
a.event1 as a_event1, a.event2 as a_event2,
b.event1 as b_event1, b.event10 as b_event10
from users u left join
A
using (ID) left join
B
using (ID);

SQL STATEMENT QUERY

I have table1Name with data populated and table2 with no data populated.
select * from [database1Name].dbo.table1Name
join [database1Name].dbo.table2Name
on [database1Name].dbo.table1Name.fieldName like value;
After running the above sql statement it joins the tables but does not show any populated data from the table 'table1Name'.
Why does this happen?
Using JOIN which is an INNER JOIN means that it will get you only data where the condition matches. So if the second table has not data, then the condition is never met, so you get no data in return.
In your case you need a LEFT JOIN. This will get all the rows from the left table (table1Name in your case) and the corresponding values from the right table when the condition is met.
SELECT *
FROM [database1Name].dbo.table1Name
LEFT JOIN [database1Name].dbo.table2Name
ON [database1Name].dbo.table1Name.fieldName like [database1Name].dbo.table2Name.fieldName;
Just to mention that using joins mean that you might get multiple times a single row from a specific table. For instance since you have a LIKE condition, if fieldName of Table 1 matches fieldName in 2 rows from Table 2 then you will get two rows containing the same row from Table 1 and the two rows from Table 2:
Example:
Table1
FieldName
1
2
Table2
FieldName OtherField
1 1
1 2
Result of LEFT JOIN
T1FieldName T2FieldName T2OtherField
1 1 1
1 1 2
2 NULL NULL