I'm trying to combine these two SQL Queries to show the username with the highest count of 1's in the access column that also has an itemID of 2 AND Access of 1.
How would I combine these two statements to produce this result?
Show the account with the highest permissions:
SELECT UserName, COUNT(Access) AS Count
FROM Permissions
GROUP BY UserName, Access
HAVING Access = 1
ORDER BY Count DESC
Result:
Username AccessCount
user1 2
user2 1
user3 2
Show all accounts with Data Manager access:
SELECT UserName FROM Permissions
where itemid = 2 AND Access = 1
Result:
Username
user1
user2
Permissions table sample data:
itemID UserName Access
1 user1 1
2 user1 1
3 user1 1
1 user2 0
2 user2 1
3 user2 0
1 user3 1
2 user3 0
3 user3 1
The result would be user1 as they have access on itemID 2 and the highest count of access.
SQLFiddle demo here
SELECT TOP 1 UserName, COUNT(Access) AS Count
FROM Permissions ps
WHERE Access = 1
AND
userNAme in
(SELECT UserName
FROM Permissions
where itemid = 2
AND Access = 1
)
GROUP BY UserName
ORDER BY Count DESC
SELECT TOP 1 *
FROM
(
SELECT [USERNAME] as un,COUNT(ACCESS) AS COUNT
FROM Permissions
GROUP BY USERNAME,ACCESS
HAVING ACCESS=1
) a
WHERE EXISTS (SELECT [USERNAME] FROM Permissions WHERE [USERNAME]=a.un and itemid=2)
ORDER BY COUNT DESC
Related
I have two tables. I wrote this query but don't show for table result
how write perfect query show table result?
Taccess
id userid groupid accesstype
1 2 1 1
2 4 1 1
Tgroup
grugroupid groupbname userid
1 group-1 1
2 group-2 2
3 group-3 3
4 group-4 4
I need this result for userid=1
I want show all groupid then show accesstype userid=1
grugroupid groupbname accesstype
1 group-1 1
2 group-2 null
3 group-3 null
4 group-4 null
select grugroupid ,
userid ,groupbname ,
(select AccessType from Access where UserID=2 ) as dd,
from TLab_Info
union
select id userid groupid accesstype
from TLab_Access
SELECT g.grugroupid,g.groupbname,a.accesstype
FROM Tgroup g LEFT JOIN
Taccess a on g.grugroupid=a.groupid
WHERE (a.accesstype IS NULL) OR (a.accesstype=1)
GROUP BY g.grugroupid,g.groupbname,a.accesstype
Result:
grugroupid groupbname accesstype
----------------------------------
1 group-1 1
2 group-2 (null)
3 group-3 (null)
4 group-4 (null)
See result in SQL Fiddle.
additional variant to already posted by Raging Bull
SELECT DISTINCT
g.grugroupid,
g.groupbname,
a.accesstype
FROM Tgroup g LEFT JOIN
Taccess a ON g.grugroupid = a.groupid
WHERE COALESCE(a.accesstype,1) = 1
test is here SQL Fiddle
I am trying to retrieve count(*) function with multiple rows. I am using the following query
select distinct agent, count(customer) as total_customer,
(select count(january_1) from salestable where january_1!=0 group by agent) as sales_customer
from salestable
where customer_type = "urban"
group by agent
order by agent asc
At first I have retrieve distinct agent number then count how much customer he have. Note that there are few agent who have no customer. total distinct agent is 2000. But in count it retrieve 1600 as 400 agent has no customer. In another column named january_1 I have sales value. I want to get a table where 3 column will be listed with distinct agent, total no of customer and total no of sales. In january_1 column 0 means no sales. It should look like
| Agent | Customer | Service | Served
| Lynda | 6 | 4 | 0
| Marks | 7 | 5 | 6
| Tomas | 6 | 3 | 2
But in result I am getting the following error
more than one row returned by a subquery used as an expression
What I have to do?
I'm not entirely sure, but think this is what you want
select agent,
count(customer) as total_customer,
count(case when january_1 <> 0 then 1 end) as sales_customer
from salestable
where customer_type = 'urban'
group by agent
order by agent asc;
Alternatively this can be written with a filter() condition:
select agent,
count(customer) as total_customer,
count(*) filter where (january_1 <> 0) as sales_customer
from salestable
where customer_type = 'urban'
group by agent
order by agent asc;
In order to generate a report in ireport i need this query in oracle 10g.
SCHOOL:
SELECT STID,NAME,DEPT,SUM(CHARGE)
STID | PROG | DEPT | CHARGE
1 1 A 1
2 1 B 2
3 2 A 2
4 2 B 1
5 1 A 2
Desired OUTPUT:
DEPT | PROG | NBER_OF_STID | TOT_CHG
A 1 2 3
2 1 2
B 1 1 2
2 1 1
this is my query
SELECT DISTINCT DEPT, DISTINCT PROG, COUNT(STID), SUM (CHARGE) TOT_CHG
FROM SCHOOL
GROUP BY DEPT, PROG, STID, CHARGE
Help Thanks.
You need to group by only the columns that aren't going to be aggregated.
Try this:
SELECT DEPT, PROG, COUNT(STID) NBER_OF_STID, SUM (CHARGE) TOT_CHG
FROM SCHOOL
GROUP BY DEPT, PROG
Note: in your query you'll always get a tabular view, so results will be like this:
DEPT | PROG | NBER_OF_STID | TOT_CHG
A 1 2 3
A 2 1 2
B 1 1 2
B 2 1 1
IMHO, the visual formatting should be made in the report itself (ireport)
Users:
userid name email
1 venkat v#g.com
2 venu ve#g.com
3 raghu r#g.com
Partners:
id userid partnerid status
1 1 2 1
2 1 3 1
location:
id userid lat lon
1 1 12.00 13.00
2 2 14.00 12.00
3 3 14.00 14.23
Query:
var result = from partner in Partners
join user in Users on partner.UserId equals user.PartnerId
join location in Locations on patner.UserId equals location.PartnerId
where partner.UserId == 1
select new { PartnerId = partner.PartnerId, PartnerName = user.Name, Lat = location.Lat, Lon = location.Lon };
by passing userid=1 as parameter I am getting this result:
partnerid patnername lat lon
2 venkat 14.00 12.00
3 venkat 14.00 14.23
by observation of above result here partnernames are wrong for partnerid = 2 - patname was venu but displaying "venkat"
For partnerid = 3, partnername was raghu but displaying venkat.
How to display the correct partner names?
I believe this JOIN here is wrong:
var result = from partner in Partners
join user in Users on user.UserId equals partner.PartnerId
You're joining a user on his userId to a partner using his PartnerID.
Don't you need to join a user to a partner using PartnerID in both cases? Something like this:
var result = from partner in Partners
join user in Users on user.PartnerId equals partner.PartnerId
I would like to query a sql table from below
ID Val
-------------
1 5
1 7
1 8
1 9
2 5
2 7
2 9
3 1
3 5
that would return the following set of results
query > select distinct ID from dbo.table where val in (5,7,9)
result
--------
ID
1
2
I run into a problem where a single row can match only one val from the subset and not all of them...
Assuming the rows are distinct:
SELECT ID
FROM your_table
WHERE Val IN (5,7,9)
GROUP BY ID
HAVING COUNT(*) = 3