SELECT t1.id, t1.name, t1.population, CAST(SUM(t2.town_1) AS CHAR) AS town, CAST(SUM(CASE WHEN t2.id LIKE 23 THEN 1 ELSE 0 END) AS CHAR) AS population FROM area1 t1 LEFT JOIN area2 t2 ON t1.id = t2.id WHERE t1.id like 23
Normally it will match if id = 23 exist in both tables. But it's not the case (not in t1 table) so the request return NULL values.How i can do to return no result?
If you only want the result to be matched if the ID exists in BOTH tables, you're looking for an INNER JOIN instead of a LEFT JOIN.
EDIT:
On second thought, it's because of your aggregate functions (SUM). You're casting them as CHARs (didn't even know you could do that). You should be using a GROUP BY. Try this:
SQL Fiddle
SELECT
t1.id,
t1.name,
t1.population,
SUM(t2.town_1) AS town,
SUM(CASE WHEN t2.id LIKE 23 THEN 1 ELSE 0 END) AS population
FROM
area1 t1
INNER JOIN area2 t2
ON t1.id = t2.id
WHERE
t1.id like 23
GROUP BY
t1.id,
t1.name,
t1.population
On a side note, there's no need for the like in your WHERE clause. The way you have it, it's doing the same thing as an = sign.
Related
I have 2 access tables that contain partially similar data, one being more enriched than the other.
The idea here is to join the two tables by the fields id and num and to get from the table T2 the num that are not in the table T1
T1:
id
num
1
34
3
51
7
23
T2:
id
num
status
1
34
done
1
79
done
1
39
done
3
51
done
7
23
done
Expected result:
id
num
status
1
79
done
1
39
done
Under access I read on internet that there is no MINUS operator like under MySQL, so I tried with EXCEPT but the query takes a long time (stopped after 10min)
So I tried this:
SELECT T2.*
FROM T2 LEFT JOIN T1 ON (T1.id =T2.id)
WHERE T1.num IS NULL AND ( (T2.status LIKE 'done') );
The problem now is, I don't have all the records that are in T2 but not in T1
You can use RIGHT JOIN. And I recommend to do not use "LIKE" in this case because this is slow. You can just use the = operator. So your query will be:
SELECT t2.id, t2.num, t2.status
FROM t1
RIGHT JOIN t2
ON t1.id = t2.id
AND t1.num = t2.num
WHERE t1.num IS NULL
AND t2.status = 'done';
In case all column names you want to join are identic in both tables, you can join more simple:
SELECT t2.id, t2.num, t2.status
FROM t1
RIGHT JOIN t2
USING (id,num)
WHERE t1.num IS NULL
AND t2.status = 'done';
I don't like this, but it's shorter. At the end, your concrete query depends on your personal "taste".
There is a lot of variants.
SELECT t2.*
FROM t2
LEFT JOIN t1 USING (id, num)
WHERE t1.id IS NULL
AND t2.status = 'done'
SELECT *
FROM t2
WHERE NOT EXISTS ( SELECT NULL
FROM t1
WHERE (t1.id, t1.num) = (t2.id, t2.num) )
AND status = 'done'
There are more variants...
What of these (or maybe some another) variants is the most effective? this depends on the tables definitions and data statistic.
You're missing a condition on the join:
SELECT T2.*
FROM T2
LEFT JOIN T1
ON T1.id = T2.id
AND T1.num = T2.num
WHERE T1.num IS NULL
AND T2.status LIKE 'done';
Check it here.
I have a SQL query like this:
SELECT t1.name, MAX(t2.value)
FROM t2
JOIN t1 ON t1.id = t2.t1_id
WHERE t2.t1_id = 1 AND t2.text_id = 16;
However, when t2 selection is empty, it returns a row containing NULL values (because of MAX function returning NULL when called on an empty set). I would like it to return an empty set instead. How can I achieve it?
Having clause fits perfectly here:
SELECT
t1.name, MAX(t2.value)
FROM
t2 JOIN t1 ON t1.id = t2.t1_id
WHERE
t2.t1_id = 1 AND t2.text_id = 16
-- GROUP BY something?
HAVING
MAX(t2.value) IS NOT NULL
Try this in sql server ...
with cte as
(
SELECT t1.name, MAX(t2.value) a
FROM t2
JOIN t1 ON t1.id = t2.t1_id
WHERE t2.t1_id = 1 AND t2.text_id = 16;
)
select * from cte where a is not null
try this in Mysql
select p.* from
(
SELECT t1.name, MAX(t2.value) a
FROM t2
JOIN t1 ON t1.id = t2.t1_id
WHERE t2.t1_id = 1 AND t2.text_id = 16;
) p where p.a is not null
Simply RETURN when you don't want to get a resultset.
IF ((SELECT MAX(t2.Value) FROM t2) > 0)
SELECT t1.name, MAX(t2.value)
FROM t2
JOIN t1 ON t1.id = t2.t1_id
WHERE t2.t1_id = 1 AND t2.text_id = 16
ELSE
RETURN
Totally agree with comment by Gordon Linoff.
To wrap select query in another select query is good idea but when we have many aggregate fields then it will become cumbersome to do and think about that you have to make change like this to more than 10 queries.
You should use GROUP BY clause to query which will help you to fix your mentioned issue but more than that it will help you to get aggregate values(field wise) based on group by clause. In your case you should use GROUP BY t1.name can work.
So your aggregate result set will be group by name.
If you do not use GROUP BY then think that you have name field which have 100 unique values but you'll only 1 row which all rows aggregated data which may be wrong implementation.
Here's some more details about aggregate functions and group by.
How do I join these two tables together?
type count
NULL 117
2 1
type count
NULL 807
1 3
2 32
I've tried INNER JOIN, LEFT JOIN, and RIGHT JOIN and I can't quite figure it out. I would like the end result to look like
type count count
NULL 117 807
1 NULL 3
2 1 32
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
Sourced from this question
You have a problem because NULLs do not match by default. But, you can make them match. Let me assume the first is t1 and the second t2
select t2.type, t1.count, t2.count
from t2 left join
t1
on t2.type = t1.type or (t2.type is null and t1.type is null);
Here is a SQL Fiddle that demonstrates that this correctly answers the OP's question.
The right type of join would be FULL OUTER JOIN, but it does not exist in MySQL.
We can emulate it in the following way:
SELECT t1.type, t1.count, t2.count # exact matches
FROM t1 join t2 on t1.type = t2.type
UNION
select t1.type, t1.count, NULL # t2 not present
FROM t1 left join t2 on t1.type = t2.type
WHERE t2.type IS NULL
UNION
select t2.type, NULL, t2.count # t1 not present
FROM t2 left join t1 on t2.type = t1.type
WHERE t1.type IS NULL
UNION
select NULL, t1.count, t2.count # NULL on both sides
FROM t1, t2
WHERE t1.type IS NULL and t2.type IS NULL;
I have a question for the question ;)
What would happen if the tables would look like follows? (just a small change)
type count
NULL 117
3 333
2 1
type count
NULL 807
1 3
2 32
Because in this case both tables contain records that do not match the other table, so probably joining from one direction is not enough and you need to join the tables from both directions, but then, you might have trouble using data for the 'type' from only one table...
So one solution might be something like:
select if (t1.type is null, t2.type, t1.type) as type, t1.count count1, t2.count count2
from t1
left join t2
on t1.type=t2.type or (t1.type is NULL and t2.type is NULL)
union
select if (t1.type is null, t2.type, t1.type) as type, t1.count count1, t2.count count2
from t1
right join t2
on t1.type=t2.type or (t1.type is NULL and t2.type is NULL);
In addition,
you may also use the coalesce() function instead of if (.. is null, ...) e.g. coalesce(t1.type, t2.type)
you may still need to be careful with union, perhaps you want to keep duplicated records (if there is any) and use union all
http://www.sqlfiddle.com/#!2/302e69/2
This is my table structure.
sometime table1 data my repeat (Ex : Actually Id 1 should have only 4 rows but sometime it is 8 due to duplication) so avoid duplication I use GROUP BY command in select query
Table 1
|id| website|time|
-----------------
|01|facebook|20.0|
|01|google |40.0|
|01|youtube |10.0|
|01|ebay |30.0|
|02|facebook|50.0|
|02|ebay |50.0|
Table 2
|id|marks|
-----------
|01| 80|
|02| 90|
|03| 70|
|04| 100|
I want to select (marks),(time on facebook) and (count of time on google & youtube) of specific user
Following select query gives (marks),(time on facebook) of user id '01'
How to receive count of time of both google and youtube of id'1' in same query ?
SELECT table2.id,table2.marks, table1.time
FROM table1
RIGHT JOIN table2 ON table1.id= table2.id
WHERE table1.website LIKE ('%facebook%')
AND table1.id= '01'
GROUP BY table1.id, table1.website
You want to find the time on facebook and then the sum of youtube and google for a particular user you can use the mysql conditional sum to achieve it
select
sum(case when t1.website = 'facebook' then t1.time else 0 end) as `fb_time`,
(
sum(case when t1.website='google' then t1.time else 0 end)+
sum(case when t1.website='youtube' then t1.time else 0 end)
)
as `google_youtube`,
t2.marks
from table1 t1
join table2 t2 on t1.id = t2.id
where t1.id = '01'
If you need to calculate the same for all the users then you can do it as
select
t1.id,
sum(case when t1.website = 'facebook' then t1.time else 0 end) as `fb_time`,
(
sum(case when t1.website='google' then t1.time else 0 end)+
sum(case when t1.website='youtube' then t1.time else 0 end)
)
as `google_youtube`,
t2.marks
from table1 t1
join table2 t2 on t1.id = t2.id
group by t1.id
If I understand your query correctly, I think you will need to use a subquery.
The following subquery returns two counts; time_on_facebook & time_on_google_and_youtube
for all users
SELECT t1.id, t2.marks,
COUNT(t1.time) as time_on_facebook,
(SELECT COUNT(t1_sq.time)
FROM `table1` as t1_sq
WHERE (t1_sq.website = "youtube" OR t1_sq.website = "google")
AND t1_sq.id = t1.id
GROUP BY t1.id) as time_on_google_and_youtube
FROM `table1` as t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t1.website = "facebook"
GROUP BY t1.id
To restrict it to user id = 01, add in a WHERE clause
SELECT t1.id, t2.marks,
COUNT(t1.time) as time_on_facebook,
(SELECT COUNT(t1_sq.time)
FROM `table1` as t1_sq
WHERE (t1_sq.website = "youtube" OR t1_sq.website = "google")
AND t1_sq.id = t1.id
GROUP BY t1.id) as time_on_google_and_youtube
FROM `table1` as t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t1.website = "facebook" AND t1.id = 1
GROUP BY t1.id
Are you sure you want COUNT(time) or do you want SUM(time)?
Lastly, consider adding a primary key to both tables and maybe rename the "id" column to "user_id" for clarity.
Its not clear what you want the output to look like. I made a query,
but did not try it. Try it and let me know if it works.
select t1.id, t1.website, sum(t1.time) as total_time, max(t2.marks) as marks
from table1 as t1
left join table2 as t2
on t1.id = t2.id
where t1.website = 'facebook'
and t1.id = '01'
group by t1.id, t1.website
UNION
select t1.id, t1.website, sum(t1.time) as total_time, max(t2.marks) as marks
from table1 as t1
left join table2 as t2
on t1.id = t2.id
where t1.website IN ('youtube', 'google')
and t1.id= '01'
group by t1.id, t1.website
SELECT
count(t1.id) AS c1
FROM
table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c1 = 6 -> CORRECT!
SELECT
count(t2.id) AS c2
FROM
table2
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c2 = 1 -> CORRECT!
SELECT
count(t1.id) AS c1,
count(t2.id) AS c2
FROM
table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c1 = 6 -> CORRECT!
c2 = 6 -> WRONG!
How do I request both counts in one query, without getting wrong results?
I need to count two different requests at the same table (table1).
so, I'm using an alias for both request. (t1). Each alias-request is working fine alone. If I use both in the same query, i got wrong results.
count() will get you the number of records that are returned by your query. Since if you removed the counts and replaced it with * you would have 6 rows both of those counts are giving you 6.
Is there any reason why you cant use two sub selects and return the result of each of those?
So:
SELECT subQ1.c1, subQ2.c2 FROM
(SELECT count(t1.id) AS c1 FROM table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
WHERE table2.mode = 'ls') as subQ1,
(SELECT count(t2.id) AS c2 FROM table2
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE table2.mode = 'ls') as SubQ2;
I believe your problem on the full query is your group by function. You are grouping by t.id, thus a1.id will have a different count based on how many rows you have.
What I mean by this is if there are 6 rows in table t, then count is going to return 6 for table t; but also since there looks to be a 1 to 1 relation on table a, there are 6 matching rows in table a to the 6 matching rows in table t. such that
t.id = a.id
1 = 1
2= 2 ...etc.
Thus your count is returning rows versus the count you believe you should have? I believe sum function is what you want to use here.
You could try this...but I'm not really sure what you're trying to do.
SELECT (...)
count(CASE WHEN t1.uid = t3.uid THEN t1.id ELSE NULL END) AS CBanz,
count(CASE WHEN ta1.pid = t3.id THEN a1.id ELSE NULL END) AS CBanz1
FROM
t0
LEFT JOIN (...)
LEFT JOIN t1 ON (t1.uid = t3.uid)
LEFT JOIN t1 AS a1 ON (a1.pid = t3.id)
WHERE (...)