• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

SQL Query for this?

 
Ranch Hand
Posts: 62
  • 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
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by looking at SQL GROUP BY queries.
 
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



 
Rancher
Posts: 2759
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: 62
  • 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
 
Bartender
Posts: 2658
19
Netbeans IDE C++ Linux
  • 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
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic