## Dealing with Incomplete Data - A T-SQL Puzzle 1

 Author Message Suresh Kumar Maganti Ten Centuries Group: General Forum Members Points: 1193 Visits: 125 Comments posted to this topic are about the item Dealing with Incomplete Data - A T-SQL Puzzle 1 Kindest Regards,M Suresh Kumar ChrisM@Work SSC Guru Group: General Forum Members Points: 151168 Visits: 21305 What a really nicely written article to start the day off with, very enjoyable.Suresh, have you tried using CROSS APPLY to partition the rows into "records" as an alternative to calculating the upper and lower bound? You can then sequence the rows within each "record", then by joining the result to itself (staggered by one row), identification of missing data is quite simple:`;WITH OrderedData AS ( SELECT Seq_No, cdata, x.RowID, rn = ROW_NUMBER() OVER(PARTITION BY x.RowID ORDER BY p.Seq_No) FROM #Test_Table p CROSS APPLY ( SELECT RowID = MAX(Seq_No) FROM #Test_Table WHERE CData = '*********************************************' AND Seq_No <= p.Seq_No ) x ) SELECT t.* FROM OrderedData t WHERE t.RowID IN ( SELECT r1.RowID FROM OrderedData r1 INNER JOIN OrderedData r2 ON r2.RowID = r1.RowID AND r2.rn = r1.rn + 1 WHERE r1.CData = 'RateQuote' AND r2.CData = ' ')`I wonder how this would perform against the original?CheersChrisM “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor 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 ModenExploring Recursive CTEs by Example Dwain Camps aspiring_dba SSC Veteran Group: General Forum Members Points: 200 Visits: 68 Dear Sir, Very good use of CTE. I learn to know the real strength of CTE after reading this article. The best thing about CTE is that it keeps the readability of the code. Its very easy to understand the approach of a developer in the solutions based on CTE feature. and also the real life Scenario u have given here helps me to understand the kind of problems and real world situation that can arise before DBA. I am new in this forum and I want to be good DBA. Now i know that i am going to learn a lot here.Thanks... aspiring_dba SSC Veteran Group: General Forum Members Points: 200 Visits: 68 Dear Sir, This is really very good alternative solution. Can't I delete the selected bad rows in the same statement like this....`WITH OrderedData AS ( SELECT Seq_No, cdata, x.RowID, rn = ROW_NUMBER() OVER(PARTITION BY x.RowID ORDER BY p.Seq_No) FROM #Test_Table p CROSS APPLY ( SELECT RowID = MAX(Seq_No) FROM #Test_Table WHERE CData = '*********************************************' AND Seq_No <= p.Seq_No ) x ) Delete from t output deleted.Seq_No, deleted.CData into dbo.Test_Table_Bad_Records FROM OrderedData t WHERE t.RowID IN ( SELECT r1.RowID FROM OrderedData r1 INNER JOIN OrderedData r2 ON r2.RowID = r1.RowID AND r2.rn = r1.rn + 1 WHERE r1.CData = 'RateQuote' AND r2.CData = ' ')`Thanks.. Suresh Kumar Maganti Ten Centuries Group: General Forum Members Points: 1193 Visits: 125 Hi Chris, Thanks for the nice feedback. I appreciate and like the solution you have provided. Your code is shorter too for which I like it even more. Just to see from a performance perspective, I tried executing both the code snippets in the same batch. The Query cost (relative to the batch) was 17% for the one in the article. The same for the alternative version was 83%. Notwithstanding the minor performance difference since the data volume in the code is very limited, I still like your code for its brevity. ------Thanks and Best Regards, Suresh. Kindest Regards,M Suresh Kumar Jeff Moden SSC Guru Group: General Forum Members Points: 781305 Visits: 45880 Suresh Kumar Maganti (11/22/2011)The Query cost (relative to the batch) was 17% for the one in the article. The same for the alternative version was 83%. The query cost relative to the batch actually doesn't mean much. It's quite easy to write queries that doe the same thing where one will be 100% of the batch and the other will be 0% and yet, when you run them, the 0% query take take several seconds where the 100% query runs so fast it almost isn't measureable. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs floresg2 SSC-Enthusiastic Group: General Forum Members Points: 170 Visits: 619 Jeff Moden (11/22/2011)Suresh Kumar Maganti (11/22/2011)The Query cost (relative to the batch) was 17% for the one in the article. The same for the alternative version was 83%. The query cost relative to the batch actually doesn't mean much. It's quite easy to write queries that doe the same thing where one will be 100% of the batch and the other will be 0% and yet, when you run them, the 0% query take take several seconds where the 100% query runs so fast it almost isn't measureable.I'm relatively new to SQL tuning. I haven't heard of that happening. Can you explain how or why that might happen? Thank you.

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         Quest Software         ApexSQL         SQL Sentry      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books         Discuss Data Warehousing Books  Notification Services      Administration Article Discussions Future Versions      SQL 12