SQL Spreadsheets: Cherry-Picking Records

SQL Spreadsheets: Cherry-Picking Records

As a general rule, we recommend that SQL scripts be used to mass-select/prune records; however, sometimes that is not possible.  In these cases, we recommend having a member of the law firm complete a Google spreadsheet that clearly indicates what records should be kept and which should be discarded. 

A good spreadsheet will generate SQL for you so that you can easily include the user's selection in your playbook.

Example:  Cherry-Picking Matters

In the following example, a spreadsheet will be used to:

  1. Cherry-pick matters.
  2. Delete any matters not on the list.
  3. Delete any contacts that are not clients of the matters.
  4. Delete any documents that are not associated with matters on the list.

Step 1: Generate the Google Spreadsheet

Run a SQL command similar to the following:

  1. SELECT
  2. '' as 'Keep? (Y)',
  3. V1.Id,
  4. V2.Final_FullName,
  5. V1.Final_ReferenceCode,
  6. V1.Final_Subject,
  7. V1.Final_Description,
  8. V1.Final_Status,
  9. Script =
  10. --IF cell A### = "Y"
  11. '=IF(INDIRECT("A"&ROW()) = "Y"'
  12. --THEN: Generate the script using the ID in cell B###.
  13. + ', "Update __M_Matters SET Final_Status = Original_Status WHERE Id = ''" & INDIRECT("B"&ROW()) & "''"'
  14. --ELSE: Blank script
  15. + ', ""'
  16. + ')'
  17. FROM
  18. __M_Matters V1
  19. LEFT JOIN
  20. __M_Contacts V2 ON V2.Id = V1.Final_ClientContact_Id
  21. ORDER BY
  22. V1.Final_ReferenceCode


And then paste the results in a brand new google spreadsheet.  Share the spreadsheet with the client and have them put a Y in the Keep field for all matters they want to keep.  When they are done, the spreadsheet should look something like this:

Notice how the Script column automatically fills in for any columns marked with Y.


Step 2: Get the Generated Scripts from the Spreadsheet

Once the client has completed the spreadsheet, click on the KEEP column header, then select Data > Create Filter.

 


The spreadsheet will then be filterable.  Click on the filter icon next to KEEP and then remove blanks from the view.

 

You will then be able to easily select and copy the script for the rows the client wants to keep.




Step 3: Incorporate the Generated Script

At this point, you are ready to incorporate the generated scripts into your playbook.  For the scenario described in the example, the entire playbook might look something like this:

  1. --We are going to default all matters to deleted.
  2. UPDATE

  3. __M_Matters
  4. SET
  5. Final_Status = 'Deleted'

  6. --Restore the status of the matters we actually want.

  7. --PASTE OUR GENERATED SCRIPT HERE

  8. UPDATE __M_Matters SET Final_Status = Original_Status WHERE Id = 'WALLP!52125'
  9. UPDATE __M_Matters SET Final_Status = Original_Status WHERE Id = 'WALLP!52118'
  10. UPDATE __M_Matters SET Final_Status = Original_Status WHERE Id = 'WALLP!52111'
  11. UPDATE __M_Matters SET Final_Status = Original_Status WHERE Id = 'WALLP!52104'

  12. --END OF GENERATED SCRIPT

  13. --Delete all contacts where it is not
  14. --the client of a not-deleted matter
  15. UPDATE
  16. __M_Contacts
  17. SET
  18. Final_Status = 'Deleted'
  19. WHERE 1=1
  20. AND Id NOT IN (
  21. SELECT
  22. Final_ClientContact_Id
  23. FROM
  24. __M_Matters
  25. WHERE 1=1
  26. AND Final_Status != 'Deleted'
  27. )

  28. --Set all documents to be deleted
  29. UPDATE
  30. __M_Documents_Digital_Files
  31. SET
  32. Final_Status = 'Deleted'

  33. --Then restore the status of any documents that are associated to not-deleted matters
  34. UPDATE
  35. __M_Documents_Digital_Files
  36. SET
  37. Final_Status = Original_Status
  38. WHERE 1=1
  39. AND Final_Parent_Id IN (
  40. SELECT
  41. Id
  42. FROM
  43. __M_Matters
  44. WHERE 1=1
  45. AND Final_Status != 'Deleted'
  46. )

    • Related Articles

    • SQL Spreadsheets: Cherry Picking Matters

      The following SQL command will generate a spreadsheet that can be used to cherry-pick specific matters. CREATE OR ALTER FUNCTION SafeSingleLine ( @Input NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @Ret NVARCHAR(MAX) = COALESCE(@Input, '') ...
    • SQL Spreadsheets: Cherry-Picking Custom Fields

      The following SQL command will generate a Google Sheet that users can use to cherry-pick custom field definitions. SELECT '' as 'Keep? (Y)', V1.Id, V1.Final_Parent_Type, V1.Final_Kind, V1.Final_Subject, V1.Final_Description, V1.Final_Status, ...
    • SQL Spreadsheets: DocType Mappings

      The following SQL script will generate a Google Spreadsheet that will allow users to override document category names. This is extremely helpful when merging systems. SELECT V1.Id, V1.Final_ReferenceCode as 'Old Code', V1.Final_Subject as 'Old Name', ...
    • SQL Spreadsheets: User Mappings

      The following SQL script will generate a Google Spreadsheet that will allow users to override email addresses. This is extremely helpful when merging old user accounts together. SELECT V1.Id, V1.Final_FullName as 'Old User (FullName)', ...
    • SQL Spreadsheets: Reassigning Practice Areas

      Sometimes users want to reassign practice areas as part of the data restore into a new system. The following process makes it easy. Generate the SQL Spreadsheet Run the following command then copy the results and the headers into a Google Doc. ...