Solved

MS SQL query

Posted on 2013-01-27
6
332 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:Deepak Lakkad
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

771 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

10 Experts available now in Live!

Get 1:1 Help Now