I have a query
SELECT UserName,
IF(status=1, 'open', status) status,
IF(status=2, 'closed', status) status,
c_name
FROM ADMIN a
JOIN admin_course_ ad ON a.adminID=ad.fk_user_id
JOIN admin_courses ac ON ac.c_id=ad.fk_c_id
my requirement is i got status=1,2,3,4 from tables so instead of 1 2 3 4 i want to return open closed pending deferso tried
IF(status=1, 'open', status) status,IF(status=2, 'closed', status) status
but i didn't get expected values.Any help would be appreciated.
You can use CASE statement. For example:
SELECT c_name,
UserName,
(CASE status
WHEN 1 THEN 'open'
WHEN 2 THEN 'closed'
WHEN 3 THEN 'pending'
WHEN 4 THEN 'defer'
ELSE "undefined"
END) AS status_title
FROM admin a
JOIN admin_course_ ad ON a.adminID=ad.fk_user_id
JOIN admin_courses ac ON ac.c_id=ad.fk_c_id
I would suggest to create a status types table with the code, name and a description column. Once you have this you will not need any condition on your query, as youll get name by a inner join between tables.
select a.UserName, ..., s.name
from admin a
inner join statuses s on s.statusCode = a.status
join admin_course_ -- rest of your joins and query
...
You can also achieve what you want with a CASE clause of mysql (look this code example took from mysql documentation https://www.w3schools.com/sql/func_mysql_case.asp):
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN "The quantity is greater than 30"
WHEN Quantity = 30 THEN "The quantity is 30"
ELSE "The quantity is something else"
END
FROM OrderDetails;
Hope this helps
Related
I need count how many suppliers have status "CLOSED" or "READY FOR AUDIT", but in the same time I don't want to count those suppliers that have status "NULL".
supplier
status
JUSTRITE MANUFACTURING COMPANY
CLOSED
JW SPEAKER CORPORATION
CLOSED
KLEIN TOOLS INC
NULL
KLEIN TOOLS INC
CLOSED
KLEVER INNOVATIONS
CLOSED
LA-CO INDUSTRIES INC
CLOSED
As we can see in this example, there are 5 different customers, but the results will be "4" because KLEINT contains "NULL" and it shouldn't be counted in.
Let's say table is defined like the following:
Table name: your_table with two fields
supplier status
---------------- ------
Then you can get the result by aggregation function count.
SELECT
supplier,
SUM(IFNULL((SELECT 1 FROM your_table WHERE a. supplier = supplier LIMIT 1), 0)) AS cnt
FROM your_table a
WHERE `status` IN ('CLOSED', 'READY')
GROUP BY supplier
HAVING cnt > 0
;
I have made a fiddle for you with the solution as an example
https://www.db-fiddle.com/f/rW914i1yUA5GBK2d9j6PGB/0
also the code should look like this
SELECT count(*)
FROM test
WHERE STATUS IS NOT NULL
AND STATUS IN (
'CLOSED'
,'READY FOR AUDIT'
)
this code ignores NULL values, (but they have to be empty values, not someone typing NULL in the fields. that makes a diffrence)
You can:
select all suppliers which have "CLOSED" or "READY FOR AUDIT" status
remove from the selection all those that have a "NULL" status
SELECT COUNT(DISTINCT supplier)
FROM tab
WHERE status IN ('CLOSED', 'READY FOR AUDIT')
AND supplier NOT IN (SELECT supplier FROM tab WHERE status IS NULL)
Check the demo here.
WITH CTE(COMPANY,STATUSS) AS
(
SELECT 'JUSTRITE MANUFACTURING COMPANY', 'CLOSED' UNION ALL
SELECT'JW SPEAKER CORPORATION', 'CLOSED' UNION ALL
SELECT'KLEIN TOOLS INC' , NULL UNION ALL
SELECT'KLEIN TOOLS INC' , 'CLOSED' UNION ALL
SELECT'KLEVER INNOVATIONS', 'CLOSED' UNION ALL
SELECT'LA-CO INDUSTRIES INC' , 'CLOSED'
)
SELECT COUNT(C.COMPANY)
FROM CTE AS C
WHERE C.STATUSS IN('CLOSED','READY FOR AUDIT')
AND NOT EXISTS
(
SELECT 1 FROM CTE AS X WHERE C.COMPANY=X.COMPANY AND X.STATUSS IS NULL
)
CTE is an example of your data. Please replace it with your table name
I have a table, and I want to get the DISTINCT count of usernames over a certain period of time. Currently I'm running this query
SELECT DISTINCT username FROM user_activity WHERE company_id = 9 AND timestamp BETWEEN '2015-09-00' AND '2015-10-01' AND action = "Login Success";
It works great, however, I have multiple Companies that I want to select the count for. How do I expand the previous query to show me the distinct counts for multiple companies?
select count(distinct username),
sum(case when company_id = 1 then 1 else 0 end) A,
sum(case when company_id = 9 then 1 else 0 end) B
from `user_activity` Where timestamp BETWEEN '2015-09-00' AND '2015-10-01' AND action = "Login Success"
I've done something like this, however, I'm not getting the correct numbers. Ideally I would like to list each count as a different value for ease of reading, like the previous query illustrates. I don't need the count(distinct username) column to appear in my result, just the conditionals.
Thanks in advance.
If you don't mind two rows instead of two columns:
SELECT company_id, COUNT(DISTINCT username)
FROM user_activity
WHERE company_id IN (1,9)
AND timestamp >= '2015-09-01'
AND timestamp < '2015-09-01' + INTERVAL 1 MONTH
AND action = "Login Success"
GROUP BY company_id
I'm curious about how to write the correct query when I wanna use count function with having clause..
So I have a table named : seat
and here are the columns:
SEAT
ID_SEAT ID_TIME STATUS
-----------------------------------------
A1 | 1 | Available
A2 | 2 | Available
A3 | 1 | Available
A4 | 1 | Reserved
And so on.
I have 119 rows which the status is 'Available' and 1 row which the status is 'Reserved'.
And here is the query in MySQL:
SELECT status, COUNT(status) AS number FROM seat
WHERE ID_SEAT = 'A3' AND ID_TIME = '1'
HAVING COUNT(status) IN (SELECT COUNT(status) from SEAT where status = 'Available')
But the query didn't show anything..Any help and clear answer would be very helpful ..thank you :)
EDIT :
What I really want to display is something like this :
STATUS NUMBER
---------------------
Available 119
The STATUS field above is for a certain ID_SEAT...in my case the ID_SEAT = 'A3' and for ID_TIME = 1
EDITTTTT :
thank you for all the answer to me..
Now I can fix it clearly..
So I'm using "UNION" to separate the query..I'm using 2 queries to make it works..
here the code :
SELECT status from seat where ID_SEAT = 'A3' AND ID_TIME = '1' UNION
SELECT COUNT(status) from seat
where status = 'Available'
however the code above is work for mysql query.. thank you a lot :)
if you want your result like the one you showed.
SELECT status, COUNT(status) as number
FROM seat
WHERE status = 'Available'
GROUP BY status
If you wanted to see status of seat then simply select status, no need for count
SELECT status
FROM seat
WHERE ID_SEAT = 'A3' AND ID_TIME = '1'
If you must, return status of seat and a count of ALL available seats
SELECT status,(SELECT count(*) FROM seat WHERE status = 'Available') as number
FROM seat
WHERE ID_SEAT = 'A3' AND ID_TIME = '1'
Any time you use an aggregate function like count, sum, avg etc... in the select part of the query, as well as a non aggregate function--"status" in this case--you MUST have a "group by" clause.
So it would look like this:
SELECT status, COUNT(status), AS number FROM seat
HAVING COUNT(status) IN (SELECT COUNT(status) FROM SEAT WHERE status = 'Available')
GROUP BY status
This will make it so you see two rows. One for available with a count of 119, and one for reserved with a count of 1.
In your query, the expression (SELECT COUNT(status) from SEAT where status = 'Available') is going to return 119. However, you are only selecting one row, so the two values can never be equal.
I think you want:
SELECT status, COUNT(status) AS number
FROM seat
GROUP BY status;
This will return all status. If you really only want 'Available', then filter using a where:
SELECT status, COUNT(status) AS number
FROM seat
WHERE status = 'Available';
EDIT: (Based on clarification in comment)
Oh, if you only want the status information for seat number 23, you can get that with a having clause:
SELECT status, COUNT(status) AS number
FROM seat
GROUP BY status
HAVING sum(id_seat = '23A') > 0;
The having clause is counting up the number of rows, for each status, where the id_seat has a value of 23. You want the status where there is at least one row, which is what the having clause does.
I have two tables marks and exams.
In the marks table I have studentid, mark1, mark2 and examid-foreign key from exams for different exams.
I want to get distinct student id and their number of failures in one single query.
The condition for failure is mark1+mark2 <50 or mark1<30. For e.g. If a student having studentid 1 has 15 entries(15 exams) in marks table and the same student failed in 6 so I want to get result as '1' and '6' in two columns and similarly for all students. For this case I wrote query using 'case' and is given below
select
distinct t1.studentid,
(#arrear:=
case
when (t1.mark1+t1.mark2) <50 OR t1.mark1 < 30
then #arrear+1 else #arrear
end) as failures
from marks t1, exams t2,
(select #arrear := 0) r
where t1.examid = t2.examid group by t1.studentid;
But the above query failed to give correct result. How can I modify the query to get correct result?
Try this. You don't need to use variables to help you.
select
m.studentid,
sum(case when m.mark1 + m.mark2 < 50 or m.mark1 < 30 then 1 else 0 end) as failures
from
marks m inner join exams e
on
m.examid = e.examid
group by
m.studentid
The case statement works out if the result is a failure or not and returns 1 for fail, 0 for no fail. Summing the result of this (grouped by studentid) gives you the number of fails per studentid
Oh and the join makes a more efficient join between your two tables :)
You don't need variable #arrear. You can get your info using only query
Try this:
select
distinct t1.studentid,
sum(
case
when (t1.mark1+t1.mark2) <50 OR t1.mark1 < 30
then 1
else 0
end
) as failures
from marks t1, exams t2
where t1.examid = t2.examid group by t1.studentid;
I have a mysql query like the following.
select new,processing,close
from
(select count(id) new from tickets where id_client in (_a_list_of_client_id) and status = new),
(select count(id) processing from tickets where id_client in (_a_list_of_client_id) and status = processing),
(select count(id) close from tickets where id_client in (_a_list_of_client_id) and status = close)
The following is not the exact query but a pseudo query
here _a_list_of_client_id is another query like following
select id_client from client where id_user = {some_id_given_as_parameter}
I just wondering is this the right approach to use same subquery multiple times in a query. Or is there any other way to do things like this.
Thanks in advance
M H Rasel
You can use sum with case and move the subquery to the where criteria:
select
sum(case when status = 'new' then 1 else 0 end) new,
sum(case when status = 'processing' then 1 else 0 end) processing,
sum(case when status = 'close' then 1 else 0 end) close
from tickets
where id_client in (_a_list_of_client_id)
There are a couple other ways to do this (using if for example or leaving out the case), but I think this is easy to read. I believe mysql will work with sum(status='new') for example.