Related
i have a mysql table with colums: id(primary), name(varchar), TIME(timestamp)
ID , NAME , TIME
i want to get just first and last log for each day
example if i have data like this
1,name,2018-20-21 12:35:00
2,name,2018-20-21 13:38:00
3,name,2018-20-21 14:25:00
4,name,2018-20-21 15:39:00
5,name,2018-20-21 21:48:00
6,name,2018-20-22 13:25:00
7,name,2018-20-22 14:39:00
8,name,2018-20-22 19:48:00
i want to get in just this
1,name,2018-20-21 12:35:00
5,name,2018-20-21 21:48:00
6,name,2018-20-22 13:25:00
8,name,2018-20-22 19:48:00
Try this:
SELECT name, MAX(time), MIN(time) FROM Table GROUP BY DATE(time);
You could use the union for the min and the max time group by date
and join this with your table
select * from my_table
inner join (
select * from (
select min(time) my_time
from my_table
group by date(time)
union
select max(time)
from my_table
group by date(time)
) t on t.my_time = my_table.time
order by my_table.time
Hope this helps.
SELECT id, tmp.name, tmp.time FROM
(SELECT id, name, min(time) as time FROM table1 GROUP BY DATE(time)
UNION ALL
(SELECT id, name, max(time) as time FROM table1 GROUP BY DATE(time)) tmp
ORDER BY tmp.time
You can try selecting the min and max for each day, since you want the entire line, a join is needed
and to filter out the actual min and max day, a aub query is needed
SELECT id, name, time
FROM
(
SELECT t2.*, MIN(DATE(t.time)) As min0 MAX(DATE(t.time)) As max0
FROM
table t
INNER JOIN table t2 ON t.id = t2.id
GROUP BY
DATE (t.time),
min0,
max0
) a
SELECT
l.id,l.name,l.time
FROM
log l
LEFT JOIN
(SELECT
max(time) as maxTime
FROM
log
GROUP BY date(time)) l1 ON l.time = l1.maxTime
LEFT JOIN
(SELECT
min(time) as minTime
FROM
log
GROUP BY date(time)) l2 ON l.time = l2.minTime
WHERE
(maxTime IS NOT NULL
OR minTime IS NOT NUll);
SELECT * from stack.log;
Having this query,
SELECT DISTINCT ID, DATE FROM MAIN_TAB ORDER BY ID ASC
How can I print the total of different combinations for ID field, on the front of every row, example TOTAL_DISTINCT_VALUES:
ID DATE TOTAL_DISTINCT_VALUES
37870 02/07/2018 3
37870 03/07/2018 3
37870 04/07/2018 3
55887 04/07/2018 2
55887 03/07/2018 2
61891 02/07/2018 1
44891 02/07/2018 1
75891 02/07/2018 1
You could use count and group by
SELECT ID, DATE, count(*) TOTAL_DISINCT_VALUE
FROM MAIN_TAB
GROUP BY ID, DATE
ORDER BY ID ASC
but looking to you data sample seems you need cont only for id so you could use a join on the count group by id
select t.id, a.date, t.TOTAL_DISTINCT_VALUE
from MAIN_TAB a
inner JOIN (
select id, count(*) TOTAL_DISTINCT_VALUE
FROM MAIN_TAB
group by ID
) t on a.id = t.id
or as suggested by barmar . count(distinct date)
select t.id, a.date, t.TOTAL_DISTINCT_VALUE
from MAIN_TAB a
inner JOIN (
select id, count(distinct date) TOTAL_DISTINCT_VALUE
FROM MAIN_TAB
group by ID
) t on a.id = t.id
use group by clause
SELECT ID, DATE,count(*) as distinct_value FROM MAIN_TAB
group by ID, DATE
ORDER BY ID ASC
Try this:
select ID,DATE,count(*) as TOTAL_DISINCT_VALUE from MAIN_TAB group by ID,DATE order by ID asc;
and replace * with any other column name except ID and DATE.
This question is an extension to this earlier question (not asked by me).
I have a table consisting of these fields:
id | date_from | date_to | price | priceName
--------------------------------------------------------
CK1 22-12-2012 29-12-2012 800 low
CK1 22-12-2012 29-12-2012 1200 medium
CK2 22-12-2012 29-12-2012 1400 high
CK2 22-12-2012 29-12-2012 1800 very high
CK2 22-12-2012 29-12-2012 2200 extortionate
How do I create a SQL select that groups the results by ID, DATE_FROM, DATE_TO and picks the lowest value from price and the priceName that is associated with the lowest price?
So the result would be
CK1 22-12-2012 29-12-2012 800 low
CK2 22-12-2012 29-12-2012 1400 high
From the previous question I can do:
select id, date_from, date_to, min(price)
from table
group by id, date_from, date_to
How do I extend this to only select the priceName that matches the min(price)? Grouping by priceName won't work because it does not constrain to the min(price).
You can use subquery.
Select Table.id,Date_from,date_to,MinPrice,PriceName
from
(select id, min(price) as MinPrice
from table
group by id) t1
INNER JOIN table ON t1.id=table.id and t1.MinPrice=table.Price
Group by id,Date_from,date_to,MinPrice,PriceName
Try this:
Select t.*,t1.priceName from (
select id, date_from, date_to, min(price)
from table
group by id, date_from, date_to) t
join table t1 on t.id =t1.id
and t.date_from =t1.date_from
and t.date_to =t1.date_to
and t.price =t1.price
One way would be to use the rank window function:
SELECT id, date_from, date_to, price, priceName
FROM (SELECT id, date_from, date_to, price, priceName,
RANK() OVER (PARTITION BY id, date_from, date_to
ORDER BY price ASC) AS rk
FROM mytable) t
WHERE rk = 1
Using CTE:
WITH cte (id, date_from, date_to, minprice) AS (
SELECT id, date_from, date_to, [minprice] = min(price)
FROM table_name
GROUP BY id, date_from, date_to)
SELECT
c.id, c.date_from, c.date_to, c.[minprice], t.priceName
FROM table_name t
JOIN cte c
ON t.id = c.id
AND t.date_from = c.date_to
AND t.data_to = c.date_to
I have 3 tables and my query is :
SELECT BRAND, AMOUNT FROM
(
SELECT BRAND, AMOUNT FROM SALES1
UNION
SELECT BRAND, AMOUNT FROM SALES2
UNION
SELECT BRAND, AMOUNT FROM SALES3
)
SALES 1 TABLE HAS BRAND: A AND AMOUNT: 50
SALES 3 TABLE HAS BRAND: A AND AMOUNT: 100
I want to get the amount 50 and disregard 100. I want to ask if is there any priority when using union?
If you want the first occurrence, you should use union all rather than union. This is important for performance reasons, because union does unnecessary duplicate elimination.
Then use the not exists clauses for each subquery:
(SELECT BRAND, AMOUNT
FROM SALES1
) UNION ALL
(SELECT BRAND, AMOUNT
FROM SALES2 s2
WHERE NOT EXISTS (SELECT 1 FROM SALES1 s1 WHERE s1.BRAND = s2.BRAND)
) UNION ALL
(SELECT BRAND, AMOUNT
FROM SALES3 s3
WHERE NOT EXISTS (SELECT 1 FROM SALES1 s1 WHERE s1.BRAND = s3.BRAND) AND
NOT EXISTS (SELECT 1 FROM SALES2 s2 WHERE s2.BRAND = s3.BRAND)
)
SELECT BRAND, AMOUNT
FROM
( SELECT BRAND, AMOUNT, 1 AS priority
FROM SALES1
UNION ALL
SELECT BRAND, AMOUNT, 2 AS priority
FROM SALES2
UNION ALL
SELECT BRAND, AMOUNT, 3 AS priority
FROM SALES3
)
ORDER BY priority
LIMIT 1;
you need a "where not exists"(or "not in" or a "left join") within your unions:
(SELECT BRAND, AMOUNT FROM SALES1 )
UNION
(SELECT BRAND, AMOUNT FROM SALES2)
UNION
(SELECT BRAND, AMOUNT FROM SALES3 where brand not in (SELECT BRAND FROM SALES1))
I have a table Like below.
I want the product_id of Minimum, Maximum and Average cost products in a single query.
CREATE TABLE productlist(product_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
cost INT);
INSERT INTO productlist(cost)
VALUES('2450'),
('2200'),
('2580'),
('2405'),
('3500'),
('1500'),
('1800'),
('1520'),
('1740'),
('1940'),
('2940'),
('1250'),
('1290'),
('1390'),
('2900');
Output:
Min 12
Max 5
Avg 2093
I tried like one below but its not working.
SELECT product_id, MIN(cost) as mincost
FROM productlist
GROUP BY product_id
ORDER BY mincost ASC
LIMIT 0,1
UNION
SELECT product_id, max(cost) as maxcost
FROM productlist
GROUP BY product_id
ORDER BY maxcost DESC
LIMIT 0,1
How should I do this
select 'Min', product_id
from productlist
where cost = (select min(cost) from productlist)
UNION
select 'Max', product_id
from productlist
where cost = (select MAX(cost) from productlist)
UNION
select 'Avg', round(AVG(cost),0) as Avg
from productlist
select product_id, cost
from productlist where cost = (SELECT max(cost)from productlist)
union
select product_id, cost
from productlist where cost = (SELECT min(cost)from productlist)
union
select product_id, cost
from productlist where cost = (SELECT x.cost from productlist x, productlist y
GROUP BY x.cost
HAVING SUM(SIGN(1-SIGN(y.cost-x.cost))) = (COUNT(*)+1)/2)
This uses median, returns product id in every case
the output you want is not coming by the query you wrote
you need to try out this one for getting the required output
select 'Min', product_id
from productlist
where cost = (select min(cost) from productlist)
UNION
select 'Max', product_id
from productlist
where cost = (select MAX(cost) from productlist)
UNION
select 'Avg', floor(AVG(cost)) as Avg
from productlist
This answers your question exactly but it should be noted that it costs 3 table scans to find this data. Also, the example in the question suggests that the average value is truncated down to 2093 from 2093.67. It is perhaps better to replace this with round.
SQL Fiddle
SELECT concat('Min ', product_id)
FROM productlist
WHERE cost = (SELECT min(cost) from productlist)
UNION ALL
SELECT concat('Max ', product_id)
FROM productlist
WHERE cost = (SELECT max(cost) from productlist)
UNION ALL
SELECT concat('Avg ', truncate(avg(cost), 0))
FROM productlist
This is to select all the products
SELECT
max(cost), MIN(cost), AVG(cost)
FROM
productlist
GROUP BY
product_id
GROUP BY is not exactly required here. But seems like you are beginner, googling to will help you.
For your question try this.
SELECT
(select CONCAT(product_id, '-', cost) from productlist group by product_id order by cost DESC limit 1) as MAX,
(select CONCAT(product_id, '-', cost) from productlist group by product_id order by cost ASC limit 1) as MIN,
(select avg(cost) from productlist) as AVG
FROM
productlist limit 1