There is a table named user_transaction which has following structure:
transaction_id mediumint(6) UNSIGNED (PK)
transaction_no varchar(55)
transaction_cc_avenue_no varchar(55)
transaction_card_category varchar(100)
transaction_user_id varchar(32)
transaction_user_name varchar(255)
transaction_user_email_id varchar(255)
transaction_deal_code varchar(10)
transaction_dc_id smallint(4)
transaction_amount float(10,2)
transaction_discount float(10,2)
transaction_total_amount float(10,2)
transaction_data_assign enum('0', '1')
transaction_status enum('success', 'inprocess', 'fail', 'cancelled')
transaction_date bigint(12)
transaction_update_date bigint(12)
transaction_update_user_id varchar(32)
I'm using UNIX Timestamp values in fields transaction_date and transaction_update_date to store the dates.
Now my issue is I'm getting today's date in the format dd/mm/yyyy say(11/07/2013) from the form in PHP.
After getting this date I want to find out the following counts for today(i.e. 11/07/2013) only:
total count of all the transactions carried out,
total count of all the transactions having transaction_status as 'success',
total count of all the transactions having transaction_status as 'inprocess',
total count of all the transactions having transaction_status as 'fail',
total count of all the transactions having transaction_status as 'cancelled'
The same output is also required for two dates(range of two dates with both dates inclusive).
I'm a newbie in this UNIX Timestamp manipulations.
Can anyone help me in this regard?
Thanks in advance.
SELECT COUNT(*) `carried out`,
SUM(transaction_status = 'success') `success`,
SUM(transaction_status = 'inprocess') `inprocess`,
SUM(transaction_status = 'fail') `fail`,
SUM(transaction_status = 'cancelled') `cancelled`
FROM tableName
-- WHERE add your conditions here
and since you want to get all records from today's date only, assuming from transaction_date
you can convert the unix date into date, eg
WHERE FROM_UNIXTIME(transaction_date) >= CURDATE() AND
FROM_UNIXTIME(transaction_date) < CURDATE() + INTERVAL 1 DAY
to filter out the today's transactions you can use WHERE date(now())==date(transaction_date)
or just create 2 dates from one 11/07/2013 converted to pair
11/07/2013 00:00:00 - 11/07/2013 23:59:59
Related
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
);
I have this table structure
CREATE TABLE `fixtures` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date` date NOT NULL DEFAULT '2017-03-23',
`goalsHomeTeam` int(11) NOT NULL DEFAULT '0',
`goalsAwayTeam` int(11) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
)
I want to get the items where date is either today or greater but whenever day matches it should only bring the items for that date only. So if I have 4 items in 23 Feb, and 3 items in 25 Feb, being both are greater than today I want only items for 23 Feb!
I have tried
SELECT *
FROM fixtures
WHERE date >= NOW();
But this brings both 23rd's and 25th's items!
To get the nearest date greater than today's date you can use the following query:
select min(date) from fixtures where date>current_date
or this:
select date from fixtures
where date>current_date
order by date limit 1
(current_date returns the current date without time information, so you can just use > instead of >=). Then your query will be like this:
select * from fixtures
where date=(select min(date) from fixtures
where date>current_date)
SELECT f.*
FROM fixtures f
INNER JOIN
(SELECT MIN(date) min_date
FROM fixtures
WHERE date >= NOW()) filter
ON filter.min_date = f.date
Try this:
SELECT *
FROM fixtures
WHERE date >= NOW() AND date = (SELECT MIN(date) FROM fixtures);
This will always return the elements from the smallest possible date you have stored in your database, which will always be today or greater than today.
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.
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`
I'm having a database table named user_transaction whose structure is as below:
transaction_id mediumint(6) UNSIGNED (PK)
transaction_no varchar(55)
transaction_cc_avenue_no varchar(55)
transaction_card_category varchar(100)
transaction_user_id varchar(32)
transaction_user_name varchar(255)
transaction_user_email_id varchar(255)
transaction_deal_code varchar(10)
transaction_dc_id smallint(4)
transaction_amount float(10,2)
transaction_discount float(10,2)
transaction_total_amount float(10,2)
transaction_data_assign enum('0', '1')
transaction_status enum('success', 'inprocess', 'fail', 'cancelled')
transaction_date bigint(12)
transaction_update_date bigint(12)
transaction_update_user_id varchar(32)
I'm using UNIX Timestamp values to store the dates in the column transaction_date. Now I want to display the monthwise no. of transactions of different transaction_status(i.e.Total transactions took place on each date,total no. of transactions of status 'success', 'inprocess', 'fail' and 'cancelled') for all the transaction_dates present in a table. Those records should be grouped by month. I tried a lot to get this result but not getting any success. For your reference I'm giving below my query:
SELECT COUNT(*) `total count`, SUM(transaction_status = 'success') `success`, SUM(transaction_status = 'inprocess') `inprocess`, SUM(transaction_status = 'fail') `fail`, SUM(transaction_status = 'cancelled') `cancelled` FROM user_transaction GROUP BY FROM_UNIXTIME(transaction_date)
Can anyone help me in this regard? Thanks in advance.
It will be a lot easier if you just set transaction_date as a date. Then you can use the built-in functions.
For example, you could do
SELECT year(transaction_date), month(transaction_date),
sum(whatever), ...
FROM user_transaction
GROUP BY YEAR(transaction_date), MONTH(transaction_date)
ORDER BY YEAR(transaction_date), MONTH(transaction_date);
If you have an aversion to changing the data type of transaction_date, then you can convert it on the fly with FROM_UNIXTIME. I don't recommend that, because it is an extra (unnecessary) layer of processing in every query. Nonetheless, that would mean you can leave your schema the way it is and write:
SELECT year(FROM_UNIXTIME(transaction_date)), month(FROM_UNIXTIME(transaction_date)),
sum(whatever), ...
FROM user_transaction
GROUP BY YEAR(FROM_UNIXTIME(transaction_date)), MONTH(FROM_UNIXTIME(transaction_date))
ORDER BY YEAR(FROM_UNIXTIME(transaction_date)), MONTH(FROM_UNIXTIME(transaction_date));
Nasty, isn't it? You're better doing a 'ALTER TABLE CHANGE COLUMN transaction_date transaction_date date' and then you get all the advantages of doing date math with the easy to use MySQL functions.