SQL - Removing Duplicate Rows but Keeping Null Rows - sql

Happy New Year SO!
Problem
I'm writing a stored procedure that takes in a list of User Ids and it should return 1 record per user id that contains:
User details from the User table (first name, last name etc)
Latest modified address record from the Address table. If there IS NO Address record, then we need to return NULL in the address fields (Address, Postcode etc)
Country details from the Country table (Region etc)
Now I have the following, which is correctly returning NULL's for no address details (last record in the screen shot) BUT for a User Id that has multiple address records, I'm having multiple records returned and NOT the lastest modified address record:
SELECT
U.Id, U.FirstName, U.Surname, U.Email, U.DateOfBirth,
AD.AddressLine1, AD.AddressLine2, AD.AddressLine3,
AD.PostCode, AD.Nickname, AD.Phone, AD.Modified,
CNT.Name, CNT.Code,
a.MaxDate
FROM
#TableVariable AS List
LEFT JOIN
dbo.Users AS U ON List.Id = U.Id
LEFT JOIN
dbo.Addresses AS AD ON U.Id = AD.User_Id
LEFT JOIN
(SELECT
JA.User_Id, MAX(CONVERT(DATE,JA.Modified,10)) AS MaxDate
FROM dbo.Addresses AS JA
GROUP BY JA.User_Id) A ON (AD.User_Id = A.User_Id AND CONVERT(DATE,AD.Modified,10) = A.MaxDate)
LEFT JOIN
dbo.Countries AS CNT ON AD.Country_Id = CNT.Id
ORDER BY
AD.Modified DESC
Here is the result set after running the above. As you can see I have correctly got my record returned for a User WITHOUT an address (Last record) but I'm getting 3 records for 2108 when I wanted 1, inlcuding the latest modified address (AD.Modified).
I'm using SQL Server 2008.

You can use outer apply and order by to get the latest record, with something like this:
FROM #TableVariable AS List
LEFT JOIN dbo.Users AS U
ON List.Id = U.Id
OUTER APPLY (
SELECT top 1 *
FROM dbo.Addresses AS JA
WHERE U.Id = JA.User_Id
order by Modified DESC
) AD
LEFT JOIN dbo.Countries AS CNT
ON AD.Country_Id = CNT.Id
ORDER BY AD.Modified DESC

Related

Select name from SQL column

I am trying to select the names (along with the rest of the information on the table such as data, time, lesson) of each user that is inside a users table. The names I'm trying to select are the ones that are on the same row as a particular teacherID. However when I run this code I'm getting the same name for all records, when I should be getting two different ones.
SELECT FName, SName, bookedLessons.bookedHour,
bookedLessons.bookedDate, bookedLessons.lesson
FROM users JOIN bookedLessons
WHERE teacherID = 11
AND users.userID = (SELECT userID FROM bookedLessons WHERE teacherID = 11)
Your join needs an ON clause but not the subquery in the WHERE clause:
SELECT u.FName, u.SName, b.bookedHour, b.bookedDate, b.lesson
FROM bookedLessons b INNER JOIN users u
ON u.userID = b.userID
WHERE b.teacherID = 11

Too much Data using DISTINCT MAX

I want to see the last activity each individual handset and the user that used that handset. I have a table UserSessions that stores the last activity of a particular user as well as what handset they used in that activity. There are roughly 40 handsets, yet I always get back way too many records, like 10,000 rows when I only want the last activity of each handset. What am I doing wrong?
SELECT DISTINCT MAX(UserSessions.LastActivity), Handsets.Name,Users.Username
FROM UserSessions
INNER JOIN Handsets on Handsets.HandsetId = UserSessions.HandsetId
INNER JOIN Users on Users.UserId = UserSessions.UserId
WHERE
Handsets.Name in (1000,1001.1002,1003,1004....)
AND Handsets.Deleted = 0
GROUP BY UserSessions.LastActivity, Handsets.Name,Users.Username
I expect to get one record per handset of the users last activity with that handset. What I get is multiple records on all handsets and dates over 10000 rows
You typically GROUP BY the same columns as you SELECT, except those who are arguments to set functions.
This GROUP BY returns no duplicates, so SELECT DISTINCT isn't needed.
SELECT MAX(UserSessions.LastActivity), Handsets.Name, Users.Username
FROM UserSessions
INNER JOIN Handsets on Handsets.HandsetId = UserSessions.HandsetId
INNER JOIN Users on Users.UserId = UserSessions.UserId
WHERE Handsets.Name in (1000,1001.1002,1003,1004....)
AND Handsets.Deleted = 0
GROUP BY Handsets.Name, Users.Username
There is no such thing as DISTINCT MAX. You have SELECT DISTINCT which ensures that all columns referenced in the SELECT are not duplicated (as a group) across multiple rows. And there is MAX() an aggregation function.
As a note: SELECT DISTINCT is almost never appropriate with GROUP BY.
You seem to want:
SELECT *
FROM (SELECT h.Name, u.Username, MAX(us.LastActivity) as last_activity,
RANK() OVER (PARTITION BY h.Name ORDER BY MAX(us.LastActivity) desc) as seqnum
FROM UserSessions us JOIN
Handsets h
ON h.HandsetId = us.HandsetId INNER JOIN
Users u
ON u.UserId = us.UserId
WHERE h.Name in (1000,1001.1002,1003,1004....) AND
h.Deleted = 0
GROUP BY h.Name, u.Username
) h
WHERE seqnum = 1

Return most recent row if column value matches from another column

Lets say I have a column called UserID and a table called Active Users and then one called User History.
I know I can take the most recent user history
select userid, field1, field2
from us_hi
where (userid,timestamp)
IN (select userid, max (timestamp)
from userhistory
group by userid)
order by userid
but how would I do that if I only want to view the active users by using the userid from the active users table?
The subselect get all the users in history table that are also present in active user table, after that you join the sub select with the main query and this should give you the result you're looking for
SELECT USR.userid,field1, field2
FROM userhistory USR
INNER JOIN (SELECT UH.userid, max (UH.timestamp) timestamp
FROM userhistory UH
INNER JOIN ActiveUsers AU ON AU.UserId = UH.UserId
GROUP BY UH.userid) TMP ON TMP.userid = USR.userId AND USR.timestamp = UH.timestamp
ORDER BY USR.userid

i want to modify this SQL statement to return only distinct rows of a column

select
picks.`fbid`,
picks.`time`,
categories.`name` as cname,
options.`name` as oname,
users.`name`
from
picks
left join categories
on (categories.`id` = picks.`cid`)
left join options
on (options.`id` = picks.oid)
left join users
on (users.fbid = picks.`fbid`)
order by
time desc
that query returns a result that like:
my question is.... I would like to modify the query to select only DISTINCT fbid's. (perhaps the first row only sorted by time)
can someone help with this?
select
p2.fbid,
p2.time,
c.`name` as cname,
o.`name` as oname,
u.`name`
from
( select p1.fbid,
min( p1.time ) FirstTimePerID
from picks p1
group by p1.fbid ) as FirstPerID
JOIN Picks p2
on FirstPerID.fbid = p2.fbid
AND FirstPerID.FirstTimePerID = p2.time
LEFT JOIN Categories c
on p2.cid = c.id
LEFT JOIN Options o
on p2.oid = o.id
LEFT JOIN Users u
on p2.fbid = u.fbid
order by
time desc
I don't know why you originally had LEFT JOINs, as it appears that all picks must be associated with a valid category, option and user... I would then remove the left, and change them to INNER joins instead.
The first inner query grabs for each fbid, the FIRST entry time which will result in a single entity for the FBID. From that, it re-joins to the picks table for the same ID and timeslot... then continues for the rest of the category, options, users join criteria of that single entry.
2 options, you could write a group by clause.
Or you could write a nested query joined back to itself to get pertinent info.
Nested aliased table:
SELECT
n.fBids
FROM
MyTable t
INNER JOIN
(SELECT DISTINCT fBids
FROM MyTable) n
ON n.ID = t.ID
Or group by option
SELECT fBId from MyTable
GROUP BY fBID
select picks.`fbid`, picks.`time`, categories.`name` as cname,
options.`name` as oname, users.`name` from picks left join categories
on (categories.`id` = picks.`cid`) left join options on (options.`id` = picks.oid)
left join users on (users.fbid = picks.`fbid`)
order by time desc GROUP BY picks.`fbid`
select
picks.fbid,
MIN(picks.time) as first_time,
MAX(picks.time) as last_time
from
picks
group by
picks.fbid
order by
MIN(picks.time) desc
However, if you want only distinct fbid's you cannot display cname and other columns at the same time.

Distinct mixing rows values?

I'm getting the result with mixed dates values, instead get the last revision for each title i get them mixed.
I'm using MySQL.
The general idea is retireve all rows for each entry, the last revision of each entry.
My current sql query:
SELECT DISTINCT
w.owner_id,
w.date,
w.title,
MAX(w.revision),
u.name AS updater
FROM wiki_pages AS w
JOIN users AS u ON w.owner_id = u.id
GROUP BY title
ORDER BY title ASC
SQL TABLE
Use:
SELECT wp.owner_id,
wp.date,
wp.title,
wp.revision,
u.name AS updater
FROM WIKI_PAGES wp
JOIN USERS u ON u.id = wp.owner_id
JOIN (SELECT t.title,
MAX(t.revision) AS max_rev
FROM WIKI_PAGES t
GROUP BY t.title) x ON x.title = wp.title
AND x.max_rev = wp.revision
In your query, the only thing you can guarantee is the title & the revision value is the highest. The other rows aren't necessarily related, hence the join to a derived table...