I'm drawing a huge blank on something I'm sure I've done before, and is probably simple.
Alas, I'm going to ask for assistance anyways.
I have 2 tables:
tbl_admins_groups
tbl_admins
I'm pulling a query on the just the admin group table:
SELECT groupid, groupname, groupdesc FROM tbl_admins_groups
And now I'd like to add another column that counts the number of admin records associated to that groupid.
Here's my attempt:
SELECT g.groupid, g.groupname, g.groupdesc, COUNT(a.adminid) AS `admincount`
FROM `tbl_admins_groups` g, `tbl_admins` a
WHERE g.groupid = a.groupid
AND a.adminstatus = 1
GROUP BY g.groupid
For some reason I'm getting just one result back.
Is my GROUP BY incorrect?
You can get their COUNT inside subquery and join it with tbl_admins_groups
SELECT g.groupid, g.groupname, g.groupdesc,
a.totalCount
FROM tbl_admins_groups g
LEFT JOIN
(
SELECT groupid, COUNT(*) totalCount
FROM tbl_admins
WHERE adminstatus = 1
GROUP BY groupid
) a
ON g.groupid = a.groupid
Related
How to i get the top vendor for each country? I have this code and it shows the connection between the two tables, now I have to get the largest gmv per country.
Here is my working code:
SELECT DISTINCT a.country_name, b.vendor_name,
SUM(a.gmv_local) as total_gmw
from `my-project-67287.order1.order2`a
join `my-project-67287.vendor1.vendor2` b on a.vendor_id = b.id
group by a.country_name, b.vendor_name;
The top 3 should show this:
Assuming you can save that SELECT into a table called vendors, you need to use it as the subquery in the FROM clause.
You could use this:
SELECT vendors.country_name, vendors.vendor_name, MAX(vendors.total_gmw)
FROM
(
SELECT DISTINCT a.country_name, b.vendor_name,
SUM(a.gmv_local) as total_gmw
from `my-project-67287.order1.order2`a
join `my-project-67287.vendor1.vendor2` b on a.vendor_id = b.id
group by a.country_name, b.vendor_name
) AS vendors
GROUP BY vendors.country_name;
I must mention I have not tested your query, since I do not have your tables, so I assumed it's correct.
I only created the vendors table with the required fields and values from your picture. This should print:
SELECT odr.c_name,vdr.v_name,odr.gmv
FROM(
SELECT *
FROM(
SELECT c_name,v_id,gmv
FROM `order`
ORDER BY gmv DESC
)
GROUP BY c_name
)AS odr
LEFT JOIN vender AS vdr ON vdr.id = odr.v_id
GROUP BY odr.c_name
c_name short for country_name
I have four table like the picture bellow
I want to count how many student that have status 'v' where in table submission have submission type '1' and group by student_id so in the last i can get table like this
I have try sql query like this
select p.id, (SELECT count(*) FROM (select b.id from student as a , submission as b WHERE a.id = b.student_id and b.id_submission_type =1 and a.status_n='v' and a.id_academic_programe = p.id GROUP BY b.student_id) ) from academic_programe as p
But give me error
1054 - Unknown column 'p.id' in 'where clause'
Any suggestion? sory for my english
Correlations cannot be in nested subqueries. Fortunately, this is easy to fix:
select p.id,
(select count(*)
from student st join
submission su
on st.id = su.student_id and
su.id_submission_type = 1 and
st.status_n = 'v' and
where st.id_academic_programe = p.id
)
from academic_programe p;
Try this out:
select c.academic_program_name,count(a.distinct student_name) as count
from
(select * from student where status = 'v') a
inner join
(select * from submission id_submission_type=1) b
on a.id =b.student_id
inner join
academic_program_name c
on a.id_academic_programe = c.id
group by c.academic_program_name;
Let me know in case of any queries.
Please try the following...
SELECT student.id,
student_name,
academic_program_name AS Programe,
COUNT( status_n ) AS status_n_count
FROM student
JOIN Submission ON student.id = Submission.student_id
RIGHT JOIN academic_program ON student.id_academic_programe = academic_program.id
WHERE id_submission_type = 1
AND status_n = 'v'
GROUP BY student.id,
student_name,
academic_program_name;
This statement begins by joining the student and Submission so as to get a table containing the student's id, student_name, status_n and id_submission_type fields. This is then RIGHT JOINed to form a table where each academic program is listed along with each student's details, and that programs with no students are still listed.
The resulting dataset is refined as per your criteria with the WHERE clause, GROUPed and SELECTed
If you have any questions or comments then please feel free to post a Comment accordingly.
i have table with student_uid,grade,test_name as columns i want to count how many got each grade..for this
SELECT a.grade,COUNT(a.grade) AS count1
FROM 2015_2016_x_english_grades AS a
where test_name='ut1_marks'
GROUP BY grade
for single table worked how to do it for more than one table
my query:
SELECT a.grade, COUNT(a.grade),b.grade,COUNT(b.grade)
FROM 2015_2016_x_english_grades a
INNER JOIN 2015_2016_x_hindi_grades b ON a.grade=b.grade
WHERE a.test_name = b.ut1_marks='ut1_marks'
GROUP BY a.grade,b.grade
what is wrong in this?
i also tried this
SELECT a.grade,COUNT(a.grade),(SELECT COUNT(b.grade)FROM 2015_2016_x_biology_grades b where b.test_name='ut1_marks' GROUP BY b.grade)as count1 FROM 2015_2016_x_biology_grades a where test_name='ut1_marks' GROUP BY a.grade
it says [Err] 1242 - Subquery returns more than 1 row
Do the counting in subqueries, and join the subqueries.
SELECT e.grade, english_count, hindi_count
FROM (SELECT grade, COUNT(*) AS english_count
FROM 2015_2016_x_english_grades
WHERE test_name = 'ut1_marks'
GROUP BY grade) AS e
JOIN (SELECT grade, COUNT(*) as hindi_count
FROM 2015_2016_x_hindi_grades
WHERE test_name = 'ut1_marks'
GROUP BY grade) AS h
ON e.grade = h.grade
Or if there's a unique key in each table, you can do:
SELECT e.grade, COUNT(DISTINCT e.id) AS english_count, COUNT(DISTINCT h.id) AS hindi_count
FROM 2015_2016_x_english_grades AS e
JOIN 2015_2016_x_hindi_grades AS h ON e.grade = h.grade AND e.test_name = h.test_name
WHERE e.test_name = 'ut1_marks'
GROUP BY e.grade
Note that both of these queries will only show a grade if it exists in both tables. To get grades that only exist in one table, you need a FULL OUTER JOIN, but MySQL doesn't have this operation. See
Full Outer Join in MySQL
for how to emulate them.
It works but what I would like is to list username, first name, last name not all but I've tried JOIN and isn't seeming to work. Any ideas? Thanks!
MY DB:
http://gyazo.com/eb13cd68440d20719ce0783018cb9828
SELECT
M.Username,
M.first_name,
M.Last_name,
COUNT(1) AS num_comments
FROM members AS M
INNER JOIN comments AS C
ON C.memberID = M.memberID
GROUP BY
C.memberID
ORDER BY COUNT(1) DESC
LIMIT 1
This matches the Member to all their comments, groups by the member to get the count of comments for the users, orders by the count starting highest first, then returns the first result.
Instead of selecting * (ALL) just use SELECT table.username, table.firstname, table.lastname [...].
You can leave out the table. if all information is stored in your comments table. If not, adjust accordingly. In that case you'll also need to Join the comments table with the table where the rest of the information is stored.
Edit:
SELECT m.username, m.first_name, m.last_name FROM members m, comments c WHERE m.MemberID = c.MemberID AND c.author = (select max(author) from comments)
I have the following query which gives me the right results. But it's super slow.
What makes it slow is the
AND a.id IN (SELECT id FROM st_address GROUP BY element_id)
part. The query should show from which countries we get how many orders.
A person can have multiple addresses, but in this case, we only only want one.
Cause otherwise it will count the order multiple times. Maybe there is a better way to achieve this? A distinct join on the person or something?
SELECT cou.title_en, COUNT(co.id), SUM(co.price) AS amount
FROM customer_order co
JOIN st_person p ON (co.person_id = p.id)
JOIN st_address a ON (co.person_id = a.element_id AND a.element_type_id = 1)
JOIN st_country cou ON (a.country_id = cou.id)
WHERE order_status_id != 7 AND a.id IN (SELECT id FROM st_address GROUP BY element_id)
GROUP BY cou.id
Have you tried to replace the IN with an EXISTS?
AND EXISTS (SELECT 1 FROM st_address b WHERE a.id = b.id)
The EXISTS part should stop the subquery as soon as the first row matching the condition is found. I have read conflicting comments on if this is actually happening though so you might throw a limit 1 in there to see if you get any gain.
I found a faster solution. The trick is a join with a sub query:
JOIN (SELECT element_id, country_id, id FROM st_address WHERE element_type_id = 1 GROUP BY
This is the complete query:
SELECT cou.title_en, COUNT(o.id), SUM(o.price) AS amount
FROM customer_order o
JOIN (SELECT element_id, country_id, id FROM st_address WHERE element_type_id = 1 GROUP BY element_id) AS a ON (o.person_id = a.element_id)
JOIN st_country cou ON (a.country_id = cou.id)
WHERE o.order_status_id != 7
GROUP BY cou.id