Can't get my head around this...
I have 3 tables like this:
Computers
---------
Id
Name
ComputerLogins
--------------
Computer_Id
User_Id
NumberOfLogins
Users
-----
Id
Name
Computers "have and belong to many" Users "through" ComputerLogins.
Sample data:
Computers: Id Name
1 "Alpha"
2 "Beta"
3 "Gamma"
Users: Id Name
1 "Joe"
2 "Fred"
ComputerLogins: Computer_Id User_Id NumberOfLogins
1 1 5
1 2 12
2 1 10
2 2 6
3 1 2
3 2 4
I'm trying to construct a view that will output one row for each record in Computers, and join a Users row through MAX(NumberOfLogins) in ComputerLogins.
Desired output:
Computer_Id User_Id NumberOfLogins
1 2 12
2 1 10
3 2 4
Can you suggest a view query that will produce the desired output?
Thanks!
SELECT
CL.*, U.* --change this as needed
FROM
(
SELECT
Computer_ID, MAX(NumberOfLogins) AS NumberOfLogins
FROM
ComputerLogins
GROUP BY
Computer_ID
) maxC
JOIN
ComputerLogins CL On maxC.Computer_ID = CL.Computer_ID AND maxC.NumberOfLogins = CL.NumberOfLogins
JOIN
Users U On CL.User_ID = U.ID
Wrap in a view etc
Use:
CREATE VIEW your_view AS
SELECT c.id AS computer_id,
u.id AS user_id,
COUNT(*) AS NumberOfLogins
FROM COMPUTERS c
JOIN COMPUTERLOGINS cl ON cl.computer_id = c.id
JOIN USERS u ON u.id = cl.user_id
GROUP BY c.id, u.id
Related
I have two tables in Postgres
user
ID
name
group_a_id
group_b_id
1
user_one
1
2
2
user_two
3
4
group
ID
name
1
group_one
2
group_two
3
group_three
4
group_four
I am looking for a joined result but for 2 rows from the group table
Results
ID
name
group_a_id
group_a_name
group_b_id
group_b_name
1
user_one
1
group_one
2
group_two
2
user_two
3
group_three
2
group_four
So in one join query how to get the result ?
SELECT * from user,group WHERE user.group_a_id = group.id like this how to join two rows from group to one with alias names group_a_name, group_b_name
You want join . . . twice:
select u.*, g1.name, g2.name
from users u left join
groups g1
on g1.id = u.group_a_id left join
groups g2
on g2.id = u.group_b_id;
I have database where 2 roles can't be associated with each other, and I need to display any users who have conflicting roles.
For example: an (id 2) accountant can't also be a (id 5) trainer
this has to be done without using CTE's
Table a Table b table c
--------------- ------------------- ------------
userID | roleID roleID | conflictID roleID | Role Name
1 2 2 5 1 chef
1 3 2 accountant
1 5 3 driver
2 3 4 barmaid
2 1 5 trainer
3 2
3 3
the result should contain only the userID who has both roles 2 and 5
userID
------
1
Join the b table with the a table twice, to get userID's with conflicting combinations:
select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
and a1.userID = a2.userID
I am trying to write a query using group by in sub query ,I referred lot of blogs but could not get all the values.
I have three tables and below is the structure of those tables.
Pet_Seller_Master
ps_id ps_name city_id
2 abc 1
3 xyz 2
4 fer 4
5 bbb 1
City_Master
city_id city_name
1 Bangalore
2 COIMBATORE
4 MYSORE
Api_Entry
api_id ps_id otp
1 2 yes
2 3
3 2 yes
4 3 yes
5 4
6 5 yes
7 5 yes
8 5 yes
Query is to get number of sellers, no of pet sellers with zero otp, no of pet sellers with 1 otp, no of pet sellers with 2 otp,no of pet sellers with otp>2 for the particular city and within date range.
Through Below query I am able to get city , psp , and zero otp
select cm.city_name,
count(ps.ps_id) as PSP,
((select count(ps1.ps_id)
FROM ps_master ps1
WHERE ps1.city = cm.city_id)-
(SELECT count(distinct ps1.ps_id)
from ps_master ps1
INNER JOIN api_entry ae ON ps1.ps_id = ae.ps_id and otp!=''
WHERE ps1.city = cm.city_id and date(timestamp) >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND date(timestamp) < curdate())) as zero_psp
from ps_master ps INNER JOIN city_master cm ON ps.city = cm.city_id and cm.city_type = 'IN HOUSE PNS'
group by city_id
Please tell me the solution to solve this query.
Thanks in advance
It's not hard to do and you were on a right track. Here is what I would use:
select c.city_name, a.otp, p.ps_name, COUNT(*) nbr
from Api_Entry a
inner join Pet_Seller_Master p on p.ps_id=a.ps_id
inner join City_Master c on p.city_id=c.city_id
group by c.city_name, a.otp, p.ps_name
Now, if you want to get the number of sellers with zero otp, you just apply where clause:
where otp <> 'yes'
If you want to get the number of pet sellers with otp>2, then you just use subquery:
select *
from (
select c.city_name, a.otp, p.ps_name, COUNT(*) nbr
from #tempA a
inner join #tempP p on p.ps_id=a.ps_id
inner join #tempC c on p.city_id=c.city_id
group by c.city_name, a.otp, p.ps_name
) g
where nbr > 2
Could I get some help creating this query please?
Users book product video slots to advertise ONE product video. The booking table captures these bookings. Booking spots are allocated to a user not to a product or video.
The #1-ranked video for the user's #1-ranked product will be the one that is advertised.
Bookings table
id user_id position
---- ----- --------
1 100 1
2 150 2
Users table
Users can have 1-n bookings
id name
---- ----
100 John Smith
150 Herby Brown
Products table
A user can have 1-n products
id name user_id rank
--- ---- ------- ----
1 bike 100 1
2 stereo 100 2
3 computer 100 3
4 notebook 150 1
5 scooter 150 2
Videos table
A product can have 1-n videos
id name product_id user_id rank
--- ---- ----------- ------- ----
1 bike video1 1 100 1
2 bike video2 1 100 2
3 computer video 3 100 3
4 notebook video 4 150 1
5 scooter video 5 150 2
So, the query reads, in words:
For each booking record, get the #1 ranked video for the #1-ranked product for the booked user. Order the results by booking position.
SELECT v.id,
v.name,
v.rank,
v.user_id,
u.name AS uname
FROM videos v
JOIN users u
ON u.id = v.user_id
JOIN bookings b
ON u.id = b.user_id
WHERE v.product_id = (SELECT id
FROM products
WHERE user_id = b.user_id
ORDER BY rank ASC
LIMIT 1)
ORDER BY b.position ASC, v.rank ASC
some simplified query:
SELECT v.id,
v.name,
v.user_id,
u.name AS uname,
p.name AS pname
FROM videos v
JOIN users u
ON u.id = v.user_id
JOIN bookings b
ON u.id = b.user_id
JOIN products p
ON p.id = v.product_id
WHERE v.product_id = (SELECT id
FROM products
WHERE user_id = b.user_id
AND rank = 1)
AND v.rank = 1
ORDER BY b.position ASC
I have 3 tables:
users:
Id Login
1 John
2 Bill
3 Jim
computers:
Id Name
1 Computer1
2 Computer2
3 Computer3
4 Computer4
5 Computer5
sessions:
UserId ComputerId Minutes
1 2 47
2 1 32
1 4 15
2 5 5
1 2 7
1 1 40
2 5 31
I would like to display this resulting table:
Login Total_sess Total_min Most_freq_computer Sess_on_most_freq Min_on_most_freq
John 4 109 Computer2 2 54
Bill 3 68 Computer5 2 36
Jim - - - - -
Myself I can only cover first 3 columns with:
SELECT Login, COUNT(sessions.UserId), SUM(Minutes) FROM users
LEFT JOIN sessions
ON users.Id = sessions.UserId GROUP BY users.Id
And some kind of other columns with:
SELECT main.*
FROM (SELECT UserId, ComputerId, COUNT(*) AS cnt ,SUM(Minutes)
FROM sessions
GROUP BY UserId, ComputerId) AS main
INNER JOIN (
SELECT ComputerId, MAX(cnt) AS maxCnt FROM (
SELECT ComputerId, UserId, COUNT(*) AS cnt FROM sessions GROUP BY ComputerId, UserId
)
AS Counts GROUP BY ComputerId)
AS maxes
ON main.ComputerId = maxes.ComputerId
AND main.cnt = maxes.maxCnt
But I need to get whole resulting table in one query. I feel I'm doing something completely wrong. Need help.
Here you are:
SELECT u.login, t1.total_sess, t1.total_min, t2.mf, t2.sess_mf, t2.min_mf
FROM users u
LEFT JOIN (
SELECT userid, COUNT(minutes) AS total_sess, SUM(minutes) AS total_min
FROM sessions
GROUP BY userid
) AS t1 ON t1.userid = u.id
LEFT JOIN (
SELECT userid, name AS mf, COUNT(*) AS sess_mf, SUM(minutes) AS min_mf
FROM sessions s
JOIN computers c ON c.id = s.computerid
GROUP BY userid, computerid
HAVING COUNT(computerid) >= ALL(SELECT COUNT(*)
FROM sessions s2
WHERE s2.userid = s.userid
GROUP BY s2.computerid)
) AS t2 ON t2.userid = u.id
I'm using MySQL syntax, but it should be pretty portable.
If you need anything more, feel free to ask!
EDIT: I updated the query, the previous one was wrong :(