MySQLi Select sum column order by day week - php

I have table:
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`amount` decimal(10,2) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
I need to have in result:
Total amount order by weekdays.
Email: test#mail.ru
Monday : 100
Tuesday : 33
Wednesday : 3461
Thursday: 0
Friday : 238
Saturday : 746
Sunday : 74
.....
Please help me to build query)

Here is a SQL query to solve your problem:
SELECT DAYNAME(created_date) as weekd, count(id) as cnt
FROM yourTable
WHERE created_date BETWEEN '2017-07-01 00:00:00' AND '2017-08-01 00:00:00'
GROUP BY weekd
The DAYNAME() function defines the names of the day of the week. Using GROUP BY will group by the day names. At the same time using COUNT() is the number of orders for each name of the day of the week.

SELECT SUM(amount)
FROM (
SELECT amount FROM table ORDER BY weekend
);

Related

Compare and insert date and calendar week with two different tables

I have two tables in my database. In the first table is a column named date where i insert a date period in a 1 day interval. In the second table are the calendar weeks of the same date period and an autoincrement column weekid.
For example I have the calendar week 25 with the weekid 145 (saved in the second table). The date area is from 21.06-27.06 and is saved in the first table.
Now i want to insert the weekid into the first table for every day (date) matching the calendar weeks.
Here are my tables:
CREATE TABLE `day` (
`dayid` int(255) NOT NULL AUTO_INCREMENT,
`userid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`dayid`)
CREATE TABLE `week` (
`weekid` int(255) NOT NULL AUTO_INCREMENT,
`userid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`calendar week` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` year(4) NOT NULL,
PRIMARY KEY (`weekid`
Example output for table "day":
weekid: 145
userid: 589
date: 2021-05-06
Example output for table "week":
weekid: 145
userid: 589
calendar week: 25
year: 2021
Does anyone have an idea how to do the date comparison?
If in the second table you don't have the dates of each week you can just calculate it on the first table right?
SELECT (EXTRACT(DOY FROM date_in_table1)/7)
should produce the work week of a certain date.

How to get rows from MySQL for the last 7 days?

How can I get the rows from table articles for the last 7 days?
Each row has a value timestmp where time is set via time().
I've tried this:
SELECT COUNT(*) FROM `articles` WHERE `timestmp`>NOW()-INTERVAL 168 HOUR
It doesn't work for me :(
The table is:
CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`link` text NOT NULL,
`article_name` text NOT NULL,
`descript` text NOT NULL,
`preview` text NOT NULL,
`content` text NOT NULL,
`category` int(11) NOT NULL,
`author` text NOT NULL,
`keywrds` text NOT NULL,
`timestmp` int(11) NOT NULL,
`modified` int(11) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT (`keywrds`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
The expected output is all the articles for the last 7 days with names, descriptions and so on.
Your timestmp column should be storing a UNIX timestamp, which is the number of seconds since the start of the UNIX epoch in January 1, 1970. So, if you just want records which happened exactly within the last 7 days, then you may just subtract 7 days (as seconds) from your timestmp column:
SELECT COUNT(*) AS cnt
FROM articles
WHERE timestmp > UNIX_TIMESTAMP() - 7*24*60*60;
If instead, you want records from 7 days ago, including the entire first day, then we need to do more work. In this case, we have to compute midnight on the first day, then convert that to a UNIX timestamp.
SELECT COUNT(*) AS cnt
FROM articles
WHERE timestmp > UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 7 DAY))

MySQL Query Custom Between DateTime

I'm in need of your help.
What I'm trying to achieve is the following:
Obtain both the withdrawal and deposit profit, for each day, for the past week.
So I'm hoping to get rows with the values: Day, Deposit Profit, Withdrawal Profit.
The catch however is that a day is a custom day, meaning: A day is between yyyy-mm-dd 13:00:00 and yyyy-mm-dd 13:00:00. So a group by date wouldn't be sufficient.
The query I've tried experimenting with was:
SELECT submit_date,
MAX(deposit_amount) - MIN(deposit_amount) AS deposit,
SUM(withdrawal_amount * withdrawal_percentage) as withdrawal
FROM `pro_Profits`
WHERE account_id = '{C795E1D2-452A-DEE8-A800-02E94332114A}'
AND submit_datetime >= NOW() - INTERVAL 1 WEEK
GROUP BY submit_date
ORDER BY `submit_datetime` DESC
Table:
CREATE TABLE IF NOT EXISTS `pro_Profits` (
`id` varchar(512) NOT NULL,
`account_id` varchar(512) NOT NULL,
`submit_date` date NOT NULL,
`submit_time` time NOT NULL,
`submit_datetime` datetime NOT NULL,
`deposit_amount` bigint(20) NOT NULL,
`withdrawal_amount` bigint(20) NOT NULL,
`deposit_percentage` double NOT NULL DEFAULT '1',
`withdrawal_percentage` double NOT NULL DEFAULT '0.4',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`),
KEY `account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
What you basically need to do is shift the day by 13 hours. You can use a function for this in MySQL:
TIMESTAMPDIFF(HOUR,13,submit_date)
In your SQL query this would look something like this:
SELECT
TIMESTAMPDIFF(HOUR,13,submit_date) as shifted_submit_date,
MAX(deposit_amount)-MIN(deposit_amount) AS deposit,
SUM(withdrawal_amount*withdrawal_percentage) as withdrawal
FROM
pro_Profits
WHERE
account_id = '{C795E1D2-452A-DEE8-A800-02E94332114A}' AND
submit_datetime >= NOW()-INTERVAL 1 WEEK
GROUP BY
shifted_submit_date
ORDER BY
submit_datetime DESC
A bit of experimenting might be needed to get exactly what you want. I find it strange that you group by one thing, and order by another.
You can try something like this:
SELECT
FLOOR(TIME_TO_SEC(TIMEDIFF(DATE_ADD(Date(NOW()), INTERVAL 13 Hour),submit_datetime))/86400.00) as Diff,
MAX(deposit_amount)-MIN(deposit_amount) AS deposit,
SUM(withdrawal_amount*withdrawal_percentage) as withdrawal
FROM
pro_Profits
WHERE account_id='{C795E1D2-452A-DEE8-A800-02E94332114A}'
and submit_datetime >= DATE_ADD(Date(NOW()), INTERVAL 13 Hour)-INTERVAL 1 WEEK
GROUP BY
Diff
ORDER BY
Diff
DATE_ADD(Date(NOW()), INTERVAL 13 Hour: You want to start from today at 13:00 and go back 1 week
TIME_TO_SEC(TIMEDIFF(DATE_ADD(Date(NOW()), INTERVAL 13 Hour),submit_datetime))/86400.00: Calculate difference in seconds between our date and 'submit_datetime'
FLOOR(...): we get the upper bound of that difference to create our day "buckets".
Note: count of "buckets" is actually 8, you can also find "-1" if there is a submit on the day you cast your query after 13:00. You can easily edit the above query to remove those results.

show events in next seven days when the event is having start and end date using mysql

table structure
CREATE TABLE `events` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`heading` VARCHAR( 255 ) NOT NULL ,
`start_date` DATE NOT NULL ,
`end_date` DATE NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB
i have a table events with heading,start_date and end_date. the problem is i want to show all the records which falls under next seven days either it can be start_date or end_date. sometimes the end date will not be specified because it will be to be announced, so that end_date will be 0000-00-00 and that is also to be taken care.
Try this
SELECT * FROM `events`
WHERE
/*Select with no end date */
(`end_date` = '0000-00-00' AND `start_date` BETWEEN NOW() AND ADDDATE(NOW(), 7))
/* SELECT ON THIS DAY */
OR NOW() BETWEEN `start_date` AND `end_date`
/* SELECT ON NEXT 7 DAY */
OR ADDDATE(NOW(), 7) BETWEEN `start_date` AND `end_date`

Show hourly clicks of a mysqli query for one day

I have the following table:
CREATE TABLE `visitors_table` (
`ID` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`visitor_ip` VARCHAR(32) NULL,
`visitor_browser` VARCHAR(255) NULL,
`visitor_hour` SMALLINT(2) NOT NULL DEFAULT '00',
`visitor_minute` SMALLINT(2) NOT NULL DEFAULT '00',
`visitor_date` DATE NOT NULL,
`visitor_day` SMALLINT(2) NOT NULL,
`visitor_month` SMALLINT(2) NOT NULL,
`visitor_year` SMALLINT(4) NOT NULL,
`visitor_page` VARCHAR(255) NULL
);
The following query gets the visitors per day:
"SELECT visitor_date FROM visitors_table WHERE visitor_date = CURDATE()";
Now I want output the data hourly. See this example:
Day Year Hours Clicks
July 4, 2011 00:00:00 4
July 4, 2011 01:00:00 12
July 4, 2011 02:00:00 75
July 4, 2011 03:00:00 27
and so on...
Could someone help me? I can't get clear with the output of the total visitors by hours for one day!
With the following query comes only this result:
SELECT visitor_date, count(*) FROM visitors_table WHERE visitor_date = CURDATE() GROUP BY visitor_hour
visitor_date count(*)
2013-08-31 4
2013-08-31 7
I need to get the results by hours like written as above!
Where is my mistake or how can i show it with php?
SELECT visitor_date, count(*) FROM visitors_table WHERE visitor_date = CURDATE() group by visitor_hour

Categories