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
Related
i want to sum the item sorted by master item and it should be sum different for each item, all i can do is like this, could you give me some help to fix my query?
this is the query
WITH Tb0 as
( SELECT masuk.no_masuk as no_bukti,masuk.tanggal,masuk_detail.kode_bahan,masuk_detail.qty as qty_masuk,0 'qty_keluar' from masuk,masuk_detail WHERE masuk.no_masuk=masuk_detail.no_masuk
UNION
SELECT keluar.no_keluar as no_bukti,keluar.tanggal,keluar_detail.kode_bahan,0 'qty_masuk',keluar_detail.qty as qty_keluar from keluar,keluar_detail WHERE keluar.no_keluar=keluar_detail.no_keluar
)
, Tb1 as
(
SELECT tanggal,kode_bahan,qty_masuk,qty_keluar,Row_Number() over (order by tanggal asc) as OrderId
FROM
Tb0
)
SELECT T1.kode_bahan,T1.tanggal,T1.qty_masuk,T1.qty_keluar,(Sum(T2.qty_masuk) - Sum(T2.qty_keluar)) as Balance FROM Tb1 as T1
INNER JOIN
Tb1 as T2
ON T1.OrderId >= T2.OrderId
Group By T1.kode_bahan,T1.tanggal,T1.qty_masuk,T1.qty_keluar
Order BY tanggal
and this is the result
result query
all i want is sorted by kode_bahan(master item) and tangggal(date) like this
result i want
I have two tables (table 1 and table 2). Table 1 consists the options and table 2 consists the result for each options.
**table1** table2
id id
optionvalue optionvalueid
time (unixtime)
So when the data is inserted it will be stored in table2. There are 5 optionvalue in table 1 and when data is inserted, then in the table2 it will insert the optionvalueid of table 1 and timestamp in unixtimestamp. Eaach month, I want to count the number of values for each optionvalue. Evene if there is no value for an optionvalue, I still want to see count as zero.
I did the following query but only return the value with rows with data only.
SELECT
po.id,po.optionvalue, COUNT(pr.optionvalueid) as votes,
FROM_UNIXTIME(`time`, '%m-%Y') as ndate
FROM table 1
LEFT JOIN table 2 pr ON po.id=pr.optionvalueid
GROUP BY ndate, po.optionvalue ORDER BY ndate ASC
Is there any other ways to make the query so that it will return all the options even if there is no value.
You can CROSS join table1 to the distinct months of table2 and then LEFT join to table2 to aggregate:
SELECT t.ndate, t1.id, t1.optionvalue, COUNT(t2.optionvalueid) votes
FROM table1 t1
CROSS JOIN (SELECT DISTINCT FROM_UNIXTIME(`time`, '%m-%Y') ndate FROM table2) t
LEFT JOIN table2 t2 ON t1.id = t2.optionvalueid AND t.ndate = FROM_UNIXTIME(t2.`time`, '%m-%Y')
GROUP BY t.ndate, t1.id, t1.optionvalue
ORDER BY t.ndate ASC
So my table has:
column1, column2, column3 & datetime
SELECT * FROM mytable WHERE column1 = :column1
CONCAT column1 & column2 (column1NRcolumn2) to get a string to search for duplicates.
SELECT 1 column1_column2 WHERE datetime is highest
What i want is to combine two cells to create a new temponary column. (column1NRcolumn2)
Then i want to look for duplicates and then select the latest column1NRcolum2 WHERE datetime is highest/latest
How can i manage this?
You could use a subquery for concatenated columns having count > 1 and join to your table for max datetime
select t.my_concat, max(m.datetime)
from mytable m
inner join (
SELECT concat(column1,column2) my_concat
FROM mytable
group by concat(column1,column2)
having count(*)>1
) t
group by t.my_concat
and if you need thre related mytable columns you need a join on the max datetime
select m2.*
from mytable m2
inner join
(
select t.my_concat, max(m.datetime) max_datetime
from mytable m
inner join (
SELECT concat(column1,column2) my_concat
FROM mytable
group by concat(column1,column2)
having count(*)>1
) t
group by t.my_concat
) t2 on t2.my_concat = m2. concat(column1,column2)
and t.max_datetime = m2.datetime
I have a mysql query. I need to get last value from columns Lat,Lng from my table but serial_number column needs to be distinct.
How to make such a query?
This is needed as I am using this coordinates to load it to Google map. So when the Google maps loads I need to have a marker on each last coordinates where vehicle is.
SELECT m.*
FROM (
SELECT DISTINCT serial_number
FROM mytable
) md
JOIN mytable m
ON m.id =
(
SELECT id
FROM mytable mi
WHERE mi.serial_number = md.serial_number
ORDER BY
mi.time DESC, mi.id DESC
LIMIT 1
)
Create an index on (serial_number, time, id) for this to work fast.
If you want to retrieve the last record for a certain serial_number, just use this:
SELECT *
FROM mytable
WHERE serial_number = :my_serial_number
ORDER BY
time DESC, id DESC
LIMIT 1
1#
Assuming that max ID will always give you last lat and lon, the query becomes quite simple -
SELECT t2.*
FROM table t2
where t2.id IN
(
SELECT max(t1.id)
FROM table t1
GROUP BY t1.serial_number
)
2#
If you need to consider time also, then you will need to do it this way. Here, in the inner query, max_time of each serial_number is obtained. Then this max_time and serial_number is joined with the outer table time and serial_number respectively, to get distinct records with last lat and lon.
SELECT *
FROM table t2,
(
SELECT max(t1.time) max_time, t1.serial_number
FROM table t1
GROUP BY t1.serial_number
) new_table
WHERE t2.time=new_table.max_time
AND t2.serial_number=new_table.serial_number
Try this
select distinct serial_number, *
from table t
inner join table t1 on t1.serial_number = t.serial_number and t1.id = (select max id from table t2 where t2.serial_number = t1.serial_number)
I have table with id, item_id, value (int), run (datetime) and i need select value diff betwen last two run per *item_id*.
SELECT item_id, ABS(value1 - value2) AS diff
FROM ( SELECT h.item_id, h.value AS value1, h2.value AS value2
FROM ( SELECT id, item_id, value
FROM table_name
GROUP BY item_id
ORDER BY run DESC) AS h
INNER JOIN ( SELECT id, item_id, value
FROM table_name
ORDER BY run DESC) AS h2
ON h.item_id = h2.item_id AND h.id != h2.id
GROUP BY item_id) AS h3
I believe this should do the trick for you. Just replace table_name to correct name.
Explanation:
Basicly I join the table with itself in a run DESC order, JOIN them based on item_id but also on id. Then I GROUP BY them again to remove potential 3rd and so on cases. Lastly I calculate the difference between them through ABS(value1 - value2).
SELECT t2.id, t2.item_id, (t2.value- t1.value) valueDiff, t2.run
FROM ( table_name AS t1
INNER JOIN
table_name AS t2
ON t1.run = (SELECT MAX(run) FROM table_name where run < t2.run)
and t1.item_id = t2.item_id)
This is assuming you want the diff between a record and the record with the previous run