Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Query for this?

 
Bhavesh Sangwan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table named 'user_details' and having 'status' and 'role' columns....



And Inserted some data...

I want to fetch three columns in out put ....

1st to display Status, 2nd to count users all having status like 'A' and 3rd column to count all users having status like 'A' and role like 'admin'

please help me writing this query
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by looking at SQL GROUP BY queries.
 
Patrick Martyn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I understand your requirement but this hould give you a good starting point.


The trick to placing a subquery in the select clause is that the subquery must return a single value.
This is why an aggregate function such as SUM, COUNT, MIN, or MAX is commonly used in the subquery.

Hope this helps,
pm



 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are on Oracle you could do this

 
Bhavesh Sangwan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Patrick partly its working bt still have a strange problem....

When I write the blow query


Its working for status A but for status D its not working...
I mean it is showing same results for status 'D' also ....

like this....

+--------+---------------------+--------------+
| status | USers_with_status_A | User_A_Admin |
+--------+---------------------+--------------+
| A | 3 | 2 |
| D | 3 | 2 |
+--------+---------------------+--------------+

And my actual table entries are....

mysql> select * from user_details;
+----+--------------+----------+--------+--------+
| id | username | password | status | role |
+----+--------------+----------+--------+--------+
| 1 | Bhavesh | bhbh | A | admin |
| 2 | Ravinder | ravi | A | admin |
| 3 | Amit Sangwan | amit | A | normal |
| 4 | Dinesh | dahiya | D | normal |
| 5 | Mukesh | sod | D | admin |
+----+--------------+----------+--------+--------+

What to do now....
 
Patrick Martyn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only supplied the last one to get you going in the right direction. I was not expecting to do everything. However, the following should help;
SELECT status,
(SELECT COUNT(status) as "Users with Status of A"
from B.user_details
WHERE status lIke "A%"
and B.id = A.id),
(SELECT COUNT(status) as "Users with Status of A and Admin"
from C.user_details
WHERE status lIke "A%"
and role like "admin%"
and C.id = A.id)
FROM A.user_details
GROUP BY status
 
Jan Cumps
Bartender
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Patrick!
 
Patrick Martyn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jan (I think)

Oh, if only you were a real bartender! ;-)

pm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic