SQL number of grouped rows with conditional - php

I am using the statement below to group results by month to give a total price however I also need to know how many rows have been grouped, but only if the price is above a certain amount.
"SELECT SUM(price) FROM table GROUP BY month";

use SUM with CASE
SELECT SUM(price) totalPrice,
SUM(CASE WHEN price > x THEN 1 ELSE 0 END) totalRows
FROM table
GROUP BY month
where X is the value of price

One option is to use COUNT and CASE:
SELECT SUM(price), COUNT(CASE WHEN Price > 100 THEN 1 END)
FROM table
GROUP BY month
SQL Fiddle Demo

Related

Get query result of every month between two dates

create table link : https://drive.google.com/file/d/1EEqpW2Y8UkplfQcp_fw0j2byXAxBQXOW/view?usp=sharing
I have a query
SELECT
DATE_FORMAT(seized_date,'%Y-%m') as 'Seized Date',
sum(case when seized_remarks = 'Temporary Seized' then 1 else 0 end) AS seized,
sum(case when (DATE_FORMAT(release_date, '%Y-%m') BETWEEN '2021-01' AND '2021-07') then 1 else 0 end) AS released,
sum(case when (DATE_FORMAT(stock_return_date, '%Y-%m') BETWEEN '2021-01' AND '2021-07') then 1 else 0 end) AS stock_return
FROM mahindra
where
(DATE_FORMAT(seized_date, '%Y-%m') BETWEEN '2021-01' AND '2021-07')
GROUP BY DATE_FORMAT(seized_date,'%Y-%m')
which gives result as
Date Seized Release Stock Return
2021-01 1 0 0
2021-03 1 0 0
2021-04 1 0 0
2021-05 5 1 0
2021-06 6 0 1
2021-07 2 0 0
here i didn't get the result of february 2021. I want to get the result of all months between this two dates even if the seized_date does not exist
Looks like you need in something like
SELECT
dates.`Seized Date`,
COALESCE(SUM(mahindra.seized_remarks = 'Temporary Seized'), 0) AS seized,
COALESCE(COUNT(mahindra.release_date), 0) AS released,
COALESCE(COUNT(mahindra.stock_return_date), 0) AS stock_return
FROM ( SELECT '2021-01' `Seized Date` UNION ALL
SELECT '2021-02' UNION ALL
SELECT '2021-03' UNION ALL
SELECT '2021-04' UNION ALL
SELECT '2021-05' UNION ALL
SELECT '2021-06' UNION ALL
SELECT '2021-07' ) dates
LEFT JOIN mahindra ON DATE_FORMAT(mahindra.seized_date, '%Y-%m') = dates.`Seized Date`
GROUP BY dates.`Seized Date`
As #Akina says, if the seized_date value does not exist anywhere in your table, you cannot expect it to be present in your results at all.
You need to create a column containing all required dates and then you can do something like perform a join onto that column.
Here's an example of how I might do it.
SELECT TO_CHAR(DATEADD('MONTH', -n, (CURRENT_DATE+1)),'YYYY-MM') AS seized_date
FROM (
SELECT ROW_NUMBER() OVER () AS n
FROM mahindra LIMIT 10
)
ORDER BY seized_date DESC
This is creates the following output.
Code explanation:
The inner query is based on a window function.
The window function itself is simple. We're basically telling the computer to assign a value of 10 to n. To a human, "10" is a numeric value assigned to a specific thing or count of things (eg the temperature is 10 degrees, or I have 10 apples), but to the processor/computer, it doesn't mean much on its own. At least not in our scenario. So we simply tell the processor to count some rows ROW_NUMBER and when it finds 10 rows, that's what 10 looks like. You can use LIMIT to make this greater or fewer than 10 months.
In the outer query we just take today's date CURRENT_DATE and subtract n months from it as in (DATEADD('MONTH', -n, (CURRENT_DATE+1).
In our case, n is 10 months.
Now you have a column of dates, formatted as you per your requirements YYYY-MM.
You can write the query such that you LEFT JOIN your precessed data set to these dates on their corresponding values.
The reason why this is a good way of doing things is that you don't have to manually enter any dates or use a UNION join. You let the window function do the work for you, meaning you can go back in time as far as you need/or want very easily by changing the LIMIT value. This allows for greater efficiency in the event where you need to go back over multiple years, for example.

Min value from Database in MySQL

Am trying to find the min value from past 30 days, in my table there is one entry for every day, am using this query
SELECT MIN(low), date, low
FROM historical_data
WHERE name = 'bitcoin'
ORDER BY STR_TO_DATE(date,'%d-%m-%Y') DESC
LIMIT 7
But this value not returing the correct value. The structure of my table is
Table structure
And table data which is store is like this
Table data style
Now what i need is to get the minimum low value. But my query not working it give me wrong value which even did not exist in table as well.
Updates:
Here is my updated Table Structure.
enter image description here
And here is my data in this table which look like this
enter image description here
Now if you look at the data, i want to check the name of token omisego and fatch the low value from past 7 days which will be from 2017-12-25 to 2017-12-19
and in this cast the low value is 9.67, but my current query and the query suggested by my some member did not brings the right answer.
Update 2:
http://rextester.com/TDBSV28042
Here it is, basically i have more then 1400 coins and token historical data, which means that there will me more then 1400 entries for same date like 2017-12-25 but having different name, total i have more then 650000 records. so every date have many entries with different names.
To get the lowest row per group you could use following
SELECT a.*
FROM historical_data a
LEFT JOIN historical_data b ON a.name = b.name
AND a.low > b.low
WHERE b.name IS NULL
AND DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25'
AND a.name = 'omisego'
or
SELECT a.*
FROM historical_data a
JOIN (
SELECT name,MIN(low) low
FROM historical_data
GROUP BY name
) b USING(name,low)
WHERE DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25'
AND a.name = 'omisego'
DEMO
For last 30 day of 7 days or n days you could write above query as
SELECT a.*, DATE(a.`date`)
FROM historical_data2 a
LEFT JOIN historical_data2 b ON a.name = b.name
AND DATE(b.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY
AND DATE(b.`date`) <= CURRENT_DATE()
AND a.low > b.low
WHERE b.name IS NULL
AND a.name = 'omisego'
AND DATE(a.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY
AND DATE(a.`date`) <= CURRENT_DATE()
;
DEMO
But note it may return more than one records where low value is same, to choose 1 row among these you have specify another criteria to on different attribute
Consider grouping the same and running the clauses
SELECT name, date, MIN(low)
FROM historical_data
GROUP BY name
HAVING name = 'bitcoin'
AND STR_TO_DATE(date, '%M %d,%Y') > DATE_SUB(NOW(), INTERVAL 30 DAY);
Given the structure, the above query should get you your results.
// Try this code ..
SELECT MIN(`date`) AS date1,low
FROM historical_data
WHERE `date` BETWEEN now() - interval 1 month
AND now() ORDER by low ASC;

Laravel query with case and different table subtraction

Check my laravel query it's not working... but mysql query works fine.
$data1 = DB::table(DB::raw('(select sum(case when type="debit" then amount else -amount end) from report) - (select sum(amount) from total) as balance' ))
For mysql query please refer sqlfiddle : http://sqlfiddle.com/#!9/2d0343/9
Thanks a lot...
Assuming the type can't be null and has either credit/debit value, you can use aggregation with CASE:
select
sum(case when type='credit' then amount else -amount end)
from report;
Demo
In case the column is nullable or other values are allowed in the column type, you can modify the above like:
select
sum(case when type='credit' then amount
else -amount end)
from report
where type in ('credit', 'debit');
EDIT:
For the latest edit, calculate the difference from one table and subtract the aggregate from other:
select (
select sum(case when type='credit' then amount else -amount end)
from report
) - (select sum(amount) from total);
select sum(case type when 'credit' then amount when 'debit' then -amount else 0 end)
from report
select (sum(case when type='credit' then amount else -amount end)) - (select sum(amount) from total)
from report

PHP and SQL - How to select highest sum of all months in a current year?

So I have table 'item' in database with attributes: id, dateItem, price.
I want to find MAX(SUM(price)) for some month in a current year.
id dateItem (Format: yyyy-mm-dd) price
1 25.06.2015. 986,69
2 21.06.2015. 1564
3 22.03.2015. 23,56
4 21.03.2015. 187,23
5 01.03.2015. 489,33
6 06.10.2015. 975,26
I came up with something like this, but I know it's not ok. Please, help :s
$sql = "SELECT MAX(SUM(price)) FROM item WHERE DATE('Y') = 2015 AND
dateItem between DATE('Y/m/1') and DATE('Y/m/31')";
You can't nest aggregation functions. You need to use a subquery.
SELECT MAX(pricesum)
FROM (SELECT SUM(price) AS pricesum
FROM item
WHERE YEAR(dateItem) = YEAR(NOW())
GROUP BY MONTH(dateItem)) AS subquery
You can do this with ORDER BY and LIMIT:
SELECT SUM(price) AS pricesum
FROM item
WHERE YEAR(dateItem) = YEAR(NOW())
GROUP BY MONTH(dateItem)
ORDER BY pricesum DESC
LIMIT 1;
If you want to know the month as well, you can include that in the SELECT clause.

Date as column (Create report per day)

I have this query which generates the result that i have wanted.
I just need to make date into a column
SELECT item, date, SUM(quantity)
FROM t
WHERE date between '2015-08-18' and '2015-08-20'
GROUP BY item, date
Here is my SQL FIDDLE
which generates
Result I've wanted
Please can anyone give me at least idea on how to achieve the result I've wanted?
Something like this using conditional Aggregate
To extract the day from date use DAY() function
SELECT item,
SUM(case when day(date) = 18 then quantity else 0 end) as `18`,
SUM(case when day(date) = 19 then quantity else 0 end) as `19`,
SUM(case when day(date) = 20 then quantity else 0 end) as `20`
FROM t
WHERE date between '2015-08-18' and '2015-08-20'
GROUP BY item
SQL FIDDLE DEMO

Categories