March 23, 2017 at 6:33 am
The following code works fine in SQL 2014/2016 but gives me zero's in the counts (c) on older versions 2005/8. Any clues as what I might need to change?
Thanks
select top 1 t.* from (select 4 as slot,UDCONT4,'Record' as RC,count(CONTNUMB.DONOR_NO) as c from CONTNUMB,SYSFILE where WHICH_CONTACT = 0 and (len(UDCONT4)>0) and 1=(select 1 from SYSFILE where (UDCONT4<>'') and CONTACT_ID=4) group by sysfile.UDCONT4,WHICH_CONTACT
union all
select 4, case when len(UDCONT4)=0 then '[Undefined]' else UDCONT4 end, 'Record',0 as rc from SYSFILE ) as t
union all
select top 1 t.* from (select 4 as slot,UDCONT4,'Contact' as RC,count(CONTNUMB.DONOR_NO) as c from CONTNUMB,SYSFILE where WHICH_CONTACT > 0 and (len(UDCONT4)>0) and 1=(select 1 from SYSFILE where (UDCONT4<>'') and CONTACT_ID=4) group by sysfile.UDCONT4,WHICH_CONTACT
union all
select 4, case when len(UDCONT4)=0 then '[Undefined]' else UDCONT4 end, 'Contact',0 as rc from SYSFILE) as t
March 23, 2017 at 8:22 am
Thanks for the reply
It's a bit tricky to show you the data but the result looks like this on 2005/8 but on 2014/16 the C column has numbersSLOT UDCONT4 RC C
4 Mobile Record 0
4 Mobile Contact 0
Sysfile is a single record system table containing UDCONT4
ContNumb is a table with different types of phone numbers in
I've got a bit further since posting
If I run the pair of sub queries together on their own they produce a result so I think it is something to do with the outer
select top 1 t.* from ( ..... ) t that 2005/8 don't like ?
March 23, 2017 at 8:28 am
andrew 67979 - Thursday, March 23, 2017 6:33 AMThe following code works fine in SQL 2014/2016 but gives me zero's in the counts (c) on older versions 2005/8. Any clues as what I might need to change?
Thanks
select top 1 t.* from (select 4 as slot,UDCONT4,'Record' as RC,count(CONTNUMB.DONOR_NO) as c from CONTNUMB,SYSFILE where WHICH_CONTACT = 0 and (len(UDCONT4)>0) and 1=(select 1 from SYSFILE where (UDCONT4<>'') and CONTACT_ID=4) group by sysfile.UDCONT4,WHICH_CONTACT
union all
select 4, case when len(UDCONT4)=0 then '[Undefined]' else UDCONT4 end, 'Record',0 as rc from SYSFILE ) as t
union all
select top 1 t.* from (select 4 as slot,UDCONT4,'Contact' as RC,count(CONTNUMB.DONOR_NO) as c from CONTNUMB,SYSFILE where WHICH_CONTACT > 0 and (len(UDCONT4)>0) and 1=(select 1 from SYSFILE where (UDCONT4<>'') and CONTACT_ID=4) group by sysfile.UDCONT4,WHICH_CONTACT
union all
select 4, case when len(UDCONT4)=0 then '[Undefined]' else UDCONT4 end, 'Contact',0 as rc from SYSFILE) as t
Have you tried running one of the blocks, such as this:
select 4 as slot,UDCONT4,'Contact' as RC,count(CONTNUMB.DONOR_NO) as c
from CONTNUMB,SYSFILE
where WHICH_CONTACT > 0
and (len(UDCONT4)>0)
and 1=(select 1 from SYSFILE where (UDCONT4<>'') and CONTACT_ID=4)
group by sysfile.UDCONT4,WHICH_CONTACT
Next, investigate CONTNUMB and SYSFILE. Shouldn't take more than a minute or two.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 23, 2017 at 8:41 am
Yes perhaps our posts crossed I can run the whole query this is inside the outer select top 1 t.* from ( ..... )
As soon as it goes in here column c contains zeros
March 23, 2017 at 9:42 am
Just a little OCD when it comes to formatting code:select top 1
t.*
from (select
4 as slot,
UDCONT4,
'Record' as RC,
count(CONTNUMB.DONOR_NO) as c
from
CONTNUMB,SYSFILE
where
WHICH_CONTACT = 0
and (len(UDCONT4)>0)
and 1=(select 1
from
SYSFILE
where
(UDCONT4<>'')
and CONTACT_ID=4)
group by
sysfile.
UDCONT4,
WHICH_CONTACT
union all
select
4,
case when len(UDCONT4)=0 then '[Undefined]'
else UDCONT4
end,
'Record',
0 as rc
from
SYSFILE
) as t
union all
select top 1
t.*
from (select
4 as slot,
UDCONT4,
'Contact' as RC,
count(CONTNUMB.DONOR_NO) as c
from
CONTNUMB,SYSFILE
where
WHICH_CONTACT > 0
and (len(UDCONT4)>0) and 1=(select
1
from
SYSFILE
where
(UDCONT4<>'')
and CONTACT_ID=4
)
group by
sysfile.
UDCONT4,
WHICH_CONTACT
union all
select
4,
case when len(UDCONT4)=0 then '[Undefined]'
else UDCONT4
end,
'Contact',
0 as rc
from
SYSFILE
) as t;
Not a fan of SQL-89 style joins, even if it is a Cartesian product of a one row table, much prefer the use of SQL-92 style joins and using CROSS JOIN where a Cartesian join is being used. I think it makes the code cleaner and easier to understand.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply