MySQL query date ranges - mysql

I have googled but did not find anything related. I have a MySQL table like this:
+++++++++++++++++++++++++++++++
| roomID | date | price |
+++++++++++++++++++++++++++++++
| 1 | 2012-10-10 | 10 |
| 1 | 2012-10-11 | 10 |
| 1 | 2012-10-12 | 10 |
| 1 | 2012-10-13 | 12 |
| 2 | 2012-10-10 | 15 |
| 2 | 2012-10-11 | 15 |
| 2 | 2012-10-12 | 15 |
| 2 | 2012-10-13 | 16 |
| 2 | 2012-10-14 | 16 |
| 2 | 2012-10-15 | 16 |
+++++++++++++++++++++++++++++++
I need to get periods based on price and roomID:
++++++++++++++++++++++++++++++++++++++++++++
| roomID | from | till | price |
++++++++++++++++++++++++++++++++++++++++++++
| 1 | 2012-10-10 | 2012-10-12 | 10 |
| 1 | 2012-10-13 | 2012-10-13 | 12 |
| 2 | 2012-10-10 | 2012-10-12 | 15 |
| 2 | 2012-10-13 | 2012-10-15 | 16 |
++++++++++++++++++++++++++++++++++++++++++++
Thank you!

select roomid,
min(date) as from,
max(date) as till,
price
from periods
group by price
order by price

You can try using the following query:
SELECT roomid, MIN(date) AS `from`, MAX(date) AS `till`, price
FROM tableName
GROUP BY price
ORDER BY price

Related

sql joins with multiple conditions

i have two tables, (say bill and soldproduct)
select * from bill;
+------+------------+------------+
| id | solddate | customerId |
+------+------------+------------+
| 11 | 2018-07-23 | 1 |
| 12 | 2018-07-21 | 1 |
| 13 | 2018-08-02 | 2 |
| 14 | 2018-08-08 | 2 |
| 15 | 2018-08-08 | 1 |
| 16 | 2018-08-08 | 1 |
+------+------------+------------+
select * from soldproduct;
+--------+-------------+----------+-------+------------+
| billid | productname | quantity | price | totalprice |
+--------+-------------+----------+-------+------------+
| 11 | book | 2 | 100 | 200 |
| 11 | pen | 10 | 10 | 100 |
| 11 | pencil | 5 | 2 | 10 |
| 12 | pencil | 5 | 2 | 10 |
| 13 | pen | 10 | 10 | 100 |
| 13 | book | 2 | 100 | 200 |
| 14 | pen | 1 | 10 | 10 |
| 14 | bottle | 1 | 75 | 75 |
| 15 | phone | 1 | 5000 | 5000 |
| 16 | lock | 15 | 50 | 750 |
+--------+-------------+----------+-------+------------+
I need to find the highest bill id using totalprice.
I tried using
select billid,sum(totalprice)
from soldproduct
where billid in (select id from bill where solddate >= date_sub(curdate(),interval 1 month))
group by billid
order by totalprice desc;
and my output is
+--------+-----------------+
| billid | sum(totalprice) |
+--------+-----------------+
| 15 | 5000 |
| 16 | 750 |
| 11 | 310 |
| 13 | 300 |
| 12 | 10 |
| 14 | 85 |
+--------+-----------------+
How do i get the same output with a single query using joins (without using subquery)?
try the following join
select billid,sum(totalprice)
from soldproduct
join bill on soldproduct.billid = bill.id and solddate >= date_sub(curdate(),interval 1
month)
group by billid
order by totalprice desc;
Can you try the below query:(I do not tested it out)
SELECT billid, SUM(totalprice)
FROM soldproduct SP
JOIN bill B ON (B.id = SP.billid)
WHERE B.solddate BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE()
GROUP BY SP.billid
ORDER BY SP.totalprice DESC;

Sum values based on ctiteria in MySQL

I have the following data table from which I would like to sum the values of the field 'pts' for each 'pid' as follows:
The sum of the top 3 values per 'cont' plus the values of any other 'cont' per 'pid'. The results should be presented in DESC order by 'total'
+--------+-----+------+
| pid | pts | cont |
+--------+-----+------+
| 121693 | 40 | 1 |
| 121693 | 80 | 2 |
| 121693 | 120 | 1 |
| 121693 | 100 | 1 |
| 121693 | 500 | 1 |
| 121694 | 20 | 1 |
| 121694 | 0 | 2 |
| 121694 | 30 | 3 |
| 121695 | 0 | 1 |
| 121695 | 30 | 2 |
| 121695 | 0 | 1 |
+--------+-----+------+
In this example the query should return something like this
+--------+-------+
| pid | total |
+--------+-------+
| 121693 | 800 |
| 121694 | 50 |
| 121695 | 30 |
+--------+-------+
Is this possible?
Thanks in advance.
SELECT DISTINCT pid, SUM(Pts) AS Total
FROM your tablename
GROUP BY Pid
ORDER BY TOTAL
(Requires testing and minor fixes on small syntax)

SELECT the customer who has ordered the greatest quantity of Products?

I'm trying to make query to find the Customer who has ordered the greatest quantity of Products from the following table!
mysql> select * from ORDERS;
+---------+---------+------------+-----+
| CUSTNUM | PRODNUM | DATE | QTY |
+---------+---------+------------+-----+
| 125216 | 2323 | 2016-03-21 | 2 |
| 136101 | 2357 | 2016-03-21 | 5 |
| 136101 | 2357 | 2016-10-12 | 1 |
| 136101 | 2357 | 2016-11-25 | 5 |
| 136101 | 3737 | 2016-10-12 | 10 |
| 136101 | 9193 | 2016-11-25 | 5 |
| 182764 | 2357 | 2015-03-21 | 12 |
| 182764 | 2357 | 2016-05-12 | 10 |
| 212836 | 3737 | 2015-09-16 | 6 |
| 455566 | 4143 | 2016-02-09 | 10 |
| 455566 | 4143 | 2016-05-12 | 10 |
+---------+---------+------------+-----+
expected result
+-------------+------------------+
| CUSTNUM | quantity_ordered |
+-------------+------------------+
| 136101 | 26 |
+-------------+------------------+
Thanks in advance for help.
Use group by clause.
For more info,
Please refer some tutorials
Or read the official docs
SELECT CUSTNUM, SUM(QTY) s FROM ORDERS GROUP BY CUSTNUM
ORDER BY s DESC LIMIT 1
SQLfiddle

MySQL: Get everyday incremental data

I want to fetch the data from Table based on date but in an incremental way.
Suppose I have data like this which is grouped by date
| DATE | Count |
| 2015-06-23 | 10 |
| 2015-06-24 | 8 |
| 2015-06-25 | 6 |
| 2015-06-26 | 3 |
| 2015-06-27 | 2 |
| 2015-06-29 | 2 |
| 2015-06-30 | 3 |
| 2015-07-01 | 1 |
| 2015-07-02 | 3 |
| 2015-07-03 | 4 |
So the result should come like this
| DATE | Count| Sum|
| 2015-06-23 | 10 | 10 |
| 2015-06-24 | 8 | 18 |
| 2015-06-25 | 6 | 24 |
| 2015-06-26 | 3 | 27 |
| 2015-06-27 | 2 | 29 |
| 2015-06-29 | 2 | 31 |
| 2015-06-30 | 3 | 34 |
| 2015-07-01 | 1 | 35 |
| 2015-07-02 | 3 | 38 |
| 2015-07-03 | 4 | 42 |
You would join every other previous date on that date, and then sum the count on that
If you give me your table structure, I can make it run.
id, name, date_joined
SELECT counts.theCount, sum(counts.theCount), table.date_joined
FROM yourTable
LEFT JOIN
(SELECT count(*) as theCount, table.date_joined
FROM yourTable
GROUP BY table.date_joined
) as counts
ON
yourTable.date_joined> counts.date_joined
GROUP BY yourTable.date_joined

MySQL select rows by max(column) by another column in SQL while using GROUP BY WEEK?

For each week I want to select the rows which have the highest weight for each distinct value of reps
-------------------------------------
| id | reps | weight | date |
| 1 | 1 | 15 | 2015-06-10 |
| 2 | 2 | 29 | 2015-06-12 |
| 3 | 1 | 30 | 2015-06-13 |
| 4 | 4 | 11 | 2015-06-14 |
| 5 | 1 | 15 | 2015-06-29 |
| 6 | 1 | 9 | 2015-06-30 |
and I would like it to return
-------------------------------------
| id | reps | weight | date |
| 2 | 2 | 29 | 2015-06-12 |
| 3 | 1 | 30 | 2015-06-13 |
| 4 | 4 | 11 | 2015-06-14 |
| 5 | 1 | 15 | 2015-06-29 |
I've tried
SELECT MAX(weight) as weight, reps, date, id FROM log_items
GROUP BY reps, WEEK(date)
But it seems to return completely random results, I have also tried using sub queries but they didn't work either (I'm guessing I was doing it wrong)
Try:
SELECT *
FROM log_items
WHERE (reps, WEEK(date), weight ) in (
SELECT reps, WEEK(date) , MAX(weight)
FROM log_items
GROUP BY reps, WEEK(date)
)
demo: http://sqlfiddle.com/#!9/4bf84/9