| Not able improve the performace of the query used in SSRS report Posted: 10 Apr 2013 11:20 PM PDT I have used the below query in SSRS report but it is not at all rndring data.Can you plese help/guide me to improve the prformance of the query:-- Snapshot ValidationWITH Snapshot_CTE( flk_leadid , flk_leadsnapshotid , flk_fiscalperiodstart , flk_fiscalperiodend , flk_subject , flk_name , flk_companyname , flk_campaignid , flk_reportingproductfamilyid , flk_countryid , flk_stateid , flk_statuscode , flk_pricenettotal , flk_salesteamterritoryleadid , flk_territoryid , flk_leadownerterritoryid , ownerid , flk_createdon , flk_closeddate , snapshotcount , leadcount)As( select fleadsnapshot.flk_leadid , fleadsnapshot.flk_leadsnapshotid , fleadsnapshot.flk_fiscalperiodstart , fleadsnapshot.flk_fiscalperiodend , fleadsnapshot.flk_subject , fleadsnapshot.flk_name , fleadsnapshot.flk_companyname , fleadsnapshot.flk_campaignid , fleadsnapshot.flk_reportingproductfamilyid , fleadsnapshot.flk_countryid , fleadsnapshot.flk_stateid , fleadsnapshot.flk_statuscode , fleadsnapshot.flk_pricenettotal , fleadsnapshot.flk_salesteamterritoryleadid , fleadsnapshot.flk_territoryid , fleadsnapshot.flk_leadownerterritoryid , fleadsnapshot.ownerid , fleadsnapshot.flk_createdon , fleadsnapshot.flk_closeddate , SnapshotWithMaxCreatedOn.snapshotcount , 1 as leadcount from Filteredflk_leadsnapshot fleadsnapshot inner join ( select fleadsnapshot.flk_leadid , max(fleadsnapshot.createdon) as maxcreatedon , COUNT(fleadsnapshot.flk_leadid) as snapshotcount from Filteredflk_leadsnapshot fleadsnapshot where fleadsnapshot.flk_leadid is not null group by fleadsnapshot.flk_leadid ) as SnapshotWithMaxCreatedOn on SnapshotWithMaxCreatedOn.flk_leadid = fleadsnapshot.flk_leadid and SnapshotWithMaxCreatedOn.maxcreatedon = fleadsnapshot.createdon)SELECT SnapshotValidation.* FROM(-- No Snapshotselect 'No Snapshot' as [Error Type] , 1 as [Error Sequence] , LeadWithoutSnapshot.leadcount as [Lead Count] , flead.leadid as [Lead ID] , flead.[subject] as [Lead Name] , flead.createdon as [Lead Create Date] , flead.modifiedon as [Lead Modified Date] , flead.flk_closeddate as [Lead Close Date] , 0 as [Snapshot Count] , NULL as [Snapshot ID] , NULL as [Snapshot Name] , NULL as [Snapshot Fiscal Start] , NULL as [Snapshot Fiscal End]from FilteredLead flead inner join ( select COUNT(1) as leadcount, flead.leadid from FilteredLead flead where flead.leadid not in ( select distinct flead.leadid from FilteredLead flead inner join Filteredflk_leadsnapshot fsnapshot on fsnapshot.flk_leadid = flead.leadid ) group by flead.leadid ) as LeadWithoutSnapshot on LeadWithoutSnapshot.leadid = flead.leadid UNION ALL-- Fiscal Period Mismatchselect 'Fiscal Period Mismatch' as [Error Type] , 2 as [Error Sequence] , fsnapshot.leadcount as [Lead Count] , flead.leadid as [Lead ID] , flead.[subject] as [Lead Name] , flead.createdon as [Lead Create Date] , flead.modifiedon as [Lead Modified Date] , flead.flk_closeddate as [Lead Close Date] , fsnapshot.snapshotcount as [Snapshot Count] , fleadsnapshot.flk_leadsnapshotid as [Snapshot ID] , fleadsnapshot.flk_subject as [Snapshot Name] , fleadsnapshot.flk_fiscalperiodstart as [Snapshot Fiscal Start] , fleadsnapshot.flk_fiscalperiodend as [Snapshot Fiscal End]from FilteredLead flead inner join Snapshot_CTE fsnapshot on fsnapshot.flk_leadid = flead.leadid inner join Filteredflk_leadsnapshot fleadsnapshot on fleadsnapshot.flk_leadid = flead.leadidwhere -- lead close date is null but snapshot fiscal end date has value (Lead E, G) (flead.flk_closeddate is null and fsnapshot.flk_fiscalperiodend is not null) or -- snapshot fiscal start date is not correct saturday depending on lead created on (Lead F, M, N) (fsnapshot.snapshotcount = 1 and (convert(date, fsnapshot.flk_fiscalperiodstart) <> convert(date, dateadd(dd, -datepart(dw, flead.createdon), flead.createdon)))) or -- lead close date is not null but snapshot fiscal end date has no value (Lead K) (flead.flk_closeddate is not null and fsnapshot.flk_fiscalperiodend is null) or -- snapshot fiscal end date is not correct friday depending on lead closed on (Lead M, O) (convert(date, fsnapshot.flk_fiscalperiodend) <> convert(date, dateadd(dd, 6 - datepart(dw, flead.flk_closeddate), flead.flk_closeddate))) UNION ALL-- Attribute Mismatchselect 'Attribute Mismatch' as [Error Type] , 3 as [Error Sequence] , fsnapshot.leadcount as [Lead Count] , flead.leadid as [Lead ID] , flead.[subject] as [Lead Name] , flead.createdon as [Lead Create Date] , flead.modifiedon as [Lead Modified Date] , flead.flk_closeddate as [Lead Close Date] , fsnapshot.snapshotcount as [Snapshot Count] , fleadsnapshot.flk_leadsnapshotid as [Snapshot ID] , fleadsnapshot.flk_subject as [Snapshot Name] , fleadsnapshot.flk_fiscalperiodstart as [Snapshot Fiscal Start] , fleadsnapshot.flk_fiscalperiodend as [Snapshot Fiscal End]from FilteredLead flead inner join Snapshot_CTE fsnapshot on fsnapshot.flk_leadid = flead.leadid inner join Filteredflk_leadsnapshot fleadsnapshot on fleadsnapshot.flk_leadid = flead.leadidwhere (fsnapshot.flk_subject <> flead.[subject]) or (fsnapshot.flk_name<>flead.fullname) or (fsnapshot.flk_companyname <>flead.companyname) or (fsnapshot.flk_campaignid <>flead.campaignid) or (fsnapshot.flk_reportingproductfamilyid <>flead.flk_reportingproductfamilyid) or (fsnapshot.flk_countryid <>flead.flk_countryid) or (fsnapshot.flk_stateid <> flead.flk_stateid) or (fsnapshot.flk_statuscode <> flead.statecode) or (fsnapshot.flk_pricenettotal <>flead.flk_pricenettotal) or (fsnapshot.flk_salesteamterritoryleadid <>flead.flk_salesteamterritoryleadid) or (fsnapshot.flk_territoryid <> flead.flk_territoryid) or (fsnapshot.flk_leadownerterritoryid <> flead.flk_leadownerterritoryid) or (fsnapshot.ownerid <> flead.ownerid) or (fsnapshot.flk_createdon <>flead.createdon) or (fsnapshot.flk_closeddate <> flead.flk_closeddate) UNION ALLselect 'Multiple Snapshots' as [Error Type] , 4 as [Error Sequence] , 1 as [Lead Count] , flead.leadid as [Lead ID] , flead.[subject] as [Lead Name] , flead.createdon as [Lead Create Date] , flead.modifiedon as [Lead Modified Date] , flead.flk_closeddate as [Lead Close Date] , flsnapshot.snapshotcount as [Snapshot Count] , fleadsnapshot.flk_leadsnapshotid as [Snapshot ID] , fleadsnapshot.flk_subject as [Snapshot Name] , fleadsnapshot.flk_fiscalperiodstart as [Snapshot Fiscal Start] , fleadsnapshot.flk_fiscalperiodend as [Snapshot Fiscal End] from FilteredLead flead inner join ( select fsnapshot.flk_leadid, COUNT(1) as snapshotcount from ( select fsnapshotouter.flk_leadid , ( select COUNT(1) from Filteredflk_leadsnapshot fsnapshotinner where fsnapshotinner.flk_leadid = fsnapshotouter.flk_leadid and convert(date, fsnapshotinner.flk_fiscalperiodstart) = convert(date, fsnapshotouter.flk_fiscalperiodstart) ) as SnapshotCount from Filteredflk_leadsnapshot fsnapshotouter ) as fsnapshot where fsnapshot.SnapshotCount > 1 group by fsnapshot.flk_leadid ) as flsnapshot on flsnapshot.flk_leadid = flead.leadid inner join Filteredflk_leadsnapshot fleadsnapshot on fleadsnapshot.flk_leadid = flead.leadid UNION ALLselect 'Incomplete Snapshot' as [Error Type] , 5 as [Error Sequence] , 0 as [Lead Count] , flead.leadid as [Lead ID] , flead.[subject] as [Lead Name] , flead.createdon as [Lead Create Date] , flead.modifiedon as [Lead Modified Date] , flead.flk_closeddate as [Lead Close Date] , 0 as [Snapshot Count] , fleadsnapshot.flk_leadsnapshotid as [Snapshot ID] , fleadsnapshot.flk_subject as [Snapshot Name] , fleadsnapshot.flk_fiscalperiodstart as [Snapshot Fiscal Start] , fleadsnapshot.flk_fiscalperiodend as [Snapshot Fiscal End]from( select fleadinner.leadid, COUNT(fleadinner.leadid) as snapshotcount from Filteredflk_leadsnapshot fsnapshotinner inner join FilteredLead fleadinner on fleadinner.leadid = fsnapshotinner.flk_leadid where fsnapshotinner.flk_leadid = fleadinner.leadid and ( (fleadinner.flk_closeddate is null and fsnapshotinner.flk_fiscalperiodend is null) or (fleadinner.flk_closeddate is not null and fsnapshotinner.flk_fiscalperiodend is null) ) group by fleadinner.leadid having COUNT(fleadinner.leadid) > 1) as fleadouterinner joinFilteredLead flead on flead.leadid = fleadouter.leadidinner joinFilteredflk_leadsnapshot fleadsnapshot on fleadsnapshot.flk_leadid = fleadouter.leadid) as SnapshotValidationorder by SnapshotValidation.[Error Sequence], SnapshotValidation.[Lead Name], SnapshotValidation.[Snapshot Fiscal Start] |
| How to Calculate a column value, based on previous column value Posted: 10 Apr 2013 06:18 AM PDT Hello All, Could you please help me here with this sql query, drop table #tempo1 create table #tempo1 (ActNo int, PlanName varchar(20), ProcessMonth varchar(8)) insert into #tempo1 values (100,'Keeper','2012-Jul') insert into #tempo1 values (101,'Keeper','2012-Jul') insert into #tempo1 values (102,'Valuer','2012-Jul') insert into #tempo1 values (103,'Keeper','2012-Jul') insert into #tempo1 values (104,'Valuer','2012-Jul') insert into #tempo1 values (105,'Keeper','2012-Jul') insert into #tempo1 values (110,'Keeper','2012-Jul') insert into #tempo1 values (100,'Keeper','2012-Oct') insert into #tempo1 values (101,'Valuer','2012-Oct') insert into #tempo1 values (102,'Valuer','2012-Oct') insert into #tempo1 values (103,'Keeper','2012-Oct') insert into #tempo1 values (104,'Keeper','2012-Oct') insert into #tempo1 values (105,'Valuer','2012-Oct') insert into #tempo1 values (106,'Valuer','2012-Oct') output will show below threecolumns, first two is straight , but the fourth column shoud be calculate based on second column (if secondvalue is same as fourth then just add literal 'Cnt 2' otherwie 'Cnt 1' so that i.e., if secondcolumn value is 'Keeper' and 4th column value is also 'Keeper' then show as 'Keeper Cnt2' sameway with Valuer as well, if it is different in july and oct then just oct column value with 'Cnt 1' literal ), for exmple AcctNo, JulyPtcpt, OctPtcpt 100, 'Keeper', 'Keeper Cnt 2' ---> (Both July and Oct same so added Cnt 2 at the end ) 101, 'Keeper','Valuer Cnt 1' ---> (Both July and Oct not same, so added Cnt 1 at the end ) 102, 'Valuer','Valuer Cnt 2' 103, 'Keeper','Keeper Cnt 2' 104, 'Valuer','Keeper Cnt 1' 105, 'Keeper','Valuer Cnt 1' 106, '' ,'Valuer Cnt 1' ---> because Not exist in prior month just leave blank or just leave with Cnt1 110, 'Keeper','' --> not exist in oct so leave blank or Just leave it as Cnt1 please help me here, i am stuck at the processmonth separation for jul/oct Kindly assist me here, milan |