This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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: 2657
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
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!