Why does Apache Drill select column with the same name of a different table - apache-drill

I have a PostgreSQL database linked to a Drill instance.
Whenever I am trying to join 2 tables which both have a column name and whenever I want to select this name Drill selects the wrong name column. What am I doing wrong?
Given the following 2 tables:
Department
| id | name |
|----|------|
| 1 | A |
| 2 | B |
Employee
| id | name | dept | salary |
|----|------|------|--------|
| 1 | U | 1 | 100 |
| 2 | V | 1 | 75 |
| 3 | W | 1 | 120 |
| 4 | X | 2 | 95 |
| 5 | Y | 2 | 140 |
| 6 | Z | 2 | 55 |
Running
select employee.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'
returns
| name | salary |
|------|--------|
| A | 100 |
| A | 75 |
| A | 120 |
Running
select dept.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'
returns
| name | salary |
|------|--------|
| null | 100 |
| null | 75 |
| null | 120 |
What does work, but seems very silly to me, is:
select dept.name, employee.salary
from employee
inner join (select id, name as deptname from department) as department on employee.dept = department.id
where department.deptname = 'A'

This seems to be because you use
select dept.name, [...]
But you have never assigned an alias for the table department (department AS dept). Hence
select department.name, [...]
should yield the value you are looking for.

Related

SQL search merge

I'm trying to merge two select sentence. Is it possible ?
I have three tables :
score
+------+---------+-------+
| ID | SUBJECT | SCORE |
+------+---------+-------+
| 1 | Chinese | 65 |
| 1 | English | 75 |
| 2 | Chinese | 60 |
| 2 | English | 70 |
| 3 | Chinese | 80 |
| 3 | English | 50 |
+------+---------+-------+
student
+------+----------+--------+
| ID | CLASS_ID | NAME |
+------+----------+--------+
| 1 | 1 | TOM |
| 2 | 1 | ANNA |
| 3 | 2 | JOHN |
+------+----------+--------+
class
+------+----------+
| ID | NAME |
+------+----------+
| 1 | 5th |
| 2 | 6th |
+------+----------+
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
+------------+--------+
| CLASS_NAME | NAME |
+------------+--------+
| 5th | TOM |
| 6th | ANNA |
| 6th | JOHN |
+------------+--------+
mysql> select SUM(SCORE) as total from score group by ID;
+-------+
| total |
+-------+
| 140 |
| 130 |
| 130 |
+-------+
Could I merge two select sentence let it be
+------------+--------+-------+
| CLASS_NAME | NAME | total |
+------------+--------+-------+
| 5th | TOM | 140 |
| 6th | ANNA | 130 |
| 6th | JOHN | 130 |
+------------+--------+-------+
Or is there any better search sentence to do this well?
I try use two sentence to merge , but can't have a good idea.
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
mysql> select SUM(SCORE) as total from score group by ID;
hope it can be merge success or have another answer to do this well.
You can use inner join to merge the second query. Below query takes in consideration that the join condition will be score.id with student.id
select c.name as class_name,
st.name ,
sc.total
from class c
inner join student st on st.class_id = c.id
inner join ( select id,
SUM(SCORE) as total
from score
group by id
) as sc on sc.id=st.id ;
https://dbfiddle.uk/tauTGBFO

How to get name and manager name

How to get name and manager name
+----+-----------+---------+
| id | name | manager |
+----+-----------+---------+
| 1 | bhupendra | 0 |
| 2 | shyam | 1 |
| 3 | ram | 1 |
| 4 | geeta | 2 |
| 5 | sita | 1 |
+----+-----------+---------+
i need result like
+-----------+-----------+
| name | manager |
+-----------+-----------+
| bhupendra | |
| shyam | bhupendra |
| ram | bhupendra |
| geeta | shyam |
| sita | bhupendra |
+-----------+-----------+
You could self-join the id column on the manager column:
SELECT e.name AS name, m.name AS manager
FROM employee e
LEFT JOIN employee m ON m.id = e.manager
You should use the same table twice using alias
select a.name, b.name as manager
from my_table a
left join my_table b on a.manager = b.id
Check this out
with recursive cte (id, name, parent_id) as (
select id,
name,
manager
from employee
union all
select p.id,
p.name,
p.manager
from employee e
inner join cte
on p.manager = cte.id
)
select * from cte;

sql Select Orders with multiple addresses

I'm trying to select orders, which are send at least two times with the same addressId to a customer.
This is my table structure:
Customer Table:
+------------+-----------+
| customerId | addressId |
+------------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+------------+-----------+
Relation for Addresses to Orders
+---------+-----------+
| orderId | addressId |
+---------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 4 |
+---------+-----------+
Order Table
+----+------------+-------+
| id | orderEntry | total |
+----+------------+-------+
| 1 | timestamp | 4711 |
| 2 | timestamp | 0815 |
| 3 | timestamp | 1337 |
+----+------------+-------+
Now I want a output like this:
+------------+---------+-----------+
| customerId | orderId | addressId |
+------------+---------+-----------+
| 2 | 2 | 2 |
| 2 | 3 | 2 |
| 4 | 5 | 4 |
| 4 | 6 | 4 |
+------------+---------+-----------+
I've tried to get the right result with these Query, but I think I can't count the addresses this way.
SELECT C.`customerId`, AO.`orderId`, AO`addressId`
FROM customer AS C
JOIN address_order AS AO ON AO.addressId = C.addressId
JOIN order AS O ON O.id = AO.orderId
GROUP BY AO.`orderId`
HAVING (COUNT(AO.`addressId`) > 1);
With these Query I only get a result like this:
+------------+---------+-----------+
| customerId | orderId | addressId |
+------------+---------+-----------+
| 2 | 2 | 2 |
| 4 | 5 | 4 |
+------------+---------+-----------+
I don't see the usage of order table here. However you can use the order table into the consideration if you want to make sure that the order table data and address_order should have data. You can write the query as
select
c.customerId,
ao.orderId,
ao.addressId
from customer c
join address_order ao on ao.addressId = c.addressId
join (
select addressId, count(*) as tot from address_order
group by addressId having tot = 2
)x on x.addressId = ao.addressId
If you want to make sure all the orderId from customer_order are in the order table then you can add another join at the end as
join `order` o on o.id = ao.orderId
Try this
SELECT customerId FROM customer INNER JOIN (SELECT * FROM address_order GROUP BY addressId
HAVING (COUNT(addressId) > 1)) AS t1 ON customer.addressId=t1.addressId

SQL server JOIN query not working

Hi I have doubt in sql server
dept
+---------+--------+
| deptkey | deptno |
+---------+--------+
| 1 | 100 |
| 2 | 101 |
| 3 | -1 |
+---------+--------+
loc
+--------+-------+
| lockey | locid |
+--------+-------+
| 1 | 200 |
| 2 | 201 |
| 3 | -1 |
+--------+-------+
trans
+----+--------+-------+------+
| id | deptno | locid | Name |
+----+--------+-------+------+
| 1 | 100 | 201 | abc |
| 2 | 101 | 203 | def |
| 3 | 103 | 200 | rav |
| 4 | 105 | 204 | jai |
| 1 | 101 | 200 | kal |
| 4 | 100 | 206 | lo |
+----+--------+-------+------+
here tran deptno= dept.deptno then corresponding key values bring if not match then we need to unmatched deptno assign -1 and corresponding key need to retrive
similar tran locid=loc.locid
based on above tables I want output like below
+----+------+---------+--------+
| id | Name | deptkey | lockey |
+----+------+---------+--------+
| 1 | abc | 1 | 2 |
| 2 | def | 2 | 3 |
| 3 | rav | 3 | 1 |
| 4 | jai | 3 | 3 |
| 1 | kal | 2 | 1 |
| 4 | lo | 1 | 3 |
+----+------+---------+--------+
I tried like below query
SELECT a.[id],a.name ,b.deptkey,c.lockey
FROM [trans] a left join dept b on a.deptno=b.deptno
left join loc c on a.locid=c.locid
above query not given expected result can you please tell me how to write query to achive this task in sql server
SELECT a.[id],a.name ,
(CASE WHEN b.deptkey IS NULL THEN (select deptkey from DEPT WHERE DeptNo = -1)
ELSE b.deptkey END) AS 'deptkey',
(CASE WHEN c.lockey IS NULL THEN (select LocKey from LOC WHERE LocId = -1)
ELSE c.lockey END) AS 'lockey '
FROM [trans] a left join dept b on a.deptno=b.deptno
left join loc c on a.locid=c.locid
http://www.sqlfiddle.com/#!3/389463/2
SELECT a.[id],a.name ,b.deptkey,c.lockey
FROM [trans] a left join dept b on isnull(a.deptno,-1)=isnull(b.deptno,-1)
left join loc c on a.locid=c.locid
try with this...
SELECT
a.[id]
, a.name
, ISNULL(b.deptkey,-1) AS [deptkey]
, ISNULL(b.lockey,-1) AS [lockey]
FROM [trans] a
left join dept b
on a.deptno = b.deptno
left join loc c
on a.locid = c.locid
When the value is not found ISNULL, change the result to -1 instead of NULL. You can just change the -1 with any default value you prefer as unmatched.
OR if you want a query driven default value (get the last record as the default value). You can change your script as presented below.
SELECT
a.[id]
, a.name
, ISNULL(b.deptkey,(SELECT TOP 1 deptno from dept ORDER BY deptkey DESC)) AS [deptkey]
, ISNULL(b.lockey,(SELECT TOP 1 locid from loc ORDER BY lockey DESC)) AS [lockey]
FROM [trans] a
left join dept b
on a.deptno = b.deptno
left join loc c
on a.locid = c.locid

mysql select count from multiple tables

If I have two tables job, employee.
I need to select all from job and append employee count for each job.
Supposed Result
job table
+-------------+-------------+
| id | name |
+-------------+-------------+
| 1 | Teacher |
+-------------+-------------+
| 2 | Engineer |
+-------------+-------------+
| 3 | Programmer |
+-------------+-------------+
employee table
+-------------+-------------+-------------+
| id | name | job_id |
+-------------+-------------+-------------+
| 1 | employee N | 1 |
+-------------+-------------+-------------+
| 2 | employee N | 2 |
+-------------+-------------+-------------+
| 3 | employee N | 3 |
+-------------+-------------+-------------+
| 4 | employee N | 1 |
+-------------+-------------+-------------+
| 5 | employee N | 3 |
+-------------+-------------+-------------+
| 6 | employee N | 1 |
+-------------+-------------+-------------+
I need to select all from job and append employee count for each job.
Supposed Result
Result table
+-------------+-------------+--------------+
| id | name |employee count|
+-------------+-------------+--------------+
| 1 | Teacher | 3 |
+-------------+-------------+--------------+
| 2 | Engineer | 1 |
+-------------+-------------+--------------+
| 3 | Programmer | 2 |
+-------------+-------------+--------------+
I like to use left join : )
SELECT job.id, job.name, count( * ) AS 'employee count'
FROM job
LEFT JOIN employee
ON job.id = employee.job_id
GROUP BY job.id
You want to use INNER JOIN to join the tables, then GROUP BY to group on the job id. Finally use COUNT() to get a count of each group
SELECT job.id, job.name, count(*) AS employee_count
FROM job
INNER JOIN employee
ON job.id = employee.job_id
GROUP BY job.id
You can see it live here http://sqlfiddle.com/#!2/9d59c1/1