I also want to see the count of zero [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I also want to show the count of zero in this sql.
SELECT diaries.id, emotion_id, image_url, color, summary, public, is_completed, diaries.created_at, diaries.is_deleted, likes.is_deleted, users.nickname, likes.diary_id, COUNT(likes.user_id) AS cnt FROM diaries
INNER JOIN emotions ON diaries.emotion_id = emotions.id
INNER JOIN users ON diaries.user_id = users.id
LEFT OUTER JOIN likes ON diaries.id = likes.diary_id
WHERE diaries.is_deleted = 0 AND public = 1 AND is_completed = 1 AND likes.is_deleted = 0
GROUP BY diary_id
ORDER BY cnt DESC
I also want to see the count of zero.....

The where clause cannot include any fields from the table that you use LEFT JOIN on. This causes the LEFT JOIN to convert into an INNER JOIN.
You need to move this clause into the LEFT JOIN statement:
AND likes.is_deleted = 0
This will then return a COUNT(likes = 0)

Related

How to substract 2 columns from different table with using count function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Currently, I am using count function to calculate the value for 2 columns and wanna substract the 2 columns and export to excel file.
This is my code. I think the bold statement contains logical error.
SELECT software.*,
(SELECT count(lkey.k_id) FROM lkey WHERE lkey.s_id = software.s_id) AS Total,
(SELECT count(assign.k_id) FROM assign WHERE assign.s_id = software.s_id) AS Installed,
**(SELECT count(lkey.k_id) - count(assign.k_id) FROM lkey INNER JOIN assign
WHERE lkey.k_id = assign.k_id GROUP BY lkey.k_id) AS Available**
FROM software";
I think you want two LEFT JOINs on aggregate queries:
SELECT s.*,
COALESCE(l.total, 0) total,
COALESCE(a.installed, 0) installed,
COALESCE(l.total, 0) - COALESCE(a.installed, 0) available
FROM software s
LEFT JOIN (SELECT s_id, count(*) total FROM lkey GROUP BY s_id) l
ON l.s_id = s.s_id
LEFT JOIN (SELECT s_id, count(*) installed FROM assign GROUP BY s_id) a
ON a.s_id = s.s_id
In very recent versions of MySQL, you could use lateral joins:
SELECT s.*, l.total, a.installed, l.total - a.installed available
FROM software s
LEFT JOIN LATERAL (SELECT count(*) total FROM lkey l WHERE l.s_id = s.s_id) l ON 1
LEFT JOIN LATERAL (SELECT count(*) installed FROM assign a WHERE a.s_id = s.s_id) a ON 1

How to add two tables having sum of amounts on the basis of year in SQL(toad)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
First Table :
SELECT A.BU,A.PROJECT,SUM(A.BUDGET_AMOUNT),SUBSTR(A.BUDGET_PERIOD,3,2)
FROM BUDGET_SUMMARY A
GROUP BY A.BU,A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
And the second table:
SELECT B.BU,A.PROJECT,SUM(B.ACTUAL_AMOUNT),SUBSTR(B.ACTUAL_PERIOD,3,2)
FROM ACTUAL_SUMMARY B
GROUP BY B.BU,B.PROJECT,SUBSTR(B.ACTUAL_PERIOD,3,2)
Now,I want to join the both above mentioned table into one table in the form of
BU|PROJECT|SUM(BUDGET_AMOUNT)|SUBSTR(A.BUDGET_PERIOD,3,2)|SUM(B.ACTUAL_AMOUNT)|SUBSTR(B.ACTUAL_PERIOD,3,2)
SELECT A.BU,A.PROJECT, SUM(A.BUDGET_AMOUNT), SUBSTR(A.BUDGET_PERIOD,3,2), SUM(B.ACTUAL_AMOUNT)
FROM BUDGET_SUMMARY A
GROUP BY A.BU,A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
Or of the two table are not for the same value you can use JOIN (inner join for amtching values)
SELECT A.BU,A.PROJECT,SUM(A.BUDGET_AMOUNT),SUBSTR(A.BUDGET_PERIOD,3,2), T.SUM_BUDGET_AMOUNT, T.SUB_ACTUAL_PERIOD
FROM BUDGET_SUMMARY A
INNER JOIN (
SELECT B.BU,A.PROJECT,SUM(B.ACTUAL_AMOUNT) sum_BUDGET_AMOUNT,SUBSTR(B.ACTUAL_PERIOD,3,2) SUB_ACTUAL_PERIOD
FROM ACTUAL_SUMMARY B
GROUP BY B.BU,B.PROJECT,SUBSTR(B.ACTUAL_PERIOD,3,2)
) T ON T.BU = A.BY
AND T.PROJECT = A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
AND T.SUB_ACTUAL_PERIOD = ,SUBSTR(A.BUDGET_PERIOD,3,2)
GROUP BY A.BU,A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
left join if you have aprtila match
SELECT A.BU,A.PROJECT,SUM(A.BUDGET_AMOUNT),SUBSTR(A.BUDGET_PERIOD,3,2), T.SUM_BUDGET_AMOUNT, T.SUB_ACTUAL_PERIOD
FROM BUDGET_SUMMARY A
LEFT JOIN (
SELECT B.BU,A.PROJECT,SUM(B.ACTUAL_AMOUNT) sum_BUDGET_AMOUNT,SUBSTR(B.ACTUAL_PERIOD,3,2) SUB_ACTUAL_PERIOD
FROM ACTUAL_SUMMARY B
GROUP BY B.BU,B.PROJECT,SUBSTR(B.ACTUAL_PERIOD,3,2)
) T ON T.BU = A.BY
AND T.PROJECT = A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
AND T.SUB_ACTUAL_PERIOD = ,SUBSTR(A.BUDGET_PERIOD,3,2)
GROUP BY A.BU,A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
just guessing, your description is hard to read and not executable. named-sub-queries should help to get your desired result (assuming "project" can be used as join-column)
with budget as
(
SELECT A.BU,A.PROJECT,SUM(A.BUDGET_AMOUNT) sum_amount,SUBSTR(A.BUDGET_PERIOD,3,2) m_period FROM BUDGET_SUMMARY A GROUP BY A.BU,A.PROJECT,SUBSTR(A.BUDGET_PERIOD,3,2)
), actual as
(
SELECT B.BU,B.PROJECT,SUM(B.ACTUAL_AMOUNT) sum_amount,SUBSTR(B.ACTUAL_PERIOD,3,2) m_period FROM ACTUAL_SUMMARY B GROUP BY B.BU,B.PROJECT,SUBSTR(B.ACTUAL_PERIOD,3,2)
)
select budget.BU,budget.PROJECT,budget.sum_amount,budget.m_period,actual.sum_amount,actual.m_period
from budget
join actual on budget.project=actual.project
(possibly a left-join is needed)

mysql query with joins, order by and limits [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am running a query in mysql which contains JOIN, ORDER BY and LIMIT,
The Query is
SELECT r.* FROM register r INNER JOIN payments p ON r.matri_id =
p.pmatri_id WHERE p.p_plan LIKE '%Free%' ORDER BY r.index_id DESC
LIMIT 0, 10
where register table contains almost 5098 Records and payments table contains almost 5023 records, Now the thing happening is when I am running this Query it's taking ages to take the result
Its taking almost 102 seconds to 147 seconds (If I am putting the limit on 10 too).
And When I am trying to run
select * from payments
or
select * from register
query from register or payments, the whole table results comes within 0.01 seconds.
I had also tried modifying the above query, everything is working fine if I am removing the ORDER BY, After Removing the order by with joins, its taking only 0.04 seconds or less..
I don't know where I am missing or where I have left something, Please review the query and tell me the solution.
Thanks in Advance...
Just a re-write, using left joins.
SELECT r.*,
m.mtongue_id, m.mtongue_name,
rel.religion_id, rel.religion_name,
c.caste_id, c.caste_name,
con.country_id, con.country_name,
st.state_id, st.state_name,
ct.city_id, ct.city_name,
des.desg_id, des.desg_name,
ocp.ocp_id, ocp.ocp_name
FROM register r
LEFT JOIN mothertongue m ON r.m_tongue = m.mtongue_id
LEFT JOIN religion rel ON r.religion = rel.religion_id
LEFT JOIN caste c ON r.caste = c.caste_id
LEFT JOIN country con ON r.country_id = con.country_id
LEFT JOIN state st ON r.state_id = st.state_id
LEFT JOIN city ct ON r.city = ct.city_id
LEFT JOIN designation des ON r.designation = des.desg_id
LEFT JOIN occupation ocp ON r.occupation = ocp.ocp_id
WHERE r.matri_id = 'MF1202'
Assuming that the joined id fields are all the primary key in their table.

SQL Count joining table with WHERE clause [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need some help regarding counting the data from.
Here is my query so far:
SELECT COUNT(*) FROM (SELECT COUNT(biodata.Nomor_Induk) as jumData
FROM biodata
INNER JOIN statuspegawai ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
WHERE statuspegawai.bagian='IT'
GROUP BY biodata.Nomor_Induk)
I have got two tables: biodata and statuspegawai.
Both of tables have the Nomor_Induk column.
Use the code here :
-missing table alias !
select count(*) FROM (
SELECT COUNT(biodata.Nomor_Induk) as jumData
from biodata INNER JOIN
statuspegawai
ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
where statuspegawai.bagian='IT'
Group By biodata.Nomor_Induk
) as Alias1;
Your query needs a subquery table alias:
select count(*)
FROM (SELECT COUNT(biodata.Nomor_Induk) as jumData
from biodata INNER JOIN
statuspegawai
ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
where statuspegawai.bagian = 'IT'
Group By biodata.Nomor_Induk
) t;
You can also express this without a subquery:
select COUNT(distinct biodata.Nomor_Induk) as jumData
from biodata INNER JOIN
statuspegawai
ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
where statuspegawai.bagian = 'IT';

Select distinct column SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So, I am making a query using mutiple tables and i want to get a distinct product name("designacao") however I've already tried adding DISTINCT and RIGHT JOINs but it didn't work.
Please check this SQLFiddle with the Query and the data
I want the same query as the provided but with DISTINCT "designacao"
Try this way:
select produtos.designacao, produtos.marca,produtos.categoria, lojas.superficie, lojas.localizacao, produtos_lojas.preco
from produtos_lojas
inner join lojas on lojas.id = produtos_lojas.id_loja
inner join produtos on produtos.id = produtos_lojas.id_produto
where produtos_lojas.preco=
(
select min(pl.preco)
from produtos_lojas pl
inner join lojas l on l.id = pl.id_loja
inner join produtos p on p.id = pl.id_produto
where produtos.designacao = p.designacao
)
SQL Fiddle DEMO
DISTINCT keyword will get eliminate any duplicate row. Since your rows are different due to the other columns you are selecting, you will only be able to pick up each designacao once if you include only the designacao column in your query.