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
Related
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.
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
I have been trying to get my head around this for a time now and can't find a solution: I am querying for time-entries with a result like this:
2015-02-10: 13
2015-02-11: 16
2015-02-13: 11
As you can see I am missing two days in the array because there are no entries for these days. My google-fu brought me some solutions for this problem but none seem to work for my specific code:
SELECT
DATE(time_entries.start) AS date,
COUNT(time_entries.id) AS entries,
SUM(CASE WHEN user_id = 4 THEN TIMESTAMPDIFF(MINUTE, start, end) ELSE 0 END) AS me,
SUM(CASE WHEN user_id = 3 THEN TIMESTAMPDIFF(MINUTE, start, end) ELSE 0 END) AS ph
FROM time_entries
LEFT JOIN calendar_table
ON time_entries.start=calendar_table.dt
WHERE start BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURDATE()
GROUP BY date
ORDER BY date
I created the calendar_table with this help: https://www.brianshowalter.com/calendar_tables
Please help!
Best,
Chris
Try with right join. Your query is using your time_entries records to match the calendar table, and finds nothing because they're not there.
By using right join, you'll use calendar_table records first.
SELECT
DATE(time_entries.start) AS date,
COUNT(time_entries.id) AS entries,
SUM(CASE WHEN user_id = 4 THEN TIMESTAMPDIFF(MINUTE, start, end) ELSE 0 END) AS me,
SUM(CASE WHEN user_id = 3 THEN TIMESTAMPDIFF(MINUTE, start, end) ELSE 0 END) AS ph
FROM time_entries
RIGHT JOIN calendar_table
ON time_entries.start=calendar_table.dt
WHERE start BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURDATE()
GROUP BY date
ORDER BY date
i have 1 table with 3 column: id, amount, date
i want to calculate the amount only if the date is today in PHP with MySQL, i tried with various thing but none of them worked, for calculate the today date i use
$date = date('m/d/y');
SELECT SUM (CASE WHEN DATE(`date`) = DATE(NOW()) THEN amount ELSE 0 END)
FROM my_table
EDIT:
As suggested in the comments by #user3760540, using CURDATE() could be more elegant:
SELECT SUM (CASE WHEN DATE(`date`) = CURDATE()) THEN amount ELSE 0 END)
FROM my_table
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