MS SQL query

SQL is pretty new for me coming from MS access how do I do this?

I have 2 related tables

Table1 [Jobs]

ID
JobTitle
ClientID
JobOpenDate
JobCloseDate



Table 2 [ActionsOnJobs]
ID
JobID
ActionDate
ActionTitle
ActionDetail

What i'm trying to get is a list of all the jobs with the most recent Action title that is LIKE '%phone%' and the date of that phone call.

I assume I use a group by with MAX to find the most recent one and LIKE '%phone%' but, I cant seam to get a single record from Table 2 that represents gives me the answer, below. What I would like to see is below (note there are many phone calls for job ID 10 but 26/1/13 is the most recent)

Job  JobTitle     ClientID    JobOpenDate       JobCloseDate    ActionTitle       ActionDate
10   Big Job      5500       1/1/2012             NULL              Phone call       26/01/13

Thanks in advance
LVL 1
rexwithersAsked:
Who is Participating?
 
kmslogicCommented:
Might something like this work?

SELECT 
	dbo.CaseInfo.CaseNumber, 
	dbo.CaseInfo.DateOfReferral, 
	dbo.Document.Title, 
	dbo.Document.DateCreated,
	dbo.Document.id 
FROM
	dbo.CaseInfo
	INNER JOIN dbo.Document ON  dbo.CaseInfo.CaseNumber = dbo.Document.CaseNumber
WHERE
	dbo.Document.ID IN 
	(
		SELECT TOP 1 ID FROM dbo.Document
		WHERE dbo.Document.Title LIKE '%Phone%'
			AND dbo.Document.CaseNumber=dbo.CaseInfo.CaseNumber
		ORDER BY dbo.Document.DateCreated DESC
	)

Open in new window

0
 
Deepak LakkadCommented:
Hi,

Try following query,

SELECT 
	Jobs.JobID, 
	Jobs.JobTitle, 
	Jobs.ClientID, 
	Jobs.JobOpenDate, 
	Jobs.JobCloseDate, 
	ActionsOnJob.ActionTitle, 
	ActionsOnJob.ActionDate
FROM
	Jobs
	INNER JOIN ActionsOnJob ON Jobs.JobID = ActionsOnJob.JobID
WHERE
	ActionsOnJob.Id IN 
	(
		SELECT MAX(Id) FROM  ActionsOnJobs 
		GROUP BY JobID
		HAVING ActionTitle LIKE '%Phone%'
	)

Open in new window


- Deepak Lakkad
0
 
rexwithersAuthor Commented:
Firstly, thanks a lot for helping, secondly I should have given the actual field and table names. I have translated and got this.

SELECT 
	dbo.CaseInfo.CaseNumber, 
	dbo.CaseInfo.DateOfReferral, 
	dbo.Document.Title, 
	dbo.Document.DateCreated,
	dbo.Document.id 
FROM
	dbo.CaseInfo
	INNER JOIN dbo.Document ON  dbo.CaseInfo.CaseNumber = dbo.Document.CaseNumber
WHERE
	dbo.Document.ID IN 
	(
		SELECT MAX(ID) FROM  dbo.Document 
		GROUP BY  dbo.Document.CaseNumber
		HAVING dbo.Document.Title LIKE '%Phone%'
	)

Open in new window


But I ahve a few issue I see what you ahve done with the MAX being on the Document  ID but I got an error and a search brought this up for MAX:

 Limitations - uniqueidentifier can not be used
MAX() and MIN() can not be applied on columns of type uniqueidentifier. The error returned if you try to apply the functions on a uniqueidentifier column is:

Msg 8117, Level 16, State 1, Line 1
Operand data type uniqueidentifier is invalid for max operator.

So I tried used the date instead and tried to break it down into two smaller chunks the first was this:

			SELECT ID,  MAX (DateCreated), dbo.document.CaseNumber
	from dbo.Document
	GROUP BY  dbo.Document.CaseNumber
	HAVING dbo.Document.Title LIKE '%Phone%'

Open in new window


As the bit after the IN (in a new query) but I get the error, Msg 8121, Level 16, State 1, Line 4
Column 'dbo.Document.Title' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

Hmmm, when I tried this on my own I continually got errors about aggregate function as soon as I try to limit the "group by" query to include only '%Phone%', do I need to sort of nest these somehow?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Deepak LakkadCommented:
Hi,

In my query, I have placing "Having" by mistake. It should be "Where" clause.

"Having" clause will compare only those which having Aggregated Functions or It is used with "Group By" clause.

In your query replace "Having" with "Where" and put it before "Group By".

To get better solution, please provide proper information.

- Deepak Lakkad
0
 
deightonprogCommented:
since you want the top row of each group, I believe you can use row_number partitioned by CaseNumber and ordered by date in descending order. then select row 1 each time.  ROWN is my generated row number here

SELECT DQ.* FROM 
(SELECT 
	o.CaseInfo.CaseNumber, 
	dbo.CaseInfo.DateOfReferral, 
	dbo.Document.Title, 
	dbo.Document.DateCreated,
	dbo.Document.id ,
	row_number() OVER(partition by dbo.CaseInfo.CaseNumber ORDER BY dbo.Document.DateCreated DESC) ROWN
FROM
	dbo.CaseInfo
	INNER JOIN dbo.Document ON  dbo.CaseInfo.CaseNumber = dbo.Document.CaseNumber
WHERE dbo.Document.Title LIKE '%Phone%') DQ
WHERE DQ.ROWN= 1

                                            

Open in new window

0
 
rexwithersAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.