It confuses me.. total left calculation seems doesn't work.
I am trying to get the total voucher and get total used and the total left.
please help.
SELECT
(IFNULL(SUM(value), 0)) AS total_voucher,
(
SELECT
IFNULL(SUM(value), 0))
FROM
voucher_history
WHERE
idUser = 1 AND isUsed = 1 AND DATE(FROM_UNIXTIME(datetime)) = '2014-03-04'
) AS total_used,
(total_voucher-total_used) AS total_left
FROM
voucher_history
WHERE
idUser = 1 AND isUsed = 0 AND DATE(FROM_UNIXTIME(datetime)) <= '2014-03-05'
You can do this with conditional aggregation, rather than using a subquery:
SELECT coalesce(SUM(value), 0)) AS total_voucher,
sum(case when is_used = 1 then value else 0 end) as total_used,
sum(case when is_used = 1 then 0 else value end) as total_left
FROM voucher_history
WHERE idUser = 1 AND DATE(FROM_UNIXTIME(datetime)) <= '2014-03-05';
Your query has the problem that it is trying to use column aliases (total_voucher and total_used) in the same select statement. SQL does not support this. You would need to use a subquery to get that functionality.
Related
I have a table with following stucture:
Now I would like to count the amount of "yes" per column over a not defined amount of lines and output a list in descending order:
C3 -> 3x
C1 -> 1x
...
with something like this i can get the number of yes'es but can not order the list:
count(case when C_1 = '[\"yes\"]' then 1 end) C1_count
Is there an elegant way to loop and sort true the table count and sort to generate the output list? And also be able in cas that C_11 will be added to expand the code?
Thanks a lot for assistance.
Here is a solution in Postgresql
select col_no, yes_count from unnest
(
(select array
[
count(case when C_1 = 'yes' then 1 end),
count(case when C_2 = 'yes' then 1 end),
...
count(case when C_10 = 'yes' then 1 end)
]::integer[] from _table
)
) with ordinality as t(yes_count, col_no)
order by yes_count desc;
It seems to me that your table design can be improved. And BTW pls. tag your RDBMS.
In MySQL, you can use:
select t.*,
( (c_1 = 'Yes') + (c_2 = 'Yes') + . . . + (c_10 = 'Yes')
) as num_yes
from t
order by num_yes desc;
Actually, I realize that the above assumes that the blank values are actually empty strings, not NULL. To handle NULLs use NULL-safe comparisons:
select t.*,
( (c_1 <=> 'Yes') + (c_2 <= >'Yes') + . . . + (c_10 <=> 'Yes')
) as num_yes
from t
order by num_yes desc;
I have the following query. When I run the query straight in MySQL I get all the information but when I run the same query in my PHP script it's missing some information. I ran the query in MySQL first to make sure that I I get everything I need and then copied the query into my php script. Can anyone please help:
$get_attendance_profile_stmt = $conn->prepare('SELECT
p_info.fname AS \'First name\',
p_info.lname AS \'Lastname\',
stud_info.t_belt_id,
t_b.short_descr,
t_b.long_descr,
stud_info.h_belt_id,
h_b.short_descr,
h_b.long_descr,
AB.*,
ROUND(
(
AB.Considered_Attended / AB.Classes_Tought * 100
),
0
) AS \'Attendance %\'
FROM
(
SELECT
student_id,
SUM(
CASE WHEN attendance_reason = 1 THEN 1 ELSE 0
END
) AS \'Sick\',
SUM(
CASE WHEN attendance_reason = 2 THEN 1 ELSE 0
END
) AS \'Injured\',
SUM(
CASE WHEN attendance_reason = 3 THEN 1 ELSE 0
END
) AS \'Work\',
SUM(
CASE WHEN attendance_reason = 4 THEN 1 ELSE 0
END
) AS \'School\',
SUM(
CASE WHEN attendance_reason = 5 THEN 1 ELSE 0
END
) AS \'Did not attend\',
SUM(
CASE WHEN attendance_reason = 6 THEN 1 ELSE 0
END
) AS \'Present\',
SUM(
CASE WHEN attendance_reason != 5 THEN 1 ELSE 0
END
) AS \'Considered_Attended\',
MAX(DateCounter) AS \'Classes_Tought\'
FROM
attendance a
JOIN(
SELECT COUNT(
DISTINCT CONVERT(creation_date, DATE)
) AS DateCounter
FROM
attendance
WHERE
creation_date <= (SELECT DATE_SUB(NOW(), INTERVAL -6 MONTH))
) DT
WHERE
creation_date <= (SELECT DATE_SUB(NOW(), INTERVAL -1 MONTH))
GROUP BY
student_id
) AB
JOIN student_info stud_info ON
stud_info.student_id = AB.student_id
JOIN users u ON
stud_info.uid = u.uid
JOIN personal_info p_info ON
u.uid = p_info.uid
LEFT JOIN t_belts t_b ON
stud_info.t_belt_id = t_b.t_belt_id
LEFT JOIN h_belts h_b ON
stud_info.h_belt_id = h_b.h_belt_id
WHERE
u.account_status = \'Active\'');
My output running the query inside MySQL is like this:
The output of the query in php looks like this:
Any help will be greatly appreciated.
PHP array's need to have unique indices. Your query returns 2 columns with the same name so the second values are what you get (they overwrite the first during assignment, simple example https://3v4l.org/sbs41 )). As can be seen in the screenshot your second columns are empty. Use an alias on the first columns (or the second) so your indices in PHP are unique. e.g.
SELECT
p_info.fname AS \'First name\',
p_info.lname AS \'Lastname\',
stud_info.t_belt_id,
t_b.short_descr,
t_b.long_descr,
stud_info.h_belt_id,
h_b.short_descr as short_descr_dontcare,
h_b.long_descr as long_descr_dontcare
or
SELECT
p_info.fname AS \'First name\',
p_info.lname AS \'Lastname\',
stud_info.t_belt_id,
t_b.short_descr as short_descr_real,
t_b.long_descr as long_descr_real,
stud_info.h_belt_id,
h_b.short_descr,
h_b.long_descr
with the latter approach you'll need to modify the indices in your PHP array with _real.
I have the following sql-statement that I want to transform into doctrine query builder. The goal is to count how many ratings exist with rating value 1 and with rating value 2.
SELECT
COUNT(CASE WHEN rating.rating = 1 THEN rating.rating END) as rat1,
COUNT(CASE WHEN rating.rating = 2 THEN rating.rating END) as rat2
FROM rating
This sql statement is working fine - but when I try to transform it into a Doctrine statement, it does not anymore. When nothing should get counted (because no ratings for this value exist), it returns me a "1" instead of a 0. How can I tell doctrine to simply return a zero when there is nothing to count?
I tried it by removing the "ELSE 0" , but then I get an error that this part is required..
return $qb
->addSelect('COUNT(CASE WHEN r.rating = 1 THEN r.rating ELSE 0 END) as rat_1')
->addSelect('COUNT(CASE WHEN r.rating = 2 THEN r.rating ELSE 0 END) as rat_2')
->getQuery()
->getResult();
Regards,
"sum" is not required - example:
votings 2,2,2,2,2 should return 5 , because the rating with value 2 got voted 5 times.
To count distinct id's in one column depending on the value in another column the answer from Fuzzy Tree does not work in this case.
To count only the distinct values you should give null as a parameter and set it to null like:
->addSelect('COUNT(DISTINCT(CASE WHEN r.rating = 1 THEN rating.rating ELSE :nada END)) as rat_1')
->setParameter(':nada', null)
As vkp mentioned, you can use SUM but instead of summing the rating, sum either a 1 or a 0 to simulate a COUNT
SUM(CASE WHEN r.rating = 1 THEN 1 ELSE 0 END)
I have a table with a field called type, where 3 various rows are possible: 1, 2 and 3.
Now, I don't care about 3 at all. I need to count how many rows there are with type = 1 and with type = 2. I am doing this with 2 queries, like this:
Query1: SELECT COUNT(id) as count FROM users WHERE type='1'
Query2: SELECT COUNT(id) as count FROM users WHERE type='2'
Can I do this with only 1 single query? If so, should I, or not? How would the query look?
SELECT type,
COUNT(id) AS count
FROM users
WHERE type IN ('1','2')
GROUP BY type
SELECT type, COUNT(id) AS count
FROM users
GROUP BY type
HAVING type < 3
SELECT sum(case when type = '1' then 1 else 0 end) as count_for_one,
sum(case when type = '2' then 1 else 0 end) as count_for_tow
FROM users
WHERE type IN ('1','2')
If you want separate numbers,
SELECT SUM(IF(type='1', 1, 0)), SUM(IF(type='2', 1, 0))
FROM users WHERE type IN ('1', '2')
I want to have an only query with 2 queries but i don't know how to start...
My queries count the positive/negative votes for a comment.
SELECT COUNT(id) AS votes_no FROM comments_votes WHERE vote = 0 AND id_comment = 1
SELECT COUNT(id) AS votes_no FROM comments_votes WHERE vote = 1 AND id_comment = 1
I set vars to put negative and positives votes : $votes_no and $votes_yes
Then i have a final var : $votes_calc = $votes_yes - $votes_no;
How can i get the number of votes_yes, votes_no and votes_calc in only one query?
Thanks a lot!
select votes_no, votes_yes, votes_yes-votes_no as votes_calc
from (select sum(case when vote = 0 then 1 else 0 end) as votes_no,
sum(case when vote = 1 then 1 else 0 end) as votes_yes
from comments_votes
where id_comment = 1) a
select vote,count(id)
from Comment_votes
group by vote
WHERE id_comment = 1
with rollup
The with Rollup will add a row with a NULL value in the vote column and the total in the second column
I merge the query getting the comments and the comments votes and it seems to work :)
SELECT a.*, nb_votes, votes_yes-votes_no AS votes_calc
FROM comments AS a
LEFT JOIN (
SELECT id_comment, COUNT(id) AS nb_votes,
SUM(CASE WHEN vote = 0 THEN 1 ELSE 0 END) AS votes_no,
SUM(CASE WHEN vote = 1 THEN 1 ELSE 0 END) AS votes_yes
FROM comments_votes GROUP BY id_comment
) AS c ON (a.id = c.id_comment)
WHERE a.status = 'approved' AND a.id_post = 1 ORDER BY a.time ASC
Thanks for your answers :)