Inner join to select MAX value from the SUM of Rows - mysql

http://sqlfiddle.com/#!2/1a2df
I am trying to select a column "photo_name" by counting the MAX value of SUM(valid)
the preferred results is "test5.jpg" but after hours of trying, I still can't figure it out ,
below is my previous approach, but it doesn't work
SELECT photo_name FROM
(
SELECT a.*
FROM test a
INNER JOIN
(
SELECT *, SUM(valid) v
FROM test
WHERE page_id = 3 AND `valid` = 1
) b ON MAX(b.v)
)c
Please help,

Something like this should work. It sums the valid column for each photo, orders high-to-low for the sum, then limits results to the top row:
SELECT photo_name, SUM(valid) AS sum_valid
FROM test
GROUP BY photo_name
ORDER BY sum_valid DESC
LIMIT 1

Related

How can I join SQL subqueries as they are?

I have 3 subqueries that when executed independently they all return 3 rows with the desired columns and values. Once I put them all in the from statement and select them all
SELECT
*,
ROUND(Verbrecher / Buerger * 100, 1) AS Sicherheitsgrad
FROM
(SELECT name AS Dorf
FROM dorf
GROUP BY dorfnr) AS Dorf,
(SELECT COUNT(*) AS Verbrecher
FROM bewohner
WHERE status LIKE 'boese'
GROUP BY dorfnr) AS Verbrecher,
(SELECT COUNT(*) AS Buerger
FROM bewohner
GROUP BY dorfnr) AS Buerger
This is the result of all three subqueries being respectively executed standalone
Standalone
This is the result
Snippet above being run
I expect them to be joined together and have three rows with the queries aligned horizontally.
That unfortunately is not the given result.
I hope this makes sense to a certain extent.
Maybe you need in this:
SELECT dorfnr, Dorf, Verbrecher, Buerger,
ROUND(Verbrecher / Buerger * 100, 1) AS Sicherheitsgrad
FROM ( SELECT dorfnr, name AS Dorf
FROM dorf
-- GROUP BY dorfnr
) AS Dorf
JOIN ( SELECT dorfnr, COUNT(*) AS Verbrecher
FROM bewohner
WHERE status LIKE 'boese'
GROUP BY dorfnr
) AS Verbrecher USING (dorfnr)
JOIN ( SELECT dorfnr, COUNT(*) AS Buerger
FROM bewohner
GROUP BY dorfnr
) AS Buerger USING (dorfnr)

Searching and Sorting Using MySQL Inner Join

I guess I can't explain my problem properly. I want to explain this to you with a picture.
Picture 1
In the first picture you can see the hashtags in the trend section. These hashtags are searched for the highest total and it is checked whether the date has passed. If valid data is available, the first 5 hashtags are taken.
Picture 2
In the second picture, it is checked whether the posts in the hashtag are in the post, if any, the oldest date value is taken, LIMIT is set to 1 and the id value from the oyuncular table is matched with sid. Thus, the name of the person sharing can be accessed.
Picture 3
My English is a little bad, I hope I could explain it properly.
SELECT
social_trend.hashtag,
social_trend.total,
social_trend.tarih,
social_post.sid,
social_post.tarih,
social_post.post,
oyuncular.id,
oyuncular.isim
FROM
social_trend
INNER JOIN
social_post
ON
social_post.post LIKE '%social_trend.hashtag%' ORDER BY social_post.tarih LIMIT 1
INNER JOIN
oyuncular
ON
oyuncular.id = social_post.sid
WHERE
social_trend.tarih > UNIX_TIMESTAMP() ORDER BY social_trend.total DESC LIMIT 5
YOu should use a sibquery
and add a proper join between subqiery and social_trend
(i assumed sing both sid)
SELECT
social_trend.hashtag,
social_trend.total,
social_trend.tarih,
t.sid,
t.tarih,
t.post,
oyuncular.id,
oyuncular.isim
FROM (
select social_post.*
from social_post
INNER JOIN social_trend ON social_post.post LIKE concat('%',social_trend.hashtag,'%' )
ORDER BY social_post.tarih LIMIT 1
) t
INNER JOIN social_trend ON social_trend.hashtag= t.post
INNER JOIN oyuncular ON oyuncular.id = t.sid
WHERE
social_trend.tarih > UNIX_TIMESTAMP() ORDER BY social_trend.total DESC LIMIT 5
but looking to your new explanation and img seems you need
SELECT
t.hashtag,
t.total,
t.tarih_trend,
t.sid,
t.tarih,
t.post,
oyuncular.id,
oyuncular.isim
FROM (
select social_post.sid
, social_post.tarih
, social_post.post
, st.hashtag
, st.total
, st.tarih tarih_trend
from social_post
INNER JOIN (
select * from social_trend
WHERE social_trend.tarih > UNIX_TIMESTAMP()
order by total DESC LIMIT 5
) st ON social_post.post LIKE concat('%',st.hashtag,'%' )
ORDER BY social_post.tarih LIMIT 5
) t
INNER JOIN oyuncular ON oyuncular.id = t.sid

Count non null values from a left joint table

Count non-null values directly from select statement (not using where) on a left joint table
count(*) as comments Need this to provide count of non-null values only. Also, inner join is not a solution because, that does not include content which have zero comments in count(distinct (t1.postId)) as no_of_content
select t1.tagId as tagId, count(distinct (t1.postId)) as no_of_content, count(*) as comments
from content_created as t1
left join comment_created as t2
on t1.postId=t2.postId
where
( (t1.tagId = "S2036623" )
or (t1.tagId = "S97422" )
)
group BY 1
Though Posting the sample data might help us more to answer this but you can update your count function to -
COUNT(CASE WHEN postId IS NULL THEN 1 END) as comments
Count only counts non-null values. What you need to do is reference the right hand side table's column explicitly. So instead of saying count(*) use count(right_joined_table.join_key).
Here's a full example using BigQuery:
with left_table as (
select num
from unnest(generate_array(1,10)) as num
), right_table as (
select num
from unnest(generate_array(2,10,2)) as num
)
select
count(*) as total_rows,
count(l.num) as left_table_counts,
count(r.num) as non_null_counts
from left_table as l
left outer join right_table as r
on l.num = r.num
This gives you the following results:

Combining two select statements without on clausule

I have two tables that are not related at all
First table is called pocetna_baner that stores 3 pictures that are going to be displayed on index page.
pocetna_baner table:
Second table is for blog and I want to take information of 3 blogs ordered by datum desc:
blog table
I tried using UNION, UNION ALL, JOIN without clauses, full outer join, but just cannot get it to work because I never had this situation where there is no on clause
union all with queries that have a different number of columns -> I searched for answers and got to this one which seemed okay but unfortunately couldn't get it to work
This is what I have by now
(SELECT null as blog_id, null as naslov, null as tekst1, null as kategorija, p.ime_slike FROM
pocetna_baner p )
UNION ALL
(SELECT b.blog_id, b.naslov, b.tekst1, b.kategorija ,null as ime_slike
FROM blog b ORDER BY b.datum DESC LIMIT 3 )
And result is this
My expected output is only to take b.blog_id, b.naslov, b.tekst1, b.kategorija FROM blog b ordered by datume desc limit 3 and add column with ime_slike from second table
You can construct the ON clause by creating 1 counter column in each table and matching on them:
select
b.blog_id, b.naslov, b.tekst1, b.kategorija, p.ime_slike
from (
select
blog_id, naslov, tekst1, kategorija,
(#row_number1:=#row_number1 + 1) num
from blog, (select #row_number1:=0) t
order by datum desc limit 3
) b left join (
select ime_slike,
(#row_number2:=#row_number2 + 1) num
from pocetna_baner, (select #row_number2:=0) t
order by id limit 3
) p on p.num = b.num
If you are sure that the ids in the table pocetna_baner have values 1, 2 and 3 then you can simplify to this:
select
b.blog_id, b.naslov, b.tekst1, b.kategorija, p.ime_slike
from (
select
blog_id, naslov, tekst1, kategorija,
(#row_number1:=#row_number1 + 1) num
from blog, (select #row_number1:=0) t
order by datum desc limit 3
) b left join pocetna_baner p
on p.id = b.num
I have just given an example by this code.
Please change as per needs.
SELECT * FROM (
SELECT p.ime_slike,null as blog_id,null as naslov,null as kategorija FROM
pocetna_baner p
UNION ALL
SELECT b.blog_id, b.naslov, b.tekst1, b.kategorija ,null as ime_slike
FROM blog b
ORDER BY datum DESC LIMIT 3 ) AS k

Mysql need to select all values having the biggest number of multiple entries

My mysql query
SELECT count(ICD10) as columnDis, ICD10
FROM(
SELECT tbl_disease.ICD10, tbl_symptom_disease.symptomCode
FROM `tbl_disease`
JOIN tbl_symptom_disease
ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
WHERE tbl_symptom_disease.symptomCode='P18'
OR tbl_symptom_disease.symptomCode='P19'
) AS tbl_ICD10_symCode
GROUP BY ICD10
HAVING columnDis=?????
So this query returns all the ICD10 entries having duplicate values equal with ?????.
In this example the maximum number of duplicates is 2, but I want to use it for different sympomCode(s) in which case there could be 3 duplicate entries that appear 5 times, 5 other ones that appear 3 times, 1 that appears 2 times and so on. So what I need is only the duplicate entries that appear the MOST times. Something like:
HAVING columnDis=(MAX(columnDis))
but that of course doesn't work. Can anyone help me please?
Using a sub query to get the max count, then joining that against the main sub query
SELECT ICD10
FROM
(
SELECT MAX(disease_count) AS max_disease_count
FROM
(
SELECT tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count
FROM `tbl_disease`
JOIN tbl_symptom_disease
ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
WHERE tbl_symptom_disease.symptomCode='P18'
OR tbl_symptom_disease.symptomCode='P19'
GROUP BY ICD10
) sub0
) sub1
INNER JOIN
(
SELECT tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count
FROM `tbl_disease`
JOIN tbl_symptom_disease
ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
WHERE tbl_symptom_disease.symptomCode='P18'
OR tbl_symptom_disease.symptomCode='P19'
GROUP BY ICD10
) sub2
ON sub1.max_disease_count = disease_count