I need an output like this:
Month -->| Pre_Mnth2 | Pre_Mnth1 | current_Month_ Name
Product A | 3387 | 87985 | 2338
Product B | 6386 | 67983 | 6374
Product C | 3880 | 76988 | 9378
...
All the required data is in single table only.
Column looks like this:
id | companycode | merchantcode| pdtname | qty | value | invdate |
Can anybody help me with php mysql query code?
My current code returns the data for current month only
SELECT `pdtname`, `qty`, `value`, sum(`qty`), sum(`value`)
FROM `ist`
WHERE merchantcode = $q AND companycode = $companycode
AND MONTH(invdate) = $currentdate[mon]
GROUP BY `pdtname`
Here variable $q and $companycode is taken from user session.
Are you looking for something like this?
SELECT pdtname,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 3 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 2 MONTH THEN qty END) qty_2m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 3 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 2 MONTH THEN value END) value_2m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 2 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 1 MONTH THEN qty END) qty_1m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 2 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 1 MONTH THEN value END) value_1m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 1 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) THEN qty END) qty_m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 1 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) THEN value END) value_m
FROM ist
WHERE invdate BETWEEN LAST_DAY(CURDATE()) - INTERVAL 3 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE())
AND companycode = 1
AND merchantcode = 1
GROUP BY pdtname
Note: Make sure that you have an index on invdate.
Output:
| PDTNAME | QTY_2M | VALUE_2M | QTY_1M | VALUE_1M | QTY_M | VALUE_M |
|----------|--------|----------|--------|----------|-------|---------|
| ProductA | 30 | 3000 | 30 | 3000 | 30 | 3000 |
| ProductB | 100 | 1000 | 100 | 1000 | 100 | 1000 |
| ProductC | 320 | 3200 | 320 | 3200 | 320 | 3200 |
Here is SQLFiddle demo
Related
Good day. I am Selecting Dates to MYSql and I select it one by one. I want to select all one week. And I used to select it individually.
Here is my code:
<?php
$chart6 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 6 DAY");
$chart6->execute(array(":today"=>$today));
$chartData6 = $chart6->fetchColumn();
$chart5 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 5 DAY");
$chart5->execute(array(":today"=>$today));
$chartData5 = $chart5->fetchColumn();
$chart4 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 4 DAY");
$chart4->execute(array(":today"=>$today));
$chartData4 = $chart4->fetchColumn();
$chart3 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 3 DAY");
$chart3->execute(array(":today"=>$today));
$chartData3 = $chart3->fetchColumn();
$chart2 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 2 DAY");
$chart2->execute(array(":today"=>$today));
$chartData2 = $chart2->fetchColumn();
$chart1 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 1 DAY");
$chart1->execute(array(":today"=>$today));
$chartData1 = $chart1->fetchColumn();
$chart0 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today");
$chart0->execute(array(":today"=>$today));
$chartData0 = $chart0->fetchColumn();
?>
Is there query that can select the sum of the whole day of the week? or I should keep this code?
try it sql use group
SELECT Day(pay_date) pay_day,SUM(amount) amount
FROM tbl_paid WHERE pay_date >= NOW() - INTERVAL 6 DAY
GROUP BY Day(pay_date)
result:
| pay_day | amount |
|---------|--------|
| 1 | 246 |
| 2 | 200 |
| 28 | 1702 |
| 29 | 1462 |
| 30 | 864 |
| 31 | 1092 |
Test Data :
| amount | pay_date |
|--------|----------------------|
| 100 | 2018-06-02T00:00:00Z |
| 123 | 2018-06-01T00:00:00Z |
| 546 | 2018-05-31T00:00:00Z |
| 432 | 2018-05-30T00:00:00Z |
| 731 | 2018-05-29T00:00:00Z |
| 851 | 2018-05-28T00:00:00Z |
| 100 | 2018-06-02T00:00:00Z |
| 123 | 2018-06-01T00:00:00Z |
| 546 | 2018-05-31T00:00:00Z |
| 432 | 2018-05-30T00:00:00Z |
| 731 | 2018-05-29T00:00:00Z |
| 851 | 2018-05-28T00:00:00Z |
| 436 | 2018-05-27T00:00:00Z |
SQL Fiddle Demo Link
if you want more information then
you can use weekday() and DAYNAME()
SELECT WEEKDAY(pay_date) WEEKDAY,DAYNAME(pay_date) DAYNAME,SUM(amount) amount
FROM tbl_paid WHERE pay_date >= NOW() - INTERVAL 7 DAY
GROUP BY WEEKDAY(pay_date),DAYNAME(pay_date)
result:
| WEEKDAY | DAYNAME | amount |
|---------|-----------|--------|
| 0 | Monday | 1702 |
| 1 | Tuesday | 1462 |
| 2 | Wednesday | 864 |
| 3 | Thursday | 1092 |
| 4 | Friday | 246 |
| 5 | Saturday | 200 |
| 6 | Sunday | 1287 |
SQL Fiddle Demo Link
If you must pass the date then do:
SELECT DATE(pay_date) `DATE`, SUM(amount) daily_amount
FROM tbl_paid
WHERE DATE(pay_date) BETWEEN DATE(:today) - INTERVAL 1 WEEK AND DATE(:today)
GROUP BY DATE(pay_date);
Else use the current date of the database.
SELECT DATE(pay_date) `DATE`, SUM(amount) daily_amount
FROM tbl_paid
WHERE DATE(pay_date)>=CURRENT_DATE - INTERVAL 1 WEEK
GROUP BY DATE(pay_date);
You don't need to run the query seven times. Remember; don't repeat yourself (DRY).
I've got a mysql-database like this:
ID | date | value
1 | 2017-02-20 | 200
2 | 2017-02-17 | 400
3 | 2017-02-15 | 630
4 | 2017-01-28 | 110
5 | 2017-01-29 | 220
I know how to get the sum of january, february, ....
SELECT SUM(value) AS wert
FROM table
WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH)
But now I need a code to get the average of january and february.
I tried
SELECT SUM(value) AS wert, AVG(wert) as average
FROM table
WHERE YEAR (date) = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH)
But this didn't work.
Thank you for your help!
WHERE date >= '2017-1-1' AND date < '2017-3-1'
Here is how my table looks like
| total_hit |success_hit | date_time |
| 12 | 12 | 01-11-2009 07:32:44 |
| 12 | 11 | 01-11-2009 08:33:49 |
| 12 | 10 | 01-11-2009 09:08:24 |
| 12 | 11 | 01-12-2009 10:33:57 |
| 12 | 12 | 01-12-2009 11:37:34 |
| 12 | 11 | 01-12-2009 12:23:49 |
I am fetching the data from the table for the past 30 days using the following query:
SELECT *
FROM my_table
WHERE DATE(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
what I want to do is group the returned data in terms of date and calculate the percentage success (success_hit/total_hit)*100
Something like
Date: 01-11-2009 Percentage: x
Date: 01-12-2009 Percentage: y
any ideas?
select (success/total)*100 as perc_success, Date(date_time) from
(select sum(success_hit) as success, sum(total_hit) as total,
date_time from my_table
where DATE(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
group by Date(date_time) )
tbl
This should work!!!
Try this
SELECT (sum(success_hit)/sum(total_hit)*100) AS `Percentage`,
DATE(date_time) AS `Date`
FROM my_table
WHERE DATE(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
GROUP BY DATE(date_time);
I want to select record that must have two or more entries but should have today and tomorrow date in table. I am saving date in table in date format.
SELECT `availibility`.*
FROM (`availibility`)
WHERE `property_id`= 8818
AND (availibility.unavailibility_date between CURDATE()
AND DATE_ADD(CURDATE(),INTERVAL 1 DAY))
I am using above query but this will true even one date (today or tomorrow) exists. I want to get such record that should have both dates
for example
+---------+----------------+------------+
| ID | property_id | Date |
+---------+----------------+------------+
| 369516 | 8818 | 2013-01-19 |
| 369517 | 8818 | 2013-01-18 |
| 369518 | 8818 | 2013-01-17 |
| 418021 | 8818 | 2013-08-27 |
| 418022 | 8818 | 2013-08-28 |
| 418022 | 8818 | 2013-08-29 |
| 418022 | 2001 | 2013-07-29 |
| 418022 | 2001 |2013-07-30 |
+---------+----------------+------------+
8818 property should come in record set because both date exists here
SELECT property_id
FROM yourtable
WHERE date IN (CURDATE(), CURDATE() + INTERVAL 1 DAY)
GROUP BY property_id
HAVING COUNT(DISTINCT date)=2
Please see fiddle here.
You can use:
NOW() + INTERVAL 1 DAY
If you are only interested in the date, not the date and time then you can use CURDATE instead of NOW:
CURDATE() + INTERVAL 1 DAY
your query should be
SELECT `availibility`.*
FROM (`availibility`)
WHERE `property_id`= 8818
AND (availibility.unavailibility_date between CURDATE()
AND CURDATE() + INTERVAL 1 DAY // change here
Use a JOIN to make sure the second record exists:
SELECT `availibility`.*
FROM `availibility`
JOIN `availibility` AS availibility_tomorrow ON availibility_tomorrow.property_id = availibility.property_id AND availibility_tomorrow.unavailibility_date = DATE_ADD(CURDATE (),INTERVAL 1 DAY))
WHERE `property_id`= 8818 AND availibility.unavailibility_date = CURDATE()
If i have understood this good...
SELECT `property_id`, count(*) as no
FROM (`availibility`)
WHERE `property_id`= 8818
AND (availibility.unavailibility_date=CURDATE()
OR availibility.unavailibility_date=DATE_ADD(CURDATE(),INTERVAL 1 DAY))
having no =2
I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.