Latest entry SQL problem - sql

I have two tables:
UserTable contains (UserID, UserName) and StoryTable contains
(StoryID, UserID(foreignkey), StoryName, InsertedDate)
How can I query to get each User Name along with the latest story name that he has posted ? (I m new to queries so kindly excuse if its quite basic)
I tried:
SELECT a.Username, b.StoryName FROM [dbo].[UserTable] as A INNER JOIN
[dbo].[StoryTable] as b ON a.UserID = b.UserID WHERE InsertedDate =
MAX(InsertedDate) group by a.UserName;
but it throws error in sql server 2008.

Change your query to be this:
SELECT a.Username, b.StoryName
FROM [dbo].[UserTable] as A
INNER JOIN [dbo].[StoryTable] as b ON a.UserID = b.UserID
WHERE b.InsertedDate =
(SELECT MAX(InsertedDate) FROM [StoryTable] AS z WHERE z.UserID = A.UserID)
Edited as per comment:
SELECT a.Username, b.StoryName
FROM [dbo].[UserTable] as A
INNER JOIN [dbo].[StoryTable] as b ON a.UserID = b.UserID
WHERE b.StoryID =
(SELECT MAX(z.StoryID) FROM [StoryTable] AS z WHERE z.UserID = A.UserID)

SELECT Top 1 a.Username, b.StoryName
FROM [dbo].[UserTable] as A
INNER JOIN [dbo].[StoryTable] as b ON a.UserID = b.UserID
order by b.InsertedDate desc

MAX is an aggregate function, to filter using an aggregate function, you need to use the HAVING keyword instead of WHERE

You can do like this
SELECT u.Username, s.StoryName
FROM [dbo].[UserTable] AS u
CROSS APPLY (SELECT TOP 1 StoryName
FROM [dbo].[StoryTable] AS ss
WHERE ss.UserID = u.UserID
ORDER BY ss.InsertedDate DESC
) AS s

Related

SQL many to many select people with multiple vacancies

I am working with sql server through SSMS right now. How can i choose all people with multiple(>2)vacancies?
I am trying something like that, but i dont understand how to make part with "more than 2 vacancies"?
SELECT dbo.applicants.FirstName, dbo.vacancy.Name
FROM dbo.applicants INNER JOIN
dbo.VacancyApplicant ON dbo.applicants.id = dbo.VacancyApplicant.ApplicantId INNER JOIN
dbo.vacancy ON dbo.VacancyApplicant.VacancyId = dbo.vacancy.id WHERE dbo.vacancy.Name='third vacancy'
SELECT dbo.applicants.FirstName, dbo.vacancy.Name
FROM dbo.applicants A INNER JOIN
dbo.VacancyApplicant V ON A.id = V.ApplicantId
WHERE EXIST(
SELECT 1
FROM dbo.applicants INNER JOIN
dbo.VacancyApplicant ON dbo.applicants.id =
dbo.VacancyApplicant.ApplicantId INNER JOIN
dbo.vacancy ON dbo.VacancyApplicant.VacancyId = dbo.vacancy.id
WHERE A.id=dbo.applicants.id
GROUP BY dbo.applicants.id,dbo.vacancy.id
HAVING COUNT(1)>2
)
Group By and Having are you basic answer. Below is a simple solution, might not be ideal, but can give you the idea.
I am finding target "applicants" ids in subquery, that uses GROUP BY and HAVING then outer query joins to that to output FirstName and LastName of applicant
SELECT dbo.applicants.FirstName, dbo.applicants.LastName FROM
dbo.applicants a INNER JOIN
(
SELECT dbo.applicants.id
FROM dbo.applicants INNER JOIN
dbo.VacancyApplicant ON dbo.applicants.id = dbo.VacancyApplicant.ApplicantId INNER JOIN
dbo.vacancy ON dbo.VacancyApplicant.VacancyId = dbo.vacancy.id AND dbo.vacancy.Name='third vacancy'
GROUP BY dbo.applications.id
HAVING COUNT(dbo.vacancy.id) > 2
) targetIds ON a.id = targetIds.id
"more than 2 vacancies"?
Your question only mentions vacancies but your query is filtering for a particular name. I assume you really want more than two of that name.
If I understand correctly, you want aggregation:
SELECT a.FirstName, a.Name
FROM dbo.applicants a INNER JOIN
dbo.VacancyApplicant va
ON a.id = va.ApplicantId INNER JOIN
dbo.vacancy v
ON va.VacancyId = v.id
WHERE v.Name = 'third vacancy'
GROUP BY a.FirstName, v.Name
HAVING COUNT(*) > 2;
Note the use of table aliases. They make the query easier to write and to read.
WITH TempCTE AS (
SELECT DISTINCT ap.FirstName
,vc.Name
,COUNT (va.VacancyId) OVER (PARTITION BY ap.id) AS NoOfVacancies
FROM dbo.applicants ap
JOIN dbo.VacancyApplicant va
ON ap.id = va.ApplicantId
JOIN dbo.vacancy vc
ON va.VacancyId = vc.id
)
SELECT FirstName,[Name], NoOfVacancies FROM TempCTE
WHERE NoOfVacancies > 2

How to create distinct count from queries with several tables

I am trying to create one single query that will give me a distinct count for both the ActivityID and the CommentID. My query in MS Access looks like this:
SELECT
tbl_Category.Category, Count(tbl_Activity.ActivityID) AS CountOfActivityID,
Count(tbl_Comments.CommentID) AS CountOfCommentID
FROM tbl_Category LEFT JOIN
(tbl_Activity LEFT JOIN tbl_Comments ON
tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON
tbl_Category.CategoryID = tbl_Activity.CategoryID
WHERE
(((tbl_Activity.UnitID)=5) AND ((tbl_Comments.PeriodID)=1))
GROUP BY
tbl_Category.Category;
I know the answer must somehow include SELECT DISTINCT but am not able to get it to work. Do I need to create multiple subqueries?
This is really painful in MS Access. I think the following does what you want to do:
SELECT ac.Category, ac.num_activities, aco.num_comments
FROM (SELECT ca.category, COUNT(*) as num_activities
FROM (SELECT DISTINCT c.Category, a.ActivityID
FROM (tbl_Category as c INNER JOIN
tbl_Activity as a
ON c.CategoryID = a.CategoryID
) INNER JOIN
tbl_Comments as co
ON a.ActivityID = co.ActivityID
WHERE a.UnitID = 5 AND co.PeriodID = 1
) as caa
GROUP BY ca.category
) as ca LEFT JOIN
(SELECT c.Category, COUNT(*) as num_comments
FROM (SELECT DISTINCT c.Category, co.CommentId
FROM (tbl_Category as c INNER JOIN
tbl_Activity as a
ON c.CategoryID = a.CategoryID
) INNER JOIN
tbl_Comments as co
ON a.ActivityID = co.ActivityID
WHERE a.UnitID = 5 AND co.PeriodID = 1
) as aco
GROUP BY c.Category
) as aco
ON aco.CommentId = ac.CommentId
Note that your LEFT JOINs are superfluous because the WHERE clause turns them into INNER JOINs. This adjusts the logic for that purpose. The filtering is also very tricky, because it uses both tables, requiring that both subqueries have both JOINs.
You can use DISTINCT:
SELECT
tbl_Category.Category, Count(DISTINCT tbl_Activity.ActivityID) AS CountOfActivityID,
Count(DISTINCT tbl_Comments.CommentID) AS CountOfCommentID
FROM tbl_Category LEFT JOIN
(tbl_Activity LEFT JOIN tbl_Comments ON
tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON
tbl_Category.CategoryID = tbl_Activity.CategoryID
WHERE
(((tbl_Activity.UnitID)=5) AND ((tbl_Comments.PeriodID)=1))
GROUP BY
tbl_Category.Category;

Slow query when using NOT EXIST in Query

I would like to seek some help regarding the query below.
Running this Script causes the system to timeout. The query is so slow it took 5 minutes to run for just 22 records. I believe this has something to do with "NOT IN" statement. I already look for answers here in Stackoverflow regarding this and some are suggesting using LEFT OUTER JOIN and WHERE NOT EXIST but I can't seem to incorporate it in this query.
SELECT a.UserId, COUNT(DISTINCT(a.CustomerId)) AS TotalUniqueContact
FROM [UserActivityLog] a WITH(NOLOCK)
WHERE CAST(a.ActivityDatetime AS DATE) BETWEEN '2015-09-28' AND '2015-09-30' AND a.ID
NOT IN (
SELECT DISTINCT(COALESCE(a.activitylogid, 0))
FROM [CustomerNoteInteractions] a WITH(NOLOCK)
WHERE a.reason IN ('20', '36') AND CAST(a.datecreated AS DATE) BETWEEN '2015-09-28' AND '2015-09-30' AND a.UserId IN (SELECT b.Id
FROM [User] b
WHERE b.UserType = 'EpicUser' AND b.IsEpicEmployee = 1 AND b.IsActive = 1)
)
AND a.UserId IN (
SELECT b.Id
FROM [User] b
WHERE b.UserType = 'EpicUser' AND b.IsEpicEmployee = 1 AND b.IsActive = 1)
GROUP BY a.UserId
Here is what should be an equivalent query using EXISTS and NOT EXISTS:
SELECT a.UserId,
COUNT(DISTINCT a.CustomerId) AS TotalUniqueContact
FROM [UserActivityLog] a WITH(NOLOCK)
WHERE CAST(a.ActivityDatetime AS DATE) BETWEEN '2015-09-28' AND '2015-09-30'
AND EXISTS (SELECT *
FROM [User] b
WHERE b.Id = a.UserId
AND b.UserType = 'EpicUser'
AND b.IsEpicEmployee = 1
AND b.IsActive = 1)
AND NOT EXISTS (SELECT *
FROM [CustomerNoteInteractions] b WITH(NOLOCK)
JOIN [User] c
ON c.Id = b.UserId
AND c.UserType = 'EpicUser'
AND c.IsEpicEmployee = 1
AND c.IsActive = 1
WHERE b.activitylogid = a.ID
AND b.reason IN ('20', '36')
AND CAST(b.datecreated AS DATE) BETWEEN '2015-09-28' AND '2015-09-30' )
GROUP BY a.UserId
Obviously, it's hard to understand what will truly help your performance without understanding your data. But here is what I expect:
I think the EXISTS/NOT EXISTS version of the query will help.
I think your conditions on UserActivityLog.ActivityDateTime and CustomerNoteInteractions.datecreated are a problem. Why are you casting? Is it not a date type? If not, why not? You would probably get big gains if you could take advantage of an index on those columns. But with the cast, I don't think you can use an index there. Can you do something about it?
You'll also probably benefit from indexes on User.Id (probably the PK anyways), and CustomerNoteInteractions.ActivityLogId.
Also, not a big fan of using with (nolock) to improve performance (Bad habits : Putting NOLOCK everywhere).
EDIT
If your date columns are of type DateTime as you mention in the comments, and so you are using the CAST to eliminate the time portion, a much better alternative for performance is to not cast, but instead modify the way you filter the column. Doing this will allow you to take advantage of any index on the date column. It could make a very big difference.
The query could then be further improved like this:
SELECT a.UserId,
COUNT(DISTINCT a.CustomerId) AS TotalUniqueContact
FROM [UserActivityLog] a WITH(NOLOCK)
WHERE a.ActivityDatetime >= '2015-09-28'
AND a.ActivityDatetime < dateadd(day, 1, '2015-09-30')
AND EXISTS (SELECT *
FROM [User] b
WHERE b.Id = a.UserId
AND b.UserType = 'EpicUser'
AND b.IsEpicEmployee = 1
AND b.IsActive = 1)
AND NOT EXISTS (SELECT *
FROM [CustomerNoteInteractions] b WITH(NOLOCK)
JOIN [User] c
ON c.Id = b.UserId
AND c.UserType = 'EpicUser'
AND c.IsEpicEmployee = 1
AND c.IsActive = 1
WHERE b.activitylogid = a.ID
AND b.reason IN ('20', '36')
AND b.datecreated >= '2015-09-28'
AND b.datecreated < dateadd(day, 1, '2015-09-30'))
GROUP BY a.UserId
This should get you pretty close or exactly work:
SELECT a.UserId, COUNT(DISTINCT(a.CustomerId)) AS TotalUniqueContact
FROM [UserActivityLog] a WITH(NOLOCK)
inner join [User] b with (Nolock) on a.userid = b.id
and b.UserType = 'EpicUser' AND b.IsEpicEmployee = 1 AND b.IsActive = 1
left outer join [CustomerNoteInteractions] c with (nolock) on a.id = c.activitylogid
and c.reason IN ('20', '36') AND CAST(c.datecreated AS DATE) BETWEEN '2015-09-28' AND '2015-09-30'
left outer join [User] d with (nolock) on c.userid = d.id
and d.UserType = 'EpicUser' AND d.IsEpicEmployee = 1 AND d.IsActive = 1
WHERE CAST(a.ActivityDatetime AS DATE) BETWEEN '2015-09-28' AND '2015-09-30'
and c.activitylogid is null
GROUP BY a.UserId

SQL Server Conditional Join instead of WHERE .. IN .. clauses

SELECT A.Name, H.Name as BookedBy
FROM dbo.vwAllLoads A WITH (NOEXPAND)
LEFT JOIN dbo.SystemInfo H
ON (A.BookedByUserID = H.GlobalNetUserID)
WHERE ((A.CustomerID IN (SELECT UCR.CustomerID
FROM dbo.UserCustomerRelations UCR
WHERE UCR.UserID IN
(SELECT UserID FROM #PodUsers)
OR H.GlobalUserID IN
(SELECT UserID FROM #PodUsers)))
Now I filter data using above where clause. How can I accomplish the same using joins or in a better way?
Please help
Assuming your query is something like:
select a.*
from a
where A.CustomerID IN (SELECT UCR.CustomerID
FROM dbo.UserCustomerRelations UCR
WHERE UCR.UserID IN (SELECT UserID FROM #PodUsers) OR
UCR.GlobalNetUserID IN (SELECT UserID FROM #PodUsers)
-----------------------------^ was `H`, I'm assuming is `UCR`
)
Then the following should be an equivalent query using joins:
select distinct a.*
from a join
dbo.UserCustomerRelations ucr
on A.CustomerID = ucr.CustomerID join
#PodUsers pu
on ucr.UserId = pu.UserId or ucr.UserId = ucr.GlobalNetUserID = pu.UserId;
The distinct would be unnecessary if you know that the joins do not produce multiple rows
I guess something like this:
INNER JOIN [dbo].[UserCustomerRelations] UCR
ON A.[CustomerID] = UCR.[CustomerID]
INNER JOIN #PodUsers PU
ON UCR.[CustomerID] = PD.[UserID]
OR H.[GlobalNetUserID] = PD.[UserID]

Delete statement SQL joins

Can anyone please tell me how to write a delete statement for the following query:
SELECT a.UserID, b.UserEmailAddress
FROM tblUserProgramme AS a LEFT OUTER JOIN
tblUser AS b ON b.UserID = a.UserID LEFT OUTER JOIN
tblWorkGroup AS c ON c.WorkGroupID = b.WorkGroupID
WHERE(a.ProgrammeID = 59) AND (a.UserID NOT IN
(SELECT UserID FROM tblUser AS a WHERE (WorkGroupID IN
(SELECT WorkGroupID FROM tblWorkGroup WHERE
(WorkGroupName LIKE '%Insight%') OR (WorkGroupName LIKE '%Other%')))))
AND (b.UserEmailAddress NOT IN
(SELECT email FROM tmpUsers))
ORDER BY b.UserEmailAddress
SERVER is SQL Server 2005
Query would be
Delete from
(
SELECT a.UserID, b.UserEmailAddress
FROM tblUserProgramme AS a LEFT OUTER JOIN
tblUser AS b ON b.UserID = a.UserID LEFT OUTER JOIN
tblWorkGroup AS c ON c.WorkGroupID = b.WorkGroupID
WHERE(a.ProgrammeID = 59) AND (a.UserID NOT IN
(SELECT UserID FROM tblUser AS a WHERE (WorkGroupID IN
(SELECT WorkGroupID FROM tblWorkGroup WHERE (WorkGroupName LIKE
'%Insight%') OR (WorkGroupName LIKE '%Other%'))))) AND (b.UserEmailAddress NOT IN
(SELECT email FROM tmpUsers))
ORDER BY b.UserEmailAddress
) a
Syntax is DB specific..
For most of tha databases this should work , if you want to delete from tblUserProgramme
DELETE A
FROM tblUserProgramme AS a
.....
In general, in SQL Server, you can delete all records which match a given SQL query as follows, provided your SELECT only returns columns from a single table:
DELETE x
FROM
(
-- Any query which returns data from a single table, that you wish to delete
) x;
Or, using a CTE:
;WITH x as
(
-- Any query which returns data from a single table, that you wish to delete
)
DELETE x;
Thank you all for your quick answers:) There were a quite a few answers right, but had to pick the first one:)
As Joe G Joseph has mentioned,
this is what i did
DELETE a
from dbo.tblUserProgramme a
LEFT OUTER JOIN tblUser b ON b.UserID = a.UserID
LEFT OUTER JOIN tblWorkGroup c ON c.WorkGroupID = b.WorkGroupID
where a.ProgrammeID = 59 AND
a.UserID NOT IN (SELECT UserID FROM tblUser a WHERE a.WorkGroupID IN
(SELECT WorkGroupID FROM tblWorkGroup
WHERE WorkGroupName like '%Insight%' OR WorkGroupName like '%Other%'))
AND b.UserEmailAddress NOT IN(SELECT email FROM tmpUsers)