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
 General SQL Server Forums
 New to SQL Server Programming
 Is this possible

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-09-13 : 14:09:39
I want to join these two queries but last table doesn't have the same amount of fields in it. Can I fluff it with something so it will have the same amount and still run the query?



Select Doc, clm,
isnull(count([Clm]),0) as pendinpsc,
App_rcpdt,
Dib_mvt_seq_num,
Lorec4,
Org_ID1,
juris_mvt_typ,
Location
FROM(
Select ROW_NUMBER() OVER(Partition BY d.DOC, d.CLM,
d.App_RCPDT,
MAX(DIB_MVT_SEQ_NUM),
LOREC4,
d.ORG_ID1
ORDER BY Juris_mvt_typ) RowNum,
Doc, d.CLM,
App_rcpdt,
MAX(Dib_mvt_seq_num) Dib_mvt_seq_num,
Lorec4,
Org_ID1,
juris_mvt_typ,

CASE WHEN j.ORG_ID not like 'P%'
THEN CASE WHEN
d.org_id1 like 'S%'
or d.org_id1 like 'R%'
or d.org_id1 like 'P%'
THEN 'DDS'
ELSE 'FO' END
ELSE j.ORG_ID END Location
from (SELECT CLM, Max(DIB_MVT_SEQ_NUM) DIB
FROM T2dibPendnc d2
GROUP BY CLM) as d1
JOIN T2dibPendnc d ON
d1.CLM = d.CLM
AND d1.DIB = d.DIB_MVT_SEQ_NUM
JOIN T2PendJurisnc j ON
d.CLM = j.CLM
GROUP BY DOC,
d.CLM,
d.App_rcpdt,
Lorec4,
d.Org_ID1,
j.org_id,
juris_mvt_typ
) as A
WHERE RowNum = 1 and juris_mvt_typ='r' and location like 'p%'
GROUP BY A.Doc, a.clm, a.App_rcpdt, a.Dib_mvt_seq_num,
a.Lorec4, a.Org_ID1, a.juris_mvt_typ, a.Location

union

select Juris_mvt_seq_num, clm, juris_mvt_typ, org_id
from
(
select Juris_mvt_seq_num, clm, juris_mvt_typ, org_id,
[row] = row_number() over (partition by [clm]
order by Juris_mvt_seq_num desc) from t2pendjuris
)a
where row=1 and juris_mvt_typ='r' and org_id like 'p%' and order by clm





visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-13 : 14:21:55
put columns in same order as that coming from first select and put NULL for intermediate missing columns

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-09-13 : 14:36:05
Thanks that worked!!!!
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-09-13 : 14:51:22
One last question...

How can I added a table to the bottom union statement. I want to add the T2dibpendnc to the T2pendjuris table they can join on clm fields.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-13 : 15:06:46

just add a join inside the select containing the table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-09-13 : 20:24:06
Cool thanks will try it in the morning!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-13 : 23:20:34
let us know how you got on!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -