bduenges
asked on
SQL Query
I have a query I am using for a report that returns info from two separate tables. Currently the query will return results based on functionname =management summary and new_typeofopportunity=Airc raft Placement and it only returns one row of data per dealname or accountbase.name. I need to change this so that when the data is returned, the data returns only one management summary per dealname based on MSN. In other words, I have an activity report that users enter data to and assign to the deal name. I am creating a report that when run, will group by MSN and only show the last management summary entered for a particular account. So I might have 4 accounts entering management summaries. I only want to see one summary per account. Any help would be appreciated.
ASKER
Actually the requirements on this has changed. Right now It gives me back data when Task_Function=ManagementSu mmary and opp_dealtype<> aircraft placement. I need the data to come back as I only wanted to see deals that are not aircraft placement and task function can be only NULL or Management Summary along with Deal Status as stated in query.
Please check this example to see how you can get the last task with specific criteria from the task table. This sample uses temp tables so it doesn't change anything in your database.
The trick is in the part
I suppose you could adapt this technique to your situation. And other criteria you want to apply to the tasks you'll add at the
--create temp sample tables
set nocount on
create table #opportunity (Opportunityid int, Name varchar(10))
create table #task (TaskID int, Opportunityid int, DateCreated datetime, TaskFunction varchar(100), Description varchar(100))
--fill tables with date
insert into #opportunity values (1,'op1')
insert into #opportunity values (2,'op2')
insert into #opportunity values (3,'op3')
insert into #opportunity values (4,'op4')
insert into #task values (1,1,'20110101','Management Summary','op1 task1')
insert into #task values (2,1,'20110201','Other','op1 task2')
insert into #task values (3,1,'20110301','Management Summary','op1 task3')
insert into #task values (4,2,'20110101','Management Summary','op2 task1')
insert into #task values (5,2,'20110201','Other','op2 task2')
insert into #task values (6,2,'20110301','Other','op2 task3')
insert into #task values (7,3,'20110101','Other','op3 task1')
insert into #task values (8,3,'20110201','Other','op3 task2')
--actual query
select
opp.Opportunityid,
opp.Name,
task.DateCreated,
Task.Description
from
#opportunity opp
left join
(select * from
(select
TaskID,
Opportunityid,
DateCreated,
Description,
ROW_NUMBER() OVER (PARTITION BY Opportunityid ORDER BY DateCreated desc) as row
from
#task
where
TaskFunction = 'Management Summary'
) as TaskWithRow
where
TaskWithRow.row = 1
) Task
on opp.Opportunityid = Task.Opportunityid
--drop temp tables
drop table #opportunity
drop table #task
The trick is in the part
select
TaskID,
Opportunityid,
DateCreated,
Description,
ROW_NUMBER() OVER (PARTITION BY Opportunityid ORDER BY DateCreated desc) as row
from
#task
where
TaskFunction = 'Management Summary'
This adds a rownumber to the rows for each opportunity based on create data. On the next level this rownumber is filtered to get only rownumber 1, which is the one with the latest createdate.I suppose you could adapt this technique to your situation. And other criteria you want to apply to the tasks you'll add at the
TaskFunction = 'Management Summary'
part. Any criteria you want to apply to the opportunities you can add at the end of the query.ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
patelalpesh,
Receive this error when i parse and execute the query:
Msg 156, Level 15, State 1, Line 64
Incorrect syntax near the keyword 'AS'.
Receive this error when i parse and execute the query:
Msg 156, Level 15, State 1, Line 64
Incorrect syntax near the keyword 'AS'.
ASKER
Open in new window