How to group datas by week in sql? - php

i'm getting table values like below table
Name City Date
a 1 Mar 22 2013
b 2 Apr 19 2013
c 3 Apr 20 2013
d 4 Apr 22 2013
e 5 Apr 27 2013
f 6 Apr 30 2013
g 7 Jun 5 2013
i got values like above table. Todays date is Apr 25 2013. i need to group by those values from +7 and -7 like Apr 18 2013 to Jun 2 2013. Please help me to do that.

select * from yourTable
where DATEDIFF(day,'2013-04-25',[Date]) between -7 and 7
If you need the current date when query is running then use GETDATE() function instead of '2013-04-25'

Depending on the current date is created dynamically condition
SELECT *
FROM dbo.yourTable
WHERE Date >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())-7, 0)
AND Date < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())+8, 0)

Related

How can we take price (sum) of repeated day but id also needed in mysql?

I need to plot graph using chart.js. In x-axis I need to show day. In left y-axis I need to show price, In right y-axis I need to show id. At-present my problem is repeated day in x-axis. Please check below mentioned query and result.
Query
SELECT od.id, sum(op.price), od.status, DATE_FORMAT(od.created_at, '%d %m') AS day
FROM order_positions op
INNER JOIN orders od ON od.id = op.order_id
WHERE od.status = 'finished'
GROUP BY op.order_id
ORDER BY od.created_at LIMIT 20
Data This is the result I am getting
id sum(op.price) status day
7 180.00 finished 09 06
17 70.00 finished 09 06
19 48.00 finished 09 06
20 20.00 finished 13 06
21 31.00 finished 13 06
22 20.00 finished 15 06
23 20.00 finished 15 06
Tables are
order_positions
id order_id price
1 7 100
2 7 80
3 17 35
4 17 35
orders
id status date
7 finished 09 06
17 finished 09 06
19 finished 09 06
20 finished 13 06
You're going to want to group it by the od.created_at instead of op.order_ID
SELECT od.id,
Sum(op.price),
od.status,
Date_format(od.created_at, '%d %m') AS day
FROM order_positions op
INNER JOIN orders od
ON od.id = op.order_id
WHERE od.status = 'finished'
GROUP BY od.created_at
ORDER BY od.created_at
LIMIT 20

How can i remove other lines from a csv formatted string using PHP?

Hi I have a string which is in a csv format,
Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016
How can I remove those lines starting from ROUTING TABLE upto the last line and get this output?
Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
Thanks in advance :)
If all you need is to take a string and remove all content after "ROUTING TABLE" then this would work. However it looks like you may want to use it as a CSV after? How are you converting it to a string? Depending on how you are using it you may want to look into converting it into an array from the CSV file and then it would be done slightly different.
<?php
$string = "Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016";
$string = substr($string, 0, strpos($string, "ROUTING TABLE"));
echo "-------<br>";
echo $string;
echo "<br>-------";
// OUTPUT:
// -------
// Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016 jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016 walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
// -------
?>

Mysql query get result by month and year

Table inventory_month has three fields id , month_month and month_year
id month_month month_year
1 09 2012
2 10 2012
3 11 2012
4 01 2013
5 02 2013
6 03 2013
7 09 2013
8 10 2013
I need result FROM October 2012 TO October 2013 as below
Expected result
id month_month month_year
2 10 2012
3 11 2012
4 01 2013
5 02 2013
6 03 2013
7 09 2013
8 10 2013
But getting result below
Getting result
id month_month month_year
2 10 2012
8 10 2013
I have tried query below
select id,month_month,month_year where month_month>=10 AND month_year>=2012 AND month_month<=10 AND month_year<=2013
Need helpful suggestion, Thanks!
Use,
SELECT id,month_month,month_year WHERE
(month_month >= 10 AND month_year = 2012)
OR
(month_month <= 10 AND month_year = 2013)
Your condition logic is incorrect.
You need to replace your 2nd AND statement to an OR statement and add brackets.
(month_month>=10 AND month_year>=2012) OR (month_month<=10 AND month_year<=2013).
Hope this helps!
Try this
select id,month_month,month_year where (month_month>=10 AND month_year>=2012)
Or (month_month<=10 AND month_year<=2013)

Issue In fetching for upcoming events

SO I have write the code for getting the list of upcoming events in next 7 days and it is working fine and giving me the list of upcoming events.but the probmlem is that it is also giving the event record of past days also. Here is my code :
$today = strtotime("Now");
echo date('Y-m-d :H:i:s',$today)."<br>";
$leadDate = strtotime("+7 day", $today);
echo date('Y-m-d :H:i:s',$leadDate)."<br>";
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN CURDATE() AND '$leadDate'") or die(mysql_error());
echo "<b>Dues for upcoming 7 days</b><br>";
while($row=mysql_fetch_assoc($rs))
{
echo $row['name']." => ".$row['normal_date']."<br>";
}
I have the table :
id| name | due_date | normal_date
|1|name1|1390245240|Mon, 20 Jan 2014 19:14:00 GMT
|2|name2|1390331640|Tue, 21 Jan 2014 19:14:00 GMT
|4|name3|1390418040|Wed, 22 Jan 2014 19:14:00 GMT
|5|name4|1390590840|Fri, 24 Jan 2014 19:14:00 GMT
|6|name5|1390850040|Mon, 27 Jan 2014 19:14:00 GMT
|7|name6|1390936440|Tue, 28 Jan 2014 19:14:00 GMT
|8|name7|1396034040|Fri, 28 Mar 2014 19:14:00 GMT
|9|name8|1398708840|Mon, 28 Apr 2014 18:14:00 GMT
|10|name9|1390158840|Sunday, 19 Jan, 2014
|11|name10|1390072440|Saturday, 18 Jan, 2014
|12|name11|1390176000|20 Jan, 2015
server's today's date is 19 Jan 2014 but it is also giving me the list of 18 Jan 2014 but i am looking for the next 7 days record.and my current script is giving me the following out put:
2014-01-19 :20:44:30
2014-01-26 :20:44:30
Dues for upcoming 7 days
name1 => Mon, 20 Jan 2014 19:14:00 GMT
name2 => Tue, 21 Jan 2014 19:14:00 GMT
name3 => Wed, 22 Jan 2014 19:14:00 GMT
name4 => Fri, 24 Jan 2014 19:14:00 GMT
name9 => Sunday, 19 Jan, 2014
name10 => Saturday, 18 Jan, 2014
name11 => 20 Jan, 2015
So Why the 18 jan 2014 & 20 jan 2014 coming in record, where i am wrong.Please Help.
Thanks in Advance.
Try this code:
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN '$today' AND '$leadDate'") or die(mysql_error());
Instead of the CURDATE function use the $today.

Returning a 0 count from a FROM_UNIXTIME

Background
This is the API that powers a frontend Highcharts installation. A user has the option to select a "day" (24 hours), a week (7 days), or a custom interval, with no limitations. The x-axis (dates/time) needs to be completely dynamic due to the nature of the date range selected (this has been completed).
Y-Axis
The way we're calculating the X-Axis (dates) is working pretty good, but we're having trouble returning a null value on a certain day.
We're using this query to calculate the values:
SELECT COUNT(u.videoid) AS metric,
FROM_UNIXTIME(u.TIME, '%M %e, %Y') AS `sqltime`
FROM videos u
WHERE ( u.TIME >= 1291179600
AND u.TIME <= 1293858000 )
AND u.channel = '48'
GROUP BY `sqltime`
ORDER BY `sqltime` ASC
Here is a sample output:
metric sqltime
2 December 13, 2010
9 December 14, 2010
1 December 15, 2010
7 December 16, 2010
32 December 17, 2010
10 December 18, 2010
6 December 19, 2010
17 December 20, 2010
19 December 21, 2010
10 December 22, 2010
20 December 23, 2010
8 December 24, 2010
9 December 26, 2010
33 December 27, 2010
29 December 28, 2010
24 December 29, 2010
34 December 30, 2010
11 December 31, 2010
The problem is that it's not returning a value of December 25th (with 0 as the value of metric). This is causing Highcharts to skip over the value entirely, instead of showing a 0. So here's what the graph looks like currently:
But it really should look something like this:
Where the red line is at. The only way to do that is by returning a value of 0 for December 25th, 2010 (as an example).
Question
So the question again is, how do I modify my query to return a value of 0, for when there is no row in the videos table for that date/time and WHERE criteria?
Further Info
Here is how we are currently calculating the x-axis:
public function getCordinateCount()
{
$totalTime = $this->endTime - $this->startTime;
// 86400 seconds in a day.
$days = ceil($totalTime / 86400);
if($days <= 1) {
return 'FROM_UNIXTIME( u.time, \'%M %e, %Y %H:00\') AS `sqltime`';
} elseif ($days < 60) {
return 'FROM_UNIXTIME( u.time, \'%M %e, %Y\') AS `sqltime`';
} else {
return 'DATE_FORMAT(
SUBDATE(
FROM_UNIXTIME( u.time ),
INTERVAL WEEKDAY( FROM_UNIXTIME( u.time ) ) DAY
),
\'%M %e, %Y\') as `sqltime`';
}
}
I would try having a table DAYS with all days (from 1st of January to 31 of December).
Then do a LEFT JOIN. If the metric is null, then the result is ZERO =>
SELECT IF(tmp.metric IS NULL,0,tmp.metric),DAYS.datestr FROM DAYS LEFT JOIN (
SELECT COUNT(u.videoid) AS metric,
FROM_UNIXTIME(u.TIME, '%M %e, %Y') AS `sqltime`
FROM videos u
WHERE ( u.TIME >= 1291179600
AND u.TIME <= 1293858000 )
AND u.channel = '48'
GROUP BY `sqltime`
) tmp ON DAYS.datestr=tmp.`sqltime`
ORDER BY `sqltime` ASC
(I suppose here DAYS.datestr have the same format as sqltime).
That's how I would do it in the QUERY. But I really would do it in PHP, first set the whole array to 0 values , and then merge it with the result of the query.
HIH

Categories