Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL query

Posted on 2013-01-27
6
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1600 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

664 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