Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Complex Single query

Author  Topic 

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-10-06 : 01:47:15
[code]
tbl_master
ref_id
397



tbl_rej_info

ref_id rej_start_date
397 11/04/2004
397 05/05/2004
397 05/03/2004
397 02/05/2004


tbl_ref_status

ref_id status rej_flc_date date_compeleted masterservice_id spkid
397 'NEW' 11/05/2004 NULL 127 900
397 'NEW' 12/05/2005 NULL 220 400
397 'NEW' 06/05/2005 NULL 403 300
397 'NEW' 12/12/2005 NULL 187 300
397 'NEW' 11/05/2004 NULL 127 900
397 'NEW' 01/05/2004 NULL 127 900
397 'NEW' 07/05/2004 NULL 127 400


I need to fetch 4 fields from the 3 tables I can pass the ref_id to the stored procedure But
I should get a single record which satisfy these condition


latest_rej_start_date = The latest rejstart_date from tbl_rej_info table for the spkid=900 from tbl_ref_status table.
latest_date_completed = The latest date_compeleted from tbl_ref_status table for the spkid=300
lates_transaction_date = The latest date_compeleted from tbl_ref_status table for the spkid=1800. If the date_compeleted is NULL then get the
latest rej_flc_date from tbl_ref_status

next_action_date = The oldest rej_flc_date from tbl_ref_status table where date_compeleted IS NULL

How can I combine these conditions to get a single query
[/code]

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-10-06 : 02:49:25
I tried using this query but still its not covering all the cases.

SELECT
ref_id,
MAX(CASE
WHEN tbl_ref_status.spkid=900 THEN tbl_rej_info.rejstart_date
ELSE
NULL
END) AS latest_rej_start_date,
MAX(CASE
WHEN tbl_ref_status.spkid=300 THEN ISNULL(tbl_ref_status.date_compeleted,tbl_ref_status.rej_flc_date)
ELSE
NULL
END) AS latest_date_completed ,
MAX(CASE
WHEN tbl_ref_status.spkid=400 THEN date_compeleted
ELSE
NULL
END) AS lates_transaction_date,
MIN(CASE
WHEN tbl_ref_status.date_compeleted IS NULL THEN rej_flc_date
ELSE
NULL
END) AS next_action_date
FROM
tbl_rej_info
INNER JOIN tbl_ref_status ON
tbl_rej_info.ref_id=tbl_ref_status.ref_id AND
tbl_ref_status.ref_id=397
GROUP BY tbl_ref_status.ref_id
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-10-06 : 04:20:23
Something along these lines:

select 
(select max(rej_Start_date) from tbl_rej_info t2 where t2.ref_id = t1.ref_id and t2.spkid = 900) as latest_rej_start_date,
(select max(date_completed) from tbl_ref_status t3 where t3.ref_id = t1.ref_id and t3.spkid = 300) as latest_date_completed,
(select case when max(IsNull(date_completed, rej_flc_date)) from tbl_ref_status t3 where t3.ref_id = t1.ref_id and t3.spkid = 1800) as latest_transaction_date,
(select min(rej_flc_date) from tbl_ref_status t3 where t3.ref_id = t1.ref_id and t3.date_completed is null) as next_action_date
from tbl_master t1


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-10-06 : 13:02:52
Iam sorry I made a big mistake...
I can pass the master_number to the stored procedure But
I should get a single record which satisfy these condition

tbl_master will have the master_number
master_number ref_id
000465787 397

Again in this query :
select case when max(IsNull(date_completed, rej_flc_date)) from tbl_ref_status t3 where t3.ref_id = t1.ref_id and t3.spkid = 1800) as latest_transaction_date
I would get the result I want because the condition is :

lates_transaction_date = The latest date_compeleted from tbl_ref_status table for the spkid=1800. If the date_compeleted is NULL then get the latest rej_flc_date from tbl_ref_status
There may be multiple records.The records with date_completed should be taken and get the latest and only if all the date_compeleted values are NULL then only we should consider latest rej_flc_date

Now in this query it will compare each row and take the max out of the date_completed and rej_flc_date




Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-10-06 : 13:54:08
The input parameter is only master_number .Please help me with this query
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-10-07 : 02:44:32
Somthing like this

Declare @tbl_master Table
(
master_number int,
ref_id int
)

Insert @tbl_master
Select 000465787,397

Declare @tbl_rej_info Table
(
ref_id int,
rej_start_date Smalldatetime
)

Insert @tbl_rej_info
Select 397,'11/04/2004' Union All
Select 397,'05/05/2004' Union All
Select 397,'05/03/2004' Union All
Select 397,'02/05/2004'

Declare @tbl_ref_status Table
(
ref_id int,
status varchar(10),
rej_flc_date smalldatetime,
date_compeleted smalldatetime,
masterservice_id int,
spkid int
)

Insert @tbl_ref_status
Select 397,'NEW','11/05/2004',NULL,127,900 Union All
Select 397,'NEW','12/05/2005',NULL,220,400 Union All
Select 397,'NEW','06/05/2005',NULL,403,300 Union All
Select 397,'NEW','12/12/2005',NULL,187,300 Union All
Select 397,'NEW','11/05/2004',NULL,127,900 Union All
Select 397,'NEW','01/05/2004',NULL,127,900 Union All
Select 397,'NEW','07/05/2004',NULL,127,400 Union All
Select 397,'NEW','07/05/2004',NULL,127,1800



Select * From
(
Select Max(rej_start_date) As latest_rej_start_date,tbl1.ref_id
From @tbl_ref_status tbl1 Inner Join @tbl_rej_info tbl2 on tbl1.ref_id = tbl2.ref_id
and tbl1.spkid = 900
Group by tbl1.ref_id
) as A
Inner Join
(
Select Max(IsNull(date_compeleted,rej_flc_date)) lates_transaction_date ,ref_id From @tbl_ref_status
Where spkid = 1800
Group by ref_id
) as B
On A.ref_id = b.ref_id
Inner Join
@tbl_master C on a.ref_id= c.ref_id
Where
master_number = 000465787


Chirag
Go to Top of Page
   

- Advertisement -