I've got the following two SQL tables (in MySQL): - proc-sql

I've got the following two SQL tables (in proc SQL):
A
+----+------+--------------+
| id | age |
+----+------+
| 1 | 10 |
+----+------+
| 2 | 20 |
+----+------+
| 3 | 30 |
+----+------+
| 4 | 40 |
+----+------+
B
+----+
| id |
+----+
| 1 |
+----
| 2 |
+----+
| 3 |
+----+
The desired output would be: How do I get this output in proc sql
+----+------+
| id | age |
+----+------+
| 4 | 40 |
+----+------+

Try this
SELECT A.*
FROM A NATURAL LEFT JOIN B
WHERE B.id IS NULL
SQL FIDDLE DEMO

select * from A
where not exists(select * from b
where B.ID=A.Id)

Related

SQL Server select all max and min data in a column

+----+------+
| id | data |
+-----------+
| 1 | a |
| 2 | b |
| 3 | a |
| 3 | c |
+----+------+
I need to select data [a,b,c].
Yes, got it.
the query is 'SELECT distinct(data) from dataTable'
Thanks.
Do you want the distinct values in the second column?
select distinct data
from t;

SQL get interection of values across multiple rows grouped by primary key

I have table with data as follows
+----+------+
| id | code |
+----+------+
| 1 | M |
| 1 | Y |
| 2 | M |
| 2 | S |
| 3 | M |
| 3 | Q |
+----+------+
I would like to know if its possible to write a query that would return a list of codes that are unique to each ID? If there is no intersection the query should return no rows.
In the example above the only value common to all is M.
+----+------+
| id | code |
+----+------+
| 1 | M |
| 1 | S |
| 2 | M |
| 2 | S |
| 2 | H |
| 3 | M |
| 3 | S |
| 3 | Q |
+----+------+
The above would return M and S, common to all three ID's
Thanks
Try this:
SELECT code
FROM mytable
GROUP BY code
HAVING COUNT(*) = (SELECT COUNT(DISTINCT id) FROM mytable)
The above query assumes that code can appear only once per id.

sql NOT with ALL operator

I'm trying to use the NOT operator with ALL but does not compare as it should
I followed the following:
tablex contains for example:
+------+------+
| id | name |
+------+------+
| 6 | a |
| 7 | b |
| 8 | c |
| 9 | d |
| 10 | e |
+------+------+
5 rows in set (0.04 sec)
and tabley contains:
+------+------+
| id | name |
+------+------+
| 4 | a |
| 5 | b |
| 7 | c |
| 8 | d |
+------+------+
4 rows in set (0.03 sec)
i've used:
SELECT id, name FROM tablex WHERE NOT id < ALL (SELECT id FROM tabley);
returns:
+------+------+
| id | name |
+------+------+
| 6 | a |
| 7 | b |
| 8 | c |
| 9 | d |
| 10 | e |
+------+------+
5 rows in set (0.00 sec)
the problem is that returns lower values than those of 'tabley' in some cases,
It is very logical the solution using the operator >, but what is this about?
If you're using the < operator, it seems like you want to make sure that id is smaller than the smallest id in the subquery. So to express that in SQL:
SELECT id, name FROM tablex WHERE NOT id < (SELECT min(id) FROM tabley)
Alternately, flipping around the negation:
SELECT id, name FROM tablex WHERE id >= (SELECT min(id) FROM tabley)

Create a combined list from two tables

I have a table with CostCenter_ID (int) and a second table with Process_ID (int).
I'd like to combine the results of both tables so that each cost center ID is assigned to all process IDs, like so:
|CostCenterID | ProcessID |
---------------------------
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
I've done it before but I'm drawing a blank. I've tried this:
SELECT CostCenter_ID,NULL FROM dbo.Cost_Centers
UNION ALL
SELECT NULL,Process_ID FROM dbo.Processes
which returns this:
|CostCenterID | ProcessID |
---------------------------
| 1 | NULL |
| NULL | 1 |
| NULL | 2 |
| NULL | 3 |
Try:
select a.CostCenterID, b.ProcessID
from table1 a
cross join table2 b
or:
select a.CostCenterID, b.ProcessID
from table1 a
,table2 b
NB: cross join is the better method as it makes it clearer to the reader what your intentions are.
More info (with pics) here: http://www.w3resource.com/sql/joins/cross-join.php

MySQL JOIN two tables with different number of rows by id

I have two tables.
Table 1
+----+--------+
| Id | Column |
+----+--------+
| 1 | 23 |
+----+--------+
| 2 | 34 |
+----+--------+
| 3 | 99 |
+----+--------+
Table 2
+----+--------+
| Id | Column |
+----+--------+
| 10 | 1 |
+----+--------+
| 11 | 1 |
+----+--------+
| 21 | 2 |
+----+--------+
| 33 | 3 |
+----+--------+
I want to combine these tables to get
Table 3 (Desired)
+--------+--------+
| Column | Column |
+--------+--------+
| 10 | 23 |
+--------+--------+
| 11 | 23 |
+--------+--------+
| 21 | 34 |
+--------+--------+
| 33 | 99 |
+--------+--------+
I know how to get this:
Table 3 (Actual)
+--------+--------+
| Column | Column |
+--------+--------+
| 10 | 23 |
+--------+--------+
| 11 | null |
+--------+--------+
| 21 | 34 |
+--------+--------+
| 33 | 99 |
+--------+--------+
but when the rows have duplicate Id columns, the first is the correct value and
the rest are always NULL.
The query I have to produce Table 3 is
SELECT table2.id, table1.Column
FROM table2
LEFT JOIN (SELECT .... ) as table1 ON table1.id = table2.Column
How can I join tables to get the correct version of Table 3?
This should work fine:
SELECT
t2.id AS column1,
t1.column AS column2
FROM table2 AS t2
LEFT JOIN table1 AS t1 ON t1.id = t2.column;
See it in action here:
SQL Fiddle Demo
This will give you the same result you want:
| COLUMN1 | COLUMN2 |
---------------------
| 10 | 23 |
| 11 | 23 |
| 21 | 34 |
| 33 | 99 |