Select distinct column SQL [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 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.

Related

I also want to see the count of zero [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
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)

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.

Is there a way to make this MySQL query more efficient? [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 6 years ago.
Improve this question
select * from q_and_a qa
where keyword = :keyword_top
or id in (
select qk.question_id
from question_keywords qk
inner join q_and_a qa on qa.id = qk.question_id
inner join keywords k on k.id = qk.keyword_id
where k.keyword = :keyword_bottom
)
If I'm understanding your query correctly, you don't need the subquery, but rather outer joins:
select q.*
from q_and_a q
left join question_keywords qk on q.id = kq.question_id
left join keywords k on k.id = gk.keyword_id
where q.keyword = :keyword_top or
k.keyword = :keyword_bottom
You may need to use distinct with this, depends on the sample data and desired results.
Actually since this is really a question about performance, this may or may not make it faster. As usual, it just depends.
A couple other variations to your existing query would include using exists or using union all instead of or. You have to run the comparisons to see what works best in your situation. Also your indices and keys need to be defined appropriately.
It might be cleaner for the sql engine to do this as a left join as opposed to you doing a subquery.
SELECT qa.*
FROM q_and_a qa
LEFT JOIN question_keywords qk
ON qa.id = qk.question_id
LEFT JOIN keywords k
ON k.id = qk.keyword_id
WHERE qa.keyword = :keyword_top
OR k.keyword = :keyword_bottom

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';