Solved

MS SQL query

Posted on 2013-01-27
6
339 Views
Last Modified: 2013-02-06
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
0
Comment
Question by:rexwithers
6 Comments
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 38825549
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
 
LVL 1

Author Comment

by:rexwithers
ID: 38825633
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
 
LVL 16

Accepted Solution

by:
kmslogic earned 400 total points
ID: 38825642
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 38825953
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
 
LVL 18

Expert Comment

by:deighton
ID: 38826057
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
 
LVL 1

Author Closing Comment

by:rexwithers
ID: 38862030
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question