wood burning stoves 2.0*
The moose likes JDBC and the fly likes SQL Query for this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL Query for this?" Watch "SQL Query for this?" New topic
Author

SQL Query for this?

Bhavesh Sangwan
Ranch Hand

Joined: Aug 08, 2012
Posts: 57
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

Joined: Mar 01, 2009
Posts: 1610
    
  13

Start by looking at SQL GROUP BY queries.


No more Blub for me, thank you, Vicar.
Patrick Martyn
Greenhorn

Joined: Apr 24, 2013
Posts: 3
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
Bartender

Joined: Jan 17, 2008
Posts: 2272
    
  28

if you are on Oracle you could do this

Bhavesh Sangwan
Ranch Hand

Joined: Aug 08, 2012
Posts: 57
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

Joined: Apr 24, 2013
Posts: 3
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

Joined: Dec 20, 2006
Posts: 2491
    
    8

Welcome to the Ranch, Patrick!


OCUP UML fundamental and ITIL foundation
youtube channel
Patrick Martyn
Greenhorn

Joined: Apr 24, 2013
Posts: 3
Thanks Jan (I think)

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

pm
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SQL Query for this?
 
Similar Threads
Row count for an update statement
Updaing image field in Sql server
Column count not matching the amount of columns in database
SQL - Automatically reflecting the values of 2 columns in another column
SQL query not giving any result