Hi
I am doing a query against a relatively large table. The basic query is as follows:
select aud.record_id, aud.auditdatetime, seqno,
substring(cast(aud.oldrecord as varchar(max)), (dbo.fnNthIndex(aud.oldrecord,'|',8) + 6), 4),
substring(cast(aud.newrecord as varchar(max)), (dbo.fnNthIndex(aud.newrecord,'|',8) + 6), 4)
from dbC_AUDITTRAIL_USERTABLES aud, dbo.PEOPLE p
where
p.PEOPLE_ID = aud.RECORD_ID
and aud.TABLENAME like 'TABLENAME'
and p.LASTNAME like 'Lastname'
order by aud.auditdatetime desc
This query returns 10 records from the entire table which is around 1 million records. The response is instant. But I only want those records where the 2 last columns are different (the 2 rows in the select that start with substring and are highlighted in red). When I add
AND substring(cast(aud.oldrecord as varchar(max)), (dbo.fnNthIndex(aud.oldrecord,'|',8) + 6), 4) <> substring(cast(aud.newrecord as varchar(max)), (dbo.fnNthIndex(aud.newrecord,'|',8) + 6), 4)
to my WHERE statement it disappears off in to space.
To get around this I tried to add in a nested select as follows:
select aud.record_id, aud.auditdatetime, seqno,
substring(cast(aud.oldrecord as varchar(max)), (dbo.fnNthIndex(aud.oldrecord,'|',8) + 6), 4),
substring(cast(aud.newrecord as varchar(max)), (dbo.fnNthIndex(aud.newrecord,'|',8) + 6), 4)
from dbC_AUDITTRAIL_USERTABLES aud, dbo.PEOPLE p ,
(select record_id,
substring(cast(oldrecord as varchar(max)), (dbo.fnNthIndex(oldrecord,'|',8) + 6), 4),
substring(cast(newrecord as varchar(max)), (dbo.fnNthIndex(newrecord,'|',8) + 6), 4)
from dbC_AUDITTRAIL_USERTABLES group by RECORD_ID ) maxres
where
p.PEOPLE_ID = aud.RECORD_ID
and aud.RECORD_ID = maxres.record_id
and aud.TABLENAME like 'TABLENAME'
and p.LASTNAME like 'Lastname'
order by aud.auditdatetime desc
This gives me the following errors:
Msg 8120, Level 16, State 1, Line 6
Column 'dbC_AUDITTRAIL_USERTABLES.OLDRECORD' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 6
Column 'dbC_AUDITTRAIL_USERTABLES.OLDRECORD' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8155, Level 16, State 2, Line 9
No column name was specified for column 2 of 'maxres'.
Msg 8155, Level 16, State 2, Line 9
No column name was specified for column 3 of 'maxres'.
I never got as far as putting in the line to compare the 2 columns. I know the problem is to do with grouping in the nested select but no matter what I do I can't get it to work - can anyone help please?
Thanks
Anthony
I am doing a query against a relatively large table. The basic query is as follows:
select aud.record_id, aud.auditdatetime, seqno,
substring(cast(aud.oldrecord as varchar(max)), (dbo.fnNthIndex(aud.oldrecord,'|',8) + 6), 4),
substring(cast(aud.newrecord as varchar(max)), (dbo.fnNthIndex(aud.newrecord,'|',8) + 6), 4)
from dbC_AUDITTRAIL_USERTABLES aud, dbo.PEOPLE p
where
p.PEOPLE_ID = aud.RECORD_ID
and aud.TABLENAME like 'TABLENAME'
and p.LASTNAME like 'Lastname'
order by aud.auditdatetime desc
AND substring(cast(aud.oldrecord as varchar(max)), (dbo.fnNthIndex(aud.oldrecord,'|',8) + 6), 4) <> substring(cast(aud.newrecord as varchar(max)), (dbo.fnNthIndex(aud.newrecord,'|',8) + 6), 4)
to my WHERE statement it disappears off in to space.
To get around this I tried to add in a nested select as follows:
select aud.record_id, aud.auditdatetime, seqno,
substring(cast(aud.oldrecord as varchar(max)), (dbo.fnNthIndex(aud.oldrecord,'|',8) + 6), 4),
substring(cast(aud.newrecord as varchar(max)), (dbo.fnNthIndex(aud.newrecord,'|',8) + 6), 4)
from dbC_AUDITTRAIL_USERTABLES aud, dbo.PEOPLE p ,
(select record_id,
substring(cast(oldrecord as varchar(max)), (dbo.fnNthIndex(oldrecord,'|',8) + 6), 4),
substring(cast(newrecord as varchar(max)), (dbo.fnNthIndex(newrecord,'|',8) + 6), 4)
from dbC_AUDITTRAIL_USERTABLES group by RECORD_ID ) maxres
where
p.PEOPLE_ID = aud.RECORD_ID
and aud.RECORD_ID = maxres.record_id
and aud.TABLENAME like 'TABLENAME'
and p.LASTNAME like 'Lastname'
order by aud.auditdatetime desc
This gives me the following errors:
Msg 8120, Level 16, State 1, Line 6
Column 'dbC_AUDITTRAIL_USERTABLES.OLDRECORD' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 6
Column 'dbC_AUDITTRAIL_USERTABLES.OLDRECORD' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8155, Level 16, State 2, Line 9
No column name was specified for column 2 of 'maxres'.
Msg 8155, Level 16, State 2, Line 9
No column name was specified for column 3 of 'maxres'.
I never got as far as putting in the line to compare the 2 columns. I know the problem is to do with grouping in the nested select but no matter what I do I can't get it to work - can anyone help please?
Thanks
Anthony