I have a table called 'Bill' which is a table for a database for a restaurant system. Sample data is below:
Bill_Code, Date, Customer_ID, Order_Code, Total_Amount, Discount, Payable_Amount, Payment_Type, Staff_Name
'B201404012000', '2014-03-21', 'S8909608D', 'O222224', '0.00', '0.00', '0.00', 'CASH', 'Dany Lim'
'B201404012030', '2014-04-01', 'S8409608D', 'O222221', '100.00', '0.00', '100.00', 'VISA', 'Arya Stark'
'B201404012032', '2014-04-01', 'S8609608D', 'O222222', '80.00', '0.10', '72.00', 'VISA', 'Arya Stark'
'B201404012033', '2014-04-21', 'S8709608D', 'O222223', '0.00', '0.00', '0.00', 'NETS', 'Rob Targa'
'B201404012039', '2014-04-01', 'S8709655D', 'O222225', '0.00', '0.00', '0.00', 'MASTERCARD', 'Dany Lim'
'B201404012053', '2014-04-15', 'S8909608D', 'O222224', '0.00', '0.00', '0.00', 'CASH', 'Dany Lim'
'B201404012115', '2014-04-11', 'S8909608D', 'O222226', '50.00', '0.00', '50.00', 'CASH', 'Rob Targa'
I need to find which two dates with the same Customer_ID have a date interval of 30 days.
Meaning, I need to find out if any particular customer has had a lapse of 30 days between visits.
Appreciate your help. Thanks in advance.
If you need to know returning customers with date between first and last order more than 30 days try to use GROUP BY:
SELECT Customer_ID,
MIN(Date) as MinDate,
MAX(Date) as MaxDate
FROM BILL
GROUP BY Customer_ID
HAVING DATEDIFF(MAX(Date),MIN(Date))>=30
You try this way :
To convert a time string to the number of seconds, use the strftime function with the %s modifier. (A time string without a date part will be assumed to have the date 2000-01-01, but this cancels out when computing the differences.)
To compute the pause times for a specific production record, use a correlated subquery; the total aggregate is needed to cope with zero/one/multiple matching pauses.
For example :
SELECT date,
item,
sum(strftime('%s', end) - strftime('%s', begin) -
(SELECT total(strftime('%s', end) - strftime('%s', begin))
FROM pause
WHERE pause.date = production.date
AND pause.begin >= production.begin
AND pause.end <= production.end)
) AS seconds
FROM production
GROUP BY date,
item
Related
Here is the schema:
CREATE TABLE `available_timings` (
`id` bigint(20) NOT NULL ,
`from_time` time DEFAULT NULL,
`to_time` time DEFAULT NULL
);
INSERT INTO `available_timings` (`id`, `from_time`, `to_time`) VALUES
(1, '15:11:00' , '17:15:00'),
(2, '15:11:00', '15:11:00'),
(3, '09:00:00', '12:30:00'),
(4, '15:40:00', '15:40:00'),
(5,'13:30:00', '17:15:00'),
(6, '16:10:00', '16:10:00'),
(7, '07:45:00', '11:45:00'),
(8, '19:00:00', '22:30:00'),
(9, '16:14:00', '16:14:00'),
(10, '09:30:00', '17:45:00'),
(11, '10:30:00','15:15:00');
http://sqlfiddle.com/#!9/fc9afe/2
I am trying to achieve whether the current time falls between from time and to time in mysql
SELECT *
FROM `available_timings`
WHERE curtime() >=`from_time` or curtime() <=`to_time`
i have searched many forum and also tried few queries but couldn't succeeded.
Can any one help me here to solve my problem
Thank you
Use BETWEEN
SELECT *
FROM `available_timings`
WHERE curtime() BETWEEN `from_time` AND `to_time`
Use AND instead of OR
WHERE curtime() >= `from_time`
AND curtime() <= `to_time`
You don't want any of these conditions to be true. You want both of them to be true.
I have problem with count multiple fields within time range.
I have following table:
# date, count, fb_user_count, email_user_count, reg_dt
'2015-10-27', '11', '6', '5', '2015-11-02 13:59:14'
'2015-10-26', '3', '1', '2', '2015-11-02 13:59:10'
I want to get weekly number of registration with types, like this:
# date, count, fb_user_count, email_user_count, reg_dt
'2015-11-02', '31', '16', '15', '2015-11-02 13:59:14'
'2015-11-09', '12', '6', '6', '2015-11-09 13:59:14'
And monthly:
# date, count, fb_user_count, email_user_count, reg_dt
'2015-11', '131', 'x', 'y', '2015-11-02 13:59:14' (the last value is not so important)
'2015-12', '112', 'x', 'y', '2015-12-09 13:59:14'
I tried different approaches, but I struggle to finish this task. Any help will be great. Thanks!
You can use MySQL Date and Time functions WEEK(), MONTH() and YEAR() to build your queries :
SELECT
MIN(date),
SUM(count),
SUM(fb_user_count),
SUM(email_user_count)
FROM visit
GROUP BY WEEK(date, 1); -- First day is Monday
SELECT
CONCAT(YEAR(date), '-', MONTH(date)),
SUM(count),
SUM(fb_user_count),
SUM(email_user_count)
FROM visit
GROUP BY YEAR(date), MONTH(date);
For your weekly report (on mondays) you can use
select subdate(min(date), 6-weekday(min(date))),
sum(count), sum(fb_user_count), sum(email_user_count), max(reg_dt)
from tablename
group by subdate(date, 6-weekday(date));
and for your monthly report
select concat(year(max(date), '-', month(max(date)),
sum(count), sum(fb_user_count), sum(email_user_count), max(reg_dt)
from tablename
group by year(date), month(date);
I have a table of twitter data in MYSQL where the columns is_retweet, is_reply is made of binary values where 1=yes, 0=no. if a user retweeted multiple times in a day, there would then be multiple rows of ones in the retweet coulmn for that user on that day.
account_id, datetime, user_screenname, is_retweet, is_reply,followers_count
'9', '2008-06-11 20:06:35','Access2', '1', '0', '811'
'9', '2008-06-11 23:06:35','Access2', '1', '1', '812'
'9', '2008-06-12 20:01:21','Access2', '0', '1', '813'
'7', '2008-06-11 17:01:00','actingparty', '1', '1', '2000'
I rearrange my sql output to a table below which tells me: for a username on any day, what is the total number of retweets, replies and highest follower count.
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count
'9', '2008-06-11', 'Access2', '2', '0', '812'
'9', '2008-06-12', 'Access2', '0', '1', '813'
Here is my sql code:
CREATE VIEW `tweet_sum` AS
select
`tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
from
`tweets`
group by cast(`tweets`.`datetime` as date), tweets.username
Ultimately, I want to have one more column Reach (which is equal to followers_count times the number of columns(is_retweet, is_reply) that is greater than zero.)
For example, in the output table below, the sum_retweet and sum_reply columns are both greater than zero for 2008-06-11 so i will need to take followers_count*2=1624 for the reach column.
How can i structure my sql code to do that?
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, **Reach**
'9', '2008-06-11', 'Access2', '2', '1', '812', '1624'
'9', '2008-06-12', 'Access2', '0', '1', '813', '813'
I thought of doing it this way:
1.create a new view
2.count the number of columns that have values >0
3.then take that number multiply by followers count for that day
And the code for that below:
CREATE VIEW tweet_reach AS
SELECT
COUNT(t.sum_reply,t.sum_retweet,t.sun_mention,t.sum_direct,t.sum_mytweet)*t.followers_count AS Reach
FROM information_schema.columns
WHERE table_name='tweet_sum' t AND
t.sum_reply>0 OR
t.sum_retweet>0 OR
t.sun_mention>0 OR
t.sum_direct>0 OR
t.sum_mytweet>0;
This code is wrong but hoping to do something like this. Is it possible?
Thanks,
J
You can do this easily by adding a column in your existing view:
CREATE VIEW `tweet_sum` AS
select `tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
MAX(`tweets`.`followers_count`) * ((SUM(`tweets`.`is_reply`) > 0) + (SUM(`tweets`.`is_retweet`) > 0)) as reach
from `tweets`
group by cast(`tweets`.`datetime` as date), tweets.username;
MySQL treats a boolean expression such as x = y as the integer 1 when true and 0 when false. So, you can just add them together for your multiplication factor.
I have a table of twitter data in MYSQL where the columns is_retweet, is_reply is made of binary values where 1=yes, 0=no. if a user retweeted multiple times in a day, there would then be multiple rows of ones in the retweet coulmn for that user on that day.
account_id, datetime, user_screenname, is_retweet, is_reply,followers_count
'9', '2008-06-11 20:06:35','Access2', '1', '0', '811'
'9', '2008-06-11 23:06:35','Access2', '1', '1', '812'
'9', '2008-06-12 20:01:21','Access2', '0', '1', '813'
'7', '2008-06-11 17:01:00','actingparty', '1', '1', '2000'
How should i structure my SQL view to give me a result like the table below where i can sum up the retweets and replies for any given day, and by username?
IE What i am trying to do is:
-for a username on any day, what is the total number of retweets, replies and highest follower count.
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count
'9', '2008-06-11', 'Access2', '2', '0', '812'
'9', '2008-06-12', 'Access2', '0', '1', '813'
Here is my sql code:
CREATE VIEW `tweet_sum` AS
select
`tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
from
`tweets`
group by cast(`tweets`.`datetime` as date)
However my data dont seem to match with what i want as it seems that the sql is summing up all users retweets for that day. How can i group it by day and username as well?
Thanks!
J
******EDIT*************************************
I would like to extend the question. Say I have one more column Reach (which is equal to followers_count times the number of columns(is_retweet, is_reply) that is greater than zero.)
For example, in the output table below, the sum_retweet and sum_reply columns are both greater than zero for 2008-06-11 so i will need to take followers_count*2=1624 for the reach column.
How can i structure my sql code to do that?
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, **Reach**
'9', '2008-06-11', 'Access2', '2', '1', '812', '1624'
'9', '2008-06-12', 'Access2', '0', '1', '813', '813'
just change your GROUP BY to
group by
`tweets`.`account_id`,
`tweets`.`user_screenname`,
cast(`tweets`.`datetime` as date)
I want to draw a graph accurately, Time vs Site Visits.
X axis will be 4, 8, 12, 16, 20, 24. That's increments of four hours.
Y axis total number of visits by first 4 hours, then by next four hours etc.
How can I do it using MySql? There might be some tricks using GROUP BY, but I couldn't get it. I stored all visit to my site, used unix time stamp for time.
The query can be like this -
SELECT FLOOR(HOUR(FROM_UNIXTIME(unix_ts)) / 4) period, COUNT(*) visit_count_per_4_hours FROM visits_table
WHERE DATE(FROM_UNIXTIME(unix_ts)) = DATE(NOW())
GROUP BY period;
This query returns visits for specified day, otherwise calculation should be modified.
TRY
SELECT SUM( visit ) , HOUR( `time_column` )
FROM time_table
WHERE DATE_SUB( `time_column` , INTERVAL 4 HOUR )
GROUP BY HOUR( `time_column` )
working example
CREATE TABLE IF NOT EXISTS `time_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`waqt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`visit` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `time_table`
--
INSERT INTO `time_table` (`id`, `waqt`, `visit`) VALUES
(1, '2011-07-28 13:29:04', 3),
(2, '2011-07-28 15:29:10', 4),
(3, '2011-07-28 13:45:35', 7),
(4, '2011-07-28 15:00:47', 5),
(5, '2011-07-28 14:45:03', 6),
(6, '2011-07-28 13:00:21', 3);
and then i execute per hour visit
SELECT SUM(visit), HOUR(waqt)
FROM time_table
WHERE DATE_SUB(`waqt`,INTERVAL 1 HOUR) GROUP BY HOUR(waqt)