Complicated mysql query across two tables - mysql

Table 1: invTypes with columns typeID,groupID,typeName,description
Table 2: item_value with columns typeID,volume,avg,max,min,stddev,percentile,updated_on
I need to return all of the above columns where the latest updated_on (datetime) field as the deciding factor. I would like to return the latest datetime for each day with all of the information from table 1 and table 2 as the result.

This is how you get the rows corresponding to the most recent updated_on for each date in your item_value table:
SELECT
invTypes.*,
item_value.*
FROM
item_value
INNER JOIN
(
SELECT
MAX(updated_on) AS `updated_on`
FROM
item_value
GROUP BY
DATE(updated_on)
) latest
ON
latest.updated_on = item_value.updated_on
INNER JOIN
invTypes
ON
invTypes.typeID = item_value.typeID
ORDER BY
item_value.updated_on

SELECT * FROM table1
LEFT JOIN table1.uniqueid = table2.uniqueid ON table2
ORDER BY update_on DESC
GROUP BY (DATE(updated_on))

Related

SQL Select two records if they have certain time difference of column A and have same column B value

I have 14000 records in my sql table. They have columns ID, test_subject_id and date_created. I want to fetch all the records that have been created within a time difference of 3 minutes(difference in date_created values) and both records should have the same test_subject_id.
You should use a self join, I assume inner join is what will work for you:
SELECT a.ID, a.date_created, b.ID, b.date_created
FROM accounts a
INNER JOIN accounts b
ON a.test_subject_id = b.test_subject_id
AND TIMESTAMPDIFF(MINUTE,a.date_created,b.date_created) = 3
Note: TIMESTAMPDIFF is used assuming date_created has type datetime, details here.
You can use EXISTS:
SELECT t1.*
FROM tablename t1
WHERE EXISTS (
SELECT 1
FROM tablename t2
WHERE t2.test_subject_id = t1.test_subject_id
AND ABS(TIMESTAMPDIFF(SECOND, t1.date_created, t2.date_created)) <= 180
)
ORDER BY t1.test_subject_id, t1.date_created;

Get column values less than max date

I want to make a query where I want to get all the values from a table1 prior to the max date from the date column.
I have table1 with column names as id,ord_date.
Further, I want to join it to another table2 with column as id, name
SELECT s.title,s.title_id,t.ord_date
FROM titles s
INNER JOIN (
SELECT title_id
,max(ord_date) AS ord_date
FROM sales
where ord_date < max(ord_date)
GROUP BY title_id
) t ON s.title_id = t.title_id
I want solution query showing title from table2 and dates from table1
Query.
SELECT s.title,s.title_id,t.ord_date
FROM titles s
INNER JOIN (
SELECT x.title_id, max(x.ord_date) AS ord_date
FROM sales x
where x.ord_date < (
select max(m.ord_date) from sales m where m.title_id = x.title_id
)
GROUP BY title_id
) t ON s.title_id = t.title_id

Need my table to create only one row per id

I need to join two tables, which are:
TABLE 1
t1_id | t1_text | t1_date
TABLE 2
t2_id | t2_text | t2_date | t1_id
What i want to get:
t1_text | t1_date | t2_text | t2_date
table has to show most recent, unique, t1 rows, together with their linked t2_text and its t2_date
This is what ive got so far:
SELECT `table_1`.`t1_text` AS 'Text', `table_1`.`t1_date` AS 't1_date', `table_2`.`t2_text` AS 't2_Text', `table_2`.`t2_date` AS 'Date'
FROM `table_1`
LEFT JOIN `table_2` ON `table_1`.`t1_id`=`table_2`.`t1_id`
ORDER BY `table_1`.`t1_date` DESC
LIMIT 10
Its close, but no cigar. I still get the same t1 rows to show up more than once
I am not sure I understand clearly what is your desired output. Try the following:
SELECT news.text AS 'News', news.date AS 'Publish date',
comments.text AS 'Most Recent Comment', comments.date AS 'Comment Date'
FROM news
JOIN (
SELECT text, MAX(newsId) as newsId, date
FROM comments
GROUP BY newsId
)comments ON news.newsId=comments.newsId
WHERE comments.text IS NOT NULL
ORDER BY news.date DESC LIMIT 10
With clarification regarding the uniqueness of the id fields:
select t1.t1_id, t1.t1_date, t1.t1_text, t2.t2_id, t2.t2_date, t2.t2_text
from table_1 t1
inner join (SELECT t1_id, max(t1_date) as date from table_1) t1m
on t1.t1_id = t1m.t1_id and t1.t1_date = t1m.date
inner join table_2 t2
on t1.t1_id = t2.t1_id
inner join (SELECT t2_id, max(t2_date) as date from table_2) t2m
on t2.t2_id = t2m.t2_id and t2.t2_date = t2m.date;
In this query the inline tables select the ids with the max date, which are then joined to the original table to get the remaining fields.
There still may be duplicate t1_id's however if the combination of t1_id and t1_date is not unique. But in that case I don't think that duplication should be hidden, but rather adressed.
Original answer:
You need to use an inner join rather than a left join.
SELECT `table_1`.`t1_text` AS 'Text', `table_1`.`t1_date` AS 't1_date', `table_2`.`t2_text` AS 't2_Text', `table_2`.`t2_date` AS 'Date'
FROM `table_1`
INNER JOIN `table_2`
ON `table_1`.`t1_id`=`table_2`.`t1_id`
ORDER BY `table_1`.`t1_date` DESC LIMIT 10
Also, some of your quote characters are not consistent, different databases may require different quote characters to delimit names, but the mix might give you problems.

2 rows having unique field and recent date

Table :
I am new to query writing. Now I am stuck on retrieving 2 rows from above table.
Data will be date sorted in descending order for only 2 different topic_id. There won't be a third different topic_id.
So I want to retrieve two rows only that will have different topic_id, one data for each topic_id having most recent date.
The result would be
try sql fiddle
http://sqlfiddle.com/#!2/f37963/9
SELECT t1.* FROM temp t1
JOIN (SELECT question_id, MAX(`date`) as `date` FROM temp GROUP BY topic_id) t2
ON t1.question_id= t2.question_id AND t1.`date`= t2.`date`;
The logic is to find the latest date in each group (subquery) and join it with the table again to retrieve other particulars.
use this
$qry="SELECT * FROM table_name GROUP BY TOPIC_ID ORDER BY DATE desc";

Mysql can't join two tables columns into one show

I want to show two columns summarize data.
table1 - count all fields that the id same as the id on the show_users table.
table2 - sum all values that the id same as the id on the show_users table.
This is my query:
SELECT show_users.id, COUNT(`table1`.id) as sum_fields , SUM(`table2`.count) as count_all
FROM `show_users`
LEFT JOIN `table1` ON `show_users`.id = `table1`.id
LEFT JOIN `table2` ON `show_users`.id = `table2`.id
GROUP by show_users.id
ORDER BY sum_fields DESC
The table2 results are fine, but the table1 count isn't correct values...
Why is that?
SELECT show_users.id, COUNT(DISTINCT `table1`.id) as sum_fields , SUM(`table2`.count) as count_all