In SQL Server 2008, how can I retrieve data from table1 with new magic column (bit value), this new column take 'true' if current id from table1 exist in table2, else take 'false'.
For example :
Table1
id | name
-----------
10 | USA
11 | UK
12 | France
table2
id | title | fk
----------------------
1 | v1 | 10
2 | v2 | 10
3 | v3 | 11
result :
id | name | new column
----------------------------
10 | USA | true
11 | UK | true
12 | France | false
SELECT DISTINCT table1.id, table1.name,
CASE WHEN table2.id IS NULL THEN 'False' ELSE 'True' END AS mycolumn
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.fk
You can use left join to get the data you want.
SQL
select A.*
,case when B.fk is not null then 'true' else 'false' end as [new column]
from table1 A
left join
(
select distinct fk from table2
)B on A.id=B.fk
OUTPUT
id name new column
10 USA true
11 UK true
12 France false
Read about different join types. this is basic SQL.
SELECT Table1.[id], [name], CASE WHEN Table2.[id] IS NULL THEN 0 ELSE 1 END As [New column]
FROM Table1 LEFT JOIN
Table2 ON(Table1.[id] = Table2.[fk])
The following query returns the expected result:
SELECT T1.*
,CAST(CASE
WHEN T2.id IS NOT NULL THEN 1
ELSE 0
END AS BIT) AS magic_column
FROM table1 T1
LEFT JOIN table2 T2 ON T1.id = T2.fk
Related
I am trying to insert new values into specific column with "update table set specific column...."
I have two tables like this:
Table1
Name Idnumber Score
JOHN DB 10
JOHN IT NULL
KAL DB 9
HENRY KK 7
KAL DB 10
HENRY IP 9
ALI IG 10
ALI PA 9
Table2
NAME | MONEY |
-----------------|
JOHN | |
-----------------|
KAL | |
-----------------|
HENRY | |
-----------------|
ALI | |
-----------------
And I want that my table look like this:
Updated Table
NAME | MONEY |
-----------------|
JOHN | |
-----------------|
KAL | yes |
-----------------|
HENRY | half |
-----------------|
ALI | yes |
-----------------
The condition for writing "yes" into money column is that all scores under same name in table1 should be 9 or higher, the condition for writing "half" is that scores should be no lower than 6 ( what I mean is that scores might be 10,9,8,7 and 6)
So basically it means, that, for example, Henry cannot be selected and I cannot write "yes" next to his name in updated table, because he has score under the value of 9 in one line , but in the other he has the score of 7.(null values also should be emitted).
I'm using a sas program. Thank you!!!
You seem to be treated NULL as a value that is less than "9". In standard SQL (which works in both MySQL and SAS, the original tags) is:
update table2 t2
set money = 'YES'
where not exists (select 1
from table1 t1
where t1.name = t2.name and coalesce(t1.score, 0) < 9
);
If you want to guarantee that there is at least one row in table2, one method is aggregation:
update table2 t2
set money = 'YES'
where (select (case when sum(case when t1.score > 9 then 1 else 0 end) = count(*)
then count(*)
end)
from table1 t1
where t1.name = t2.name
) > 0
You could try the following syntax:
update table2
set money = 'YES'
where not exists (select 1 from table1 t1 where t1.name = table2.name and t1.score < 9)
However this would also update records that have no match in table1. In MySQL, one simple option to avoid that uses a join:
update table2 t2
inner join (select name, min(score) min_score from table1 group by name) t1
on t1.name = t.name and t1.min_score >= 9
set t2.money = 'YES'
I have two tables with shared key and I'm trying to join them to filter data based on few conditions
tbl1
id | OutPutValue |
1 | 2019 |
2 | 2018 |
tbl2
object_id | status | type |
1 | 22 | a |
1 | 22 | c |
1 | 33 | b |
2 | 33 | c |
2 | 33 | c |
2 | 33 | c |
What I'm trying to get is : it must select all 'OutPutValue' from tbl1 where, in tbl2 column 'type' should be c, and column 'status' must have same value for all rows i.e. 33. Note that Primary key (id) of tbl1 is foreign key (object_id) in tbl2.
Select column from tbl1 if, All rows in tbl2 (id of tbl1 have multiple rows (based on object_id) in tbl2) have same status value i.e. 33 and Type should be 'c'.
OutPutValue | Type | status |
2018 | c | 33 |
I have tried with following solution, but it's not returning desired output :
SELECT a.OutPutValue FROM tbl1 a JOIN tbl2 b ON a.id = b.object_id WHERE b.Type =c
GROUP BY a.OutPutValue, b.status HAVING b.STATUS IN(33)
You can try using correlated subquery
DEMO
select distinct OutPutValue,type, status
from t2 a inner join t1 b on a.object_id=b.id
where type='c' and not exists
(select 1 from t2 a1 where a.object_id=a1.object_id and status<>33 and type='c')
OUTPUT:
OutPutValue type status
2018 c 33
Another solution could be the following :
SELECT T1.id, T1.outputvalue FROM tbl1 T1
JOIN (
SELECT tbl2.*, MAX(type), MAX(status)
FROM tbl2
GROUP BY object_id
HAVING
MIN(status) = MAX(status) AND
MIN(type) = MAX(type)
) T2 ON T1.id = T2.object_id
WHERE T2.type = 'c'
EDIT: I have updated my query to match a particular case which make it quite similar to another answer.
FIND A DEMO HERE
Try a join combined with an aggregation:
SELECT
t1.OutPutValue,
MAX(t2.type) AS type,
MAX(t2.status) AS status
FROM tbl1 t1
INNER JOIN tbl2 t2
ON t1.id = t2.object_id
GROUP BY
t1.id,
t1.OutPutValue
HAVING
MIN(t2.status) = MAX(t2.status) AND
MAX(t2.status) = 33 AND
MIN(t2.type) = MAX(t2.type) AND
MAX(t2.type) = 'c';
I have two tables:
TABLE1
id_attr
-------
1
2
3
TABLE2
id | id_attr | val
----------------------
10 | 1 | A
10 | 2 | B
As a result I want a table that show:
RESULT
id | id_attr | val
----------------------
10 | 1 | A
10 | 2 | B
10 | 3 | NULL
So I want the row with id=10 and id_attr=3 also when id_Attr=3 is missing in TABLE2 (and I know that because I have a NULL value (or something else) in the val column of RESULT.
NB: I could have others ids in table2. For example, after insert this row on table2: {11,1,A}, as RESULT I want:
id | id_attr | val
----------------------
10 | 1 | A
10 | 2 | B
10 | 3 | NULL
11 | 1 | A
11 | 2 | NULL
11 | 3 | NULL
So, for every id, I want always the match with all id_attr.
Your specific example only has one id, so you can use the following:
select t2.id, t2.id_attr, t2.val
from table2 t2
union all
select 10, t1.id_attr, NULL
from table1 t1
where not exists (select 1 from table2 t2 where t2.id_attr = t1.id_attr);
EDIT:
You can get all combinations of attributes and ids in the following way. Use a cross join to create all the rows you want and then a left join to bring in the data you want:
select i.id, t1.id_attr, t2.val
from (select distinct id from table2) i cross join
table1 t1 left join
table2 t2
on t2.id = i.id and t2.id_attr = t1.id_attr;
It sounds like you want to do just an outer join on id_attr instead of id.
select * from table2 t2
left outer join table1 t1 on t2.id_attr = t1.id_attr;
ID | NAME | REFERENCE | STATUS
-------------------------------
1 | name1 | 123 | 0
2 | name1 | 123 | 1
3 | name2 | 111 | 0
4 | name3 | 222 | 0
5 | name5 | 555 | 1
Let's say I have this table TABLE1. I need a select statement that will only find records with STATUS=0, but if "same" record exists with STATUS=1 (like those records with ID 1 and 2)
So, query must find only third and fourth record.
Based on your suggested results, I am reading your question as "find only records with status = 0 where the same name/reference combination doesn't have a record with status = 1".
One way you can do this is with a not exists clause:
select t.*
from table t
where status = 0 and
not exists (select 1
from table t2
where t2.name = t.name and
t2.reference = t.reference and
t2.status = 1
);
Try this query out:
SELECT A.*
FROM TABLE1 A
LEFT JOIN TABLE1 B ON
A.ID = B.ID AND
A.NAME = B.NAME AND
A.REFERENCE = B.REFERENCE AND
B.STATUS = 1
WHERE A.STATUS = 0
AND B.REFERENCE IS NULL
The NULL check on B.REFERENCE in the WHERE clause basically ensures that there was no matching record found in the LEFT JOIN (you could just use B.ID or B.NAME also).
MINUS can be used to get the value with Status both 0 but not 1.
SELECT ID, Name, Reference, Status
FROM Table1
WHERE (Name, Reference) IN (SELECT Name, Reference
FROM Table1
WHERE Status = 0
MINUS
SELECT Name, Reference
FROM Table1
WHERE Status = 1)
SELECT * FROM TABLE WHERE NAME NOT IN
(
SELECT NAME FROM
TABLE
GROUP BY NAME
HAVING COUNT(DISTINCT STATUS)>1
) AND STATUS='0';
Let us suppose the following table (e.g. a result of several inner join statements):
id | column_1 | column_2
------------------------
1 | 1 |
2 | 2 | 2
3 | | 3
Which you could for example get from the following statement:
select a.id, t1.column_1, t2.column_2
from a
left join t1 on a.id = t1.id
left join t2 on a.id = t2.id
Now, if i'd like to sum up t1.column_1 and t2.column_2 as follows
select
a.id,
t1.column_1,
t2.column_2,
(t1.column_1 + t2.column_2) as cumulated
from a
left join t1 on a.id = t1.id
left join t2 on a.id = t2.id
The result will look as follows:
id | column_1 | column_2 | cumulated
------------------------------------
1 | 1 | NULL | NULL
2 | 2 | 2 | 4
3 | NULL | 3 | NULL
My question basically is: is there a way to typecast NULL into 0 in order to do some math?
I have tried CONVERT(t1.column_1, SIGNED) and CAST(t1.column_1 as SIGNED), but a NULL stays a NULL.
Use IFNULL(column, 0) to convert the column value to zero.
Alternatively, the COALESCE function will do the same thing: COALESCE(column, 0), except
COALESCE is ANSI-compliant, IFNULL is not
COALESCE takes an arbitrary number of columns/values and will return the first non-null value passed to it.