I have a list of records (domestic_helper_idcards) and I want to return only one card per staff (domestic_helper_id) that is not deleted (is_deleted = 0), and that has the card_expiration_date furthest in the future (latest expiry date).
Have tried grouping and so on, but cant get it to work. Code below:
SELECT * FROM domestic_helper_idcard
where
is_deleted = 0
order by card_expiration_date desc
This returns the following (image):
I want only records with ID 4 and 5 to be returned. Anyone?
You could use a join with the subquery grouped by domestic_helper_id with an aggregated function eg: max()
SELECT d.*
FROM domestic_helper_idcard d
inner join (
select domestic_helper_id, max(id) max_id
from domestic_helper_idcard
where is_deleted = 0
group by domestic_helper_id
) t on t.domestic_helper_id = d.domestic_helper_id and t.max_id = d.id
order by d.card_expiration_date desc
and as suggested by Jens after clarification using max card_expiration_date
SELECT d.*
FROM domestic_helper_idcard d
inner join (
select domestic_helper_id, max(card_expiration_date) max_date
from domestic_helper_idcard
where is_deleted = 0
group by domestic_helper_id
) t on t.domestic_helper_id = d.domestic_helper_id and t.max_date = d.max_date
order by d.card_expiration_date desc
Related
I am currently trying to query the fips code and sum of the amount of cases for that code from the cases table and the id of the fips code for the subquery from the dim_counties table.
Here's what I have so far that has worked:
SELECT DISTINCT C.FIPS,
SUM(C.CASES) AS SUMS
FROM DIM_DATE_MONTH B, CASES C
where 1=1
and B.DATE_SHORT = SUBSTR(TO_CHAR(C.DAY_OF, 'MONTH'), 1,3)
AND B.DATE_YR = SUBSTR(EXTRACT(YEAR FROM C.DAY_OF),1,4)
AND SUBSTR(TO_CHAR(C.DAY_OF, 'MONTH'), 1,3) = 'JAN'
AND SUBSTR(EXTRACT(YEAR FROM C.DAY_OF),1,4) = '2020'
GROUP BY C.FIPS
ORDER BY SUMS DESC
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
What I have attempted that doesn't work
SELECT A.COUNTY_NBR,
A.FIPS,
D.SUMS
FROM DIM_COUNTIES A
INNER JOIN(
SELECT DISTINCT C.FIPS,
SUM(C.CASES) AS SUMS
FROM DIM_DATE_MONTH B, CASES C
where 1=1
and B.DATE_SHORT = SUBSTR(TO_CHAR(C.DAY_OF, 'MONTH'), 1,3)
AND B.DATE_YR = SUBSTR(EXTRACT(YEAR FROM C.DAY_OF),1,4)
AND SUBSTR(TO_CHAR(C.DAY_OF, 'MONTH'), 1,3) = 'JAN'
AND SUBSTR(EXTRACT(YEAR FROM C.DAY_OF),1,4) = '2020'
GROUP BY C.FIPS
ORDER BY SUMS DESC
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY)D ON D.SUM) D ON C.FIPS = A.FIPS;
Is there a way to adjust the following MYSQL query so that it only shows results where the video count is greater than 0? I have tried the following but it doesn't recognise
video_count
SELECT channels.*,
(SELECT COUNT(*) FROM videos WHERE videos.video_publisher_id = channels.channel_id) as `video_count`
FROM channels
WHERE channel_active = 1
AND video_count > 0
AND channel_thumbnail IS NOT NULL
ORDER BY channel_subscribers DESC
Move your subquery from the SELECT clause to the FROM clause. An inner join guarantees matches.
SELECT c.*, v.video_count
FROM channels c
JOIN
(
SELECT video_publisher_id, COUNT(*) AS video_count
FROM videos
GROUP BY video_publisher_id
) v ON v.video_publisher_id = c.channel_id
WHERE c.channel_active = 1
AND c.channel_thumbnail IS NOT NULL
ORDER BY c.channel_subscribers DESC;
Maybe this will help you.
SELECT channels.*,video_count.count
FROM channels
left join (SELECT channel_id,COUNT(*) count FROM videos) as `video_count` on video_count.video_publisher_id = channels.channel_id
WHERE channel_active = 1
AND video_count.count > 0
AND channel_thumbnail IS NOT NULL
ORDER BY channel_subscribers DESC
For filter by subquery result or aggregation function value you can use HAVING. You have error in WHERE clause because MySQL don't know about this column in WHERE
SELECT channels.*,
(SELECT COUNT(*) FROM videos WHERE videos.video_publisher_id = channels.channel_id) as `video_count`
FROM channels
WHERE channel_active = 1
AND channel_thumbnail IS NOT NULL
HAVING video_count > 0
ORDER BY channel_subscribers DESC
Or:
SELECT channels.*, COUNT(*) AS video_count
FROM channels
INNER JOIN videos
ON videos.video_publisher_id = channels.channel_id
WHERE channel_active = 1
AND channel_thumbnail IS NOT NULL
GROUP BY channels.channel_id
ORDER BY channel_subscribers DESC
I have two tables 'sites' and 'index_log'. Table 'sites' is a information about sites(userid, name, description ...) Table index_log have columns date, index_count and siteid. So I want to get last and previous index_log row for each site, where userid = 10. This's my variant:
SELECT ff.id,
ff.siteurl,
ff.last_count,
ff.last_date,
il2.index_count as previous_count,
MAX(il2.date) as previous_date
FROM (
SELECT s.siteurl,
s.id,
il.index_count as last_count,
MAX(il.date) as last_date
FROM sites s
LEFT JOIN index_logs il ON il.siteid = s.id
WHERE s.userid = 10
GROUP BY s.id
) as ff
LEFT JOIN index_logs il2 ON il2.siteid = ff.id AND il2.date < ff.last_date
GROUP BY ff.id
But in this variant index_count column(last and previous) do not match with max date row. I hope for your help.
this will gives you last 2 log entries per sites.id
; with CTE as
(
SELECT s.siteurl, s.id, il.index_count, il.date,
RN = ROW_NUMBER() OVER (PARTITION BY s.id ORDER BY il.date DESC)
FROM sites s
JOIN index_logs il ON il.siteid = s.id
WHERE s.userid = 10
)
SELECT *
FROM CTE
WHERE RN <= 2
SELECT Max(c.vendor_id),c.vendors_id FROM (SELECT distinct a.vendor_id FROM service_master a,products b,vendors v,`vendor_addresses` ad WHERE a.cat_id= 242 AND a.service_id = b.s_sid AND a.is_active =1 AND b.isproductactive = 1 AND v.vendorid = a.vendor_id AND ad.vendorchild_id = a.vendor_id AND v.isvendoractive = 1 LIMIT 10) c ORDER BY c.vendor_id
Questions:
1)I want full result in vendor_id column
2)Max(vendor_id)result
How to get result in single query?
Not tested. But please try this.
select t1.id,t2.id
from detail t1
left join(
select max(id) as id
from detail
) t2 on 1 = 1
select id, ( select max(id) from detail internal_detail
where detail.id = internal_detail.id ) as max from detail
I have a query that has a sub query that returns a count of records from another table, I'm having trouble ordernar the largest number of this counter
SELECT respostas.id,
respostas.cmm,
respostas.topico,
respostas.usuario,
respostas.resposta,
perfis.nome,
perfis.sobrenome,
respostas.datahora,
(
SELECT count(id)
FROM likes
WHERE respostas.id = resposta
) AS total
FROM respostas
INNER JOIN perfis ON respostas.usuario = perfis.id
INNER JOIN likes ON respostas.topico = likes.topico
WHERE respostas.cmm = 28
AND respostas.topico = 38
ORDER BY respostas.id ASC, total ASC
LIMIT 0,20`enter code here`
I want to sort by the total column and can not.
Sorting by total does not work, only ordered by id
You can choose which column to order by numerically:
SELECT
(
SELECT count(id)
FROM likes
WHERE respostas.id = resposta
) AS total,
respostas.id,
respostas.cmm,
respostas.topico,
respostas.usuario,
respostas.resposta,
perfis.nome,
perfis.sobrenome,
respostas.datahora
FROM respostas
INNER JOIN perfis ON respostas.usuario = perfis.id
INNER JOIN likes ON respostas.topico = likes.topico
WHERE respostas.cmm = 28
AND respostas.topico = 38
ORDER BY 1, respostas.id
LIMIT 0,20
What is the purpose of Order By 1 in SQL select statement?