Solved

MS SQL query

Posted on 2013-01-27
6
334 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now