I have the following query:
SELECT COUNT(package) as advanced_count FROM users WHERE package = '2' AND site_url is NOT NULL;
What I want to do is have 2 more queries to get the basic_count and then the total_count which is basic + advanced.
My other basic query is:
SELECT count(package) as basic_count FROM users WHERE package = '1' AND site_url is NOT NULL
But I'm not sure how to combine the two so it is just one query, and then plus adding the total number in there as well.
I hope someone can point me in the right directions.
Thank you!
SELECT SUM(CASE WHEN package = '2' THEN 1 ELSE 0 END) AS advanced_count,
SUM(CASE WHEN package = '1' THEN 1 ELSE 0 END) AS basic_count,
COUNT(*) AS total_count
FROM users
WHERE site_url IS NOT NULL
AND package IN ('1', '2');
Related
I have a table which traces the users records I want to know which are the complete and process users's records based on their status
Here is the sql query
SELECT users.UserID,users.UserName,users.FirstName,users.LastName,users.Email,
CASE WHEN inword.inword_status = '3' THEN count(*) END As 'Process' ,
CASE WHEN inword.inword_status = '4' THEN count(*) END AS 'Complete'
FROM tbl_user users
INNER JOIN tbl_inword inword on users.UserID=inword.UserID
Where inword.Status=1 and users.Status=1 and
inword.CreatedDate BETWEEN '2020-10-01' and '2020-10-31' and inword.inword_status in (3)
group by users.UserID
Here is Query Output
My Expected result is
UserID Name Total Process Complete
1 Umair 1 1 0
1 Basit 20 20 0
1 Zaidi 34 32 2
Any Help would be Appreciated
You're not doing your conditional aggregation correctly, you should use something like:
COUNT(CASE WHEN inword.inword_status = '3' THEN inword.UserId END) As 'Process' ,
COUNT(CASE WHEN inword.inword_status = '4' THEN inword.UserId END) AS 'Complete'
Or you can take advantage of MySQL treating booleans as 1 or 0 in a numeric context and simplify to:
SUM(inword.inword_status = '3') As 'Process' ,
SUM(inword.inword_status = '4') AS 'Complete'
I have seperate queries but i need to reduce the no so put all in one
select count(applicant_id) as registered from student_application where filter_status=0 AND
select count(applicant_id) as filer_select from student_application where filter_status=1 AND
select count(applicant_id) as filter_reject from student_application where filter_status=2
but this shows some errors
Use CASE expression.
Query
select
count(case when filter_status = 0 then applicant_id else null end) as registered,
count(case when filter_status = 1 then applicant_id else null end) as filer_select,
count(case when filter_status = 2 then applicant_id else null end) as filer_reject
from student_application;
SQL Fiddle
You could also use group_by, with the where clause if you're looking for a subset rather than all possible values of filter_status:
SELECT filter_status, COUNT(*)
FROM student_application
WHERE filter_status in (0, 1, 2)
GROUP BY filter_status;
I have a table like this-
I want to run a query so that I can have a output like it-
I don't have a clear idea how to do it.
So, what I have done is-
SELECT
Count(* where status="Active") as count_active
Count(* where status="Inctive") as count_inactive
Count(* where status="Biase") as count_biase
FROM `subscribers`
And getting error, can anyone please help?
Can achieve this with a Case expression.
Query
select
count(case when status = 'Active' then 1 else null end) as Active_Count,
count(case when status = 'Inactive' then 1 else null end) as Inctive_Count,
count(case when status = 'Biase' then 1 else null end) as Biase_Count
from tbl_name;
SQL Fiddle
This is the way to put it in a single register:
SELECT
(SELECT count(id) FROM new_table where status = 'Active') as Active_Count,
(SELECT count(id) FROM new_table where status = 'Inactive') as Inctive_Count,
(SELECT count(id) FROM new_table where status = 'Biase') as Biase_Count;
Having trouble getting my head around this one, it likely doesn't help I don't have any SQL 'group by' experience.
I have a table that has a transaction_type column and an amount column. Basically I am trying to use SQL/Eloquent to get the following in a single query (if possible, I know this isn't sql):
add = SUM(amount) where transaction_type == 1
delete = SUM(amount) WHERE transaction_type == 2
return from MySQL: add-delete
I'm assuming this would be done using groupby, but despite my best efforts I haven't be able to find a solution by reading the sql documentation.
I think I have got it using plain SQL, but how would I convert this to Eloquent:
SELECT `transaction_type`, SUM(`amount`) FROM `credit_logs` WHERE `to_group_id` = '1'
I can't help you with eloquent, but your query should be something like this:
SELECT
SUM(CASE WHEN transaction_type = 1 THEN amount ELSE NULL END)
-
SUM(CASE WHEN transaction_type = 2 THEN amount ELSE NULL END)
FROM credit_logs
WHERE to_group_id = 1;
or for all id's you wish to group by:
SELECT
SUM(CASE WHEN transaction_type = 1 THEN amount ELSE NULL END)
-
SUM(CASE WHEN transaction_type = 2 THEN amount ELSE NULL END)
FROM credit_logs
GROUP BY to_group_id;
Easiest would be, if you'd find a way to just execute a query with eloquent. Never understood why someone wants to transform a nice readable query into some "improved" syntax of a framework.
In MySQL (v5.5), the value of numNotCast, numInFavor is always 0 when selecting all rows of the view. If the select statement is executed alone (not in the view), it works as expected returning the correct count of the the number of rows in the vote column equal to value 'notcast' and 'infavor'.
CREATE OR REPLACE
VIEW `Stats` AS
select
sum(case when `p`.`vote` = 'notcast' then 1 else 0 end) AS `numNotCast`,
sum(case when `p`.`vote` = 'infavor' then 1 else 0 end) AS `numInFavor`
from
(`Debate` `d`
join `Participant` `p` ON ((`d`.`debateId` = `p`.`debateId`)))
group by `d`.`debateId`
Is this a limitation on MySQL views? How do you accomplish this conditional summing function in the view?
Then your statement is correct as SQLFiddle confirms, so your problem is probably somewhere else
CREATE OR REPLACE VIEW Stats AS
select
Debate.debateId,
sum(case when Participant.vote='notcast' then 1 else 0 end) as numNotCast,
sum(case when Participant.vote='infavor' then 1 else 0 end) as numInFavor
from
Debate
inner join
Participant on Participant.debateId = Debate.debateId
group by
Debate.debateId
Updated after the clarification
There are many limitations in MySQL views, but I don't think this is one of them.
I think it is strange that the name of the view is in single quotes. That might be allowed, but the view may not be doing what you want. Try this:
CREATE OR REPLACE VIEW `Stats` AS
select sum(case when `p`.`vote` = 'notcast' then 1 else 0 end) AS `numNotCast`
from `Debate` `d` join
`Participant` `p`
ON `d`.`debateId` = `p`.`debateId`
group by `d`.`debateId`;
By the way, in MySQL, you can simplify the select to:
select sum(p.vote = 'notcast') as numNotCast