I have a simple log table in my database. For each page visit a row is inserted. Now i want to sum up a certain column for today and yesterday.
I tried the following query:
SELECT Sum(CASE
WHEN `time` > 1364767200 THEN `sqlquerycount`
end) AS `today`,
Sum(CASE
WHEN `time` BETWEEN 1364684400 AND 1364767200 THEN `sqlquerycount`
end) AS `yesterday`
FROM `log`
which results of the following PHP script:
$sql = "SELECT
SUM(CASE WHEN `time` > ".strtotime("today")." THEN `sqlQueryCount` END) AS `today`,
SUM(CASE WHEN `time` BETWEEN ".strtotime("yesterday")." AND ".strtotime("today")." THEN `sqlQueryCount` END) AS `yesterday`
FROM `log`";
"yesterday" is remaining NULL, but I don't see any problem with this query. Any idea?
I'd bet for
$sql = "SELECT
SUM(CASE WHEN `time` > ".strtotime("today")." THEN `sqlQueryCount` ELSE 0 END) AS `today`,
SUM(CASE WHEN `time` BETWEEN ".strtotime("yesterday")." AND ".strtotime("today")." THEN `sqlQueryCount` ELSE 0 END) AS `yesterday`
FROM `log`";
... provided sqlQueryCount is the the column you want to do the sum on!
Related
Trying to create a mysql query where it count the names of the filters database. Successful, getting names from a certain period is the main goal.
$query = query("
SELECT count(CASE WHEN filter='冬越しする(現在地検索)' THEN 1 END) as filter,
count(CASE WHEN filter='開花期で選ぶ' THEN 1 END) as filter2,
count(CASE WHEN filter='日当たりで選ぶ' THEN 1 END) as filter3,
count(CASE WHEN filter='株幅で選ぶ' THEN 1 END) as filter4,
count(CASE WHEN filter='背丈で選ぶ' THEN 1 END) as filter5,
count(CASE WHEN filter='機能、タイプで選ぶ' THEN 1 END) as filter6,
count(CASE WHEN filter='ビギナーおすすめ' THEN 1 END) as filter7 FROM
filter_clicked WHERE filter_date < DATEADD(day, -30, GETDATE())");
you've to user now()-now()-interval 30 day - what you've used it is sql server function
$query = query("
SELECT count(CASE WHEN filter='冬越しする(現在地検索)' THEN 1 END) as filter,
count(CASE WHEN filter='開花期で選ぶ' THEN 1 END) as filter2,
count(CASE WHEN filter='日当たりで選ぶ' THEN 1 END) as filter3,
count(CASE WHEN filter='株幅で選ぶ' THEN 1 END) as filter4,
count(CASE WHEN filter='背丈で選ぶ' THEN 1 END) as filter5,
count(CASE WHEN filter='機能、タイプで選ぶ' THEN 1 END) as filter6,
count(CASE WHEN filter='ビギナーおすすめ' THEN 1 END) as filter7 FROM
filter_clicked WHERE filter_date < date(now())-interval 30 day")
I am trying to display output in below format :
Error :
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as minrange, sum(case when DATE(holddate) = CURDATE()) as holddate, sum(case' at
code :
<?php
$sql =
"select
sum(case when DATE(reattemptdate) = CURDATE()) as minrange,
sum(case when DATE(holddate) = CURDATE()) as holddate,
sum(case when DATE(reattemptdate) = DATE(NOW() - INTERVAL 1 DAY)) as prev_reattemptdate,
sum(case when DATE(holddate) = DATE(NOW() - INTERVAL 1 DAY)) as prev_holddate
from orders
";
$results = $db_handle->runSelectQuery($sql);
$numrowsresult =$results[0]['count'];
echo $numrowsresult;
?>
I needed this : THEN 1 ELSE 0 END
select sum(case when DATE(reattemptdate) = CURDATE() THEN 1 ELSE 0 END) as reattemptdate,
sum(case when DATE(holddate) = CURDATE() THEN 1 ELSE 0 END) as holddate,
sum(case when DATE(reattemptdate) = DATE(NOW() - INTERVAL 1 DAY) THEN 1 ELSE 0 END) as prev_reattemptdate,
sum(case when DATE(holddate) = DATE(NOW() - INTERVAL 1 DAY) THEN 1 ELSE 0 END) as prev_holddate
from orders;
You have a CASE WHEN but you never specify the THEN (and ELSE).
Therefore the syntax is missing something.
I have been having trouble with a project i've been working on for about 5 weeks now, i've made various stackoverflow posts along the way and i'm almost at the final hurdle.
I was having issues with duplicated data searching weekly sums but I seem to have figured that out but now my statement isn't completing.
REf:
Weekly Sum Dynamic Pivot MYSQL
Here is a fiddle with the data.
http://sqlfiddle.com/#!9/a3610
$period = 'YEARWEEK';
$sql = "
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN (".$period."(date)) = ',
(".$period."(date)),
' THEN AMOUNT else 0 END) AS `',
(".$period."(date)),
'`'
)
ORDER BY date ASC ) AS `pivot_columns`
FROM record_offering
WHERE date BETWEEN ? AND ?
ORDER BY date ASC
";
$stmt = $pdo->prepare($sql);
$date_from = '2017-01-01';
$date_to = '2017-10-01';
$stmt->execute([$date_from, $date_to]);
$row = $stmt->fetch();
$stmt->closeCursor();
$pivot_columns = $row['pivot_columns'];
$sql = "
SELECT title AS `Service`, {$pivot_columns}
from record_offering t1
join setting_service ON t1.service_id = setting_service.id
WHERE t1.date BETWEEN ? AND ?
GROUP BY title asc WITH ROLLUP
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_from, $date_to]);
$results = $stmt->fetchAll();
$stmt->closeCursor();
As you can see the last statement is incomplete:
SELECT title AS `Service`, SUM(CASE WHEN (YEARWEEK(date)) = 201635 THEN AMOUNT else 0 END) AS `201635`,
SUM(CASE WHEN (YEARWEEK(date)) = 201703 THEN AMOUNT else 0 END) AS `201703`,
SUM(CASE WHEN (YEARWEEK(date)) = 201709 THEN AMOUNT else 0 END) AS `201709`,
SUM(CASE WHEN (YEARWEEK(date)) = 201713 THEN AMOUNT else 0 END) AS `201713`,
SUM(CASE WHEN (YEARWEEK(date)) = 201715 THEN AMOUNT else 0 END) AS `201715`,
SUM(CASE WHEN (YEARWEEK(date)) = 201717 THEN AMOUNT else 0 END) AS `201717`,
SUM(CASE WHEN (YEARWEEK(date)) = 201718 THEN AMOUNT else 0 END) AS `201718`,
SUM(CASE WHEN (YEARWEEK(date)) = 201722 THEN AMOUNT else 0 END) AS `201722`,
SUM(CASE WHEN (YEARWEEK(date)) = 201723 THEN AMOUNT else 0 END) AS `201723`,
SUM(CASE WHEN (YEARWEEK(date)) = 201725 THEN AMOUNT else 0 END) AS `201725`,
SUM(CASE WHEN (YEARWEEK(date)) = 201726 THEN AMOUNT else 0 END) AS `201726`,
SUM(CASE WHEN (YEARWEEK(date)) = 201735 THEN AMOUNT else 0 END) AS `201735`,
SUM(CASE WHEN (YEARWEEK(date)) = 201736 THEN AMOUNT else 0 END) AS `201736`,
SUM(CASE WHEN (YEARWEEK(date)) = 201
from record_offering t1
join setting_service ON t1.service_id = setting_service.id
WHERE t1.`date` BETWEEN ? AND ?
GROUP BY title asc WITH ROLLUP
I have tried escaping the query in various ways but either the query completes and my data is duplicated or it doesn't compile at all.
GROUP_CONCAT has a limit off 1024 bytes.
Use
SET SESSION group_concat_max_len = ##max_allowed_packet
Before the GROUP_CONCAT query.
To my way of thinking, this (or something very like it) is ALL the sql you need for this problem. Everything else can, and should, be handled in the presentation layer.
SELECT YEARWEEK(x.date) yw
, x.title
, COALESCE(SUM(y.amount),0) total
FROM setting_service x
LEFT
JOIN record_offering y
ON y.service_id = x.id
GROUP
BY yw
, x.id;
I'm using PHP and MySQL in my website. There is one table in my database named users. It has two fields viz.user_reg_date(bigint(12)) and user_last_login(bigint(12)). These two fields store the date in UNIX TIMESTAMP format. The whole table structure is as follows:
user_id varchar(32)
user_title enum('Mr', 'Ms', 'Mrs')
user_first_name varchar(50)
user_last_name varchar(50)
user_name varchar(100)
user_password varchar(50)
user_email varchar(150)
user_dob date
user_hybridauth_p_name varchar(100)
user_hybridauth_p_uid varchar(100)
user_reg_date bigint(12)
user_status enum('enable', 'disable')
user_subscription enum('lifetime', 'period')
user_update_date bigint(12)
user_last_login bigint(12)
user_last_activity bigint(12)
user_created_staff_id varchar(32)
user_updated_staff_id varchar(32)
user_registered_type enum('online', 'manual')
Now I want to display the record counts i.e. the count of users whose user_reg_date falls within the dates of current week as well the record count of users whose user_last_login falls within the dates of current week. The other queries are also required to display such record counts of last week as well as last two weeks from the current date.
I also want to display the same record counts monthwise. Can anyone help me in achieving this? Thanks in advance. I ried to fetch such counts between two dates it worked perfectly to some extent. It is giving me the date wise count of user_reg_date but not the count of users last logged in. I'm just not able to write perfect queries. The query I written for this is as follows :
SELECT date( FROM_UNIXTIME( user_reg_date ) ) 'Current Date', COUNT( user_reg_date ) 'registered_user_count', COUNT( user_last_login ) 'logged_in_count'
FROM users
WHERE user_reg_date >=1341100800
AND user_reg_date <=1374451200
AND user_last_login >=1341100800
AND user_last_login <=1374451200
GROUP BY date( FROM_UNIXTIME( user_reg_date ) ) , date( FROM_UNIXTIME( user_last_login ) )
You can do something like this
SELECT
(SELECT COUNT(*)
FROM users
WHERE user_last_login BETWEEN UNIX_TIMESTAMP(ADDDATE(CURDATE(), INTERVAL 1-DAYOFWEEK(CURDATE()) DAY))
AND UNIX_TIMESTAMP(ADDDATE(CURDATE(), INTERVAL 7-DAYOFWEEK(CURDATE()) DAY))
) logged_in_count,
(SELECT COUNT(*)
FROM users
WHERE user_reg_date BETWEEN UNIX_TIMESTAMP(ADDDATE(CURDATE(), INTERVAL 1-DAYOFWEEK(CURDATE()) DAY))
AND UNIX_TIMESTAMP(ADDDATE(CURDATE(), INTERVAL 7-DAYOFWEEK(CURDATE()) DAY))
) registered_user_count
Here is SQLFiddle demo
SELECT columnName
SUM(CASE WHEN REQUESTMONTH=1 THEN (REQUESTTOTAL) END) Jan,
SUM(CASE WHEN REQUESTMONTH=2 THEN (REQUESTTOTAL) END) Feb,
SUM(CASE WHEN REQUESTMONTH=3 THEN (REQUESTTOTAL) END) Mar,
SUM(CASE WHEN REQUESTMONTH=4 THEN (REQUESTTOTAL) END) Apr,
SUM(CASE WHEN REQUESTMONTH=5 THEN (REQUESTTOTAL) END) May,
SUM(CASE WHEN REQUESTMONTH=6 THEN (REQUESTTOTAL) END) Jun,
SUM(CASE WHEN REQUESTMONTH=7 THEN (REQUESTTOTAL) END) July,
SUM(CASE WHEN REQUESTMONTH=8 THEN (REQUESTTOTAL) END) Aug,
SUM(CASE WHEN REQUESTMONTH=9 THEN (REQUESTTOTAL) END) Sep,
SUM(CASE WHEN REQUESTMONTH=10 THEN (REQUESTTOTAL) END) 'Oct',
SUM(CASE WHEN REQUESTMONTH=11 THEN (REQUESTTOTAL) END) Nov,
SUM(CASE WHEN REQUESTMONTH=12 THEN (REQUESTTOTAL) END) 'Dec'
FROM tableName
I want to fetch 2 coulmns count and do their total as a new column.
How can I do this?
i wrote this query, but this is returning wrong total.
SELECT count(case when `status`='1' then 1 else 0 end) AS HOT,
count(case when `status`='5' then 1 end)
AS Special_Case,count(case when 1=1 then 1 end) AS TOTAL
FROM `tbl_customer_conversation` group by
date(`dt_added`),user_id
COUNT will only give the times a record is matched, which in your query will always return 1. Because the values can either be 1 or 0. So count(1) is also 1 and count(0) is also 1.
AS, you want the total number of HOT cases and SPECIAL_CASE you have to use SUM.
SELECT
SUM(case when `status`='1' then 1 else 0 end) AS HOT,
SUM(case when `status`='5' then 1 end) AS Special_Case,
SUM(case when `status` = '1' or `status` = '5' then 1 end) AS TOTAL
FROM `tbl_customer_conversation`
group by date(`dt_added`),user_id