需要基于一个值复制行。

huangapple go评论93阅读模式
英文:

Need to Replicate Rows based on a value

问题

使用现有的SQL语句,如果[Action Taken]的值等于"See Previous Actions",我需要复制行。

"See Previous Actions" 意味着参考此案例中案件参与者接收的所有操作。基本上,每个[ViolationCategory]中的 "See Previous Actions" 应填充为此案例和参与者的其他类别相同的操作。

在我的示例中有两个参与者,如您所看到的 [CaseParticipantID],其中一个填写了 [Action Taken],而另一个没有。

我假设...数据可以使用案件编号和案件参与者ID与自身联接,但我不知道如何执行。

来自SQL语句的数据(视图仅限于易于阅读的相关列]:

案件编号 案件参与者ID 违规类别 执行操作
123 456 利益冲突 报酬
123 456 利益冲突 谴责
123 456 利益冲突 其他
123 789 利益冲突 口头谴责
123 456 偏袒 查看以前的操作

期望结果

案件编号 案件参与者ID 违规类别 执行操作
123 456 利益冲突 报酬
123 456 利益冲突 谴责
123 456 利益冲突 其他
123 789 利益冲突 口头谴责
123 456 偏袒 报酬
123 456 偏袒 谴责
123 456 偏袒 其他

我当前的SQL语句,筛选为只有一个案件的情况如下:

  1. SELECT
  2. Cases.CaseNumber
  3. ,Cases.DateOpened
  4. ,Cases.DateClosed
  5. ,Participant.CaseParticipantId
  6. ,Participant.RoleInCase
  7. ,Participant.RelToOrganization
  8. ,Participant.Practice
  9. ,Participant.City
  10. ,Issues.Issue
  11. ,Issues.IssueSubCategory
  12. ,Issues.CaseIssueId
  13. ,Issues.Outcome
  14. ,ParticipantAlias.ParticipantName
  15. ,CaseAction.ActionTaken
  16. FROM [Warehouse].[Table].[Case] as Cases
  17. LEFT JOIN [Warehouse].[Table].[CaseParticipant] as Participant
  18. ON Cases.CaseNumber = Participant.CaseNumber
  19. LEFT JOIN [Warehouse].[Table].[CaseIssue] as Issues
  20. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  21. LEFT JOIN [Warehouse].[Table].[CaseAction] as CaseAction
  22. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  23. LEFT JOIN [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  24. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  25. WHERE [Cases].[CaseNumber] = '123'
英文:

Using an existing SQL statement, I need to duplicate rows if value in [Action Taken] equals "See Previous Actions".

“See Previous Actions” means refer back to all actions Case Participant received for this case.
Basically, each [ViolationCategory] with "See Previous Actions" should be filled in with the same actions as other Categories for this case and participant.

I have two participants in my example as you see by the [CaseParticipantID] one that has [Action Taken] filled in and the other one does not.

I assume...data can be unioned back to itself using Case Number and Case Participant ID, but I do not know how to do that.

Data from SQL Statement (View limited to only pertinent columns for easier reading]

Case Number CaseParticipantID ViolationCategory Action Taken
123 456 Conflict of Interest Compensation
123 456 Conflict of Interest Reprimand
123 456 Conflict of Interest Other
123 789 Conflict of Interest Verbal Reprimand
123 456 Favoritism See Previous Action

Desired Outcome

Case Number CaseParticipantID ViolationCategory Action Taken
123 456 Conflict of Interest Compensation
123 456 Conflict of Interest Reprimand
123 456 Conflict of Interest Other
123 789 Conflict of Interest Verbal Reprimand
123 456 Favoritism Compensation
123 456 Favoritism Reprimand
123 456 Favoritism Other

My current SQL Statement, filtered down to just one case is:

  1. SELECT
  2. Cases.CaseNumber
  3. ,Cases.DateOpened
  4. ,Cases.DateClosed
  5. ,Participant.CaseParticipantId
  6. ,Participant.RoleInCase
  7. ,Participant.RelToOrganization
  8. ,Participant.Practice
  9. ,Participant.City
  10. ,Issues.Issue
  11. ,Issues.IssueSubCategory
  12. ,Issues.CaseIssueId
  13. ,Issues.Outcome
  14. ,ParticipantAlias.ParticipantName
  15. ,CaseAction.ActionTaken
  16. FROM [Warehouse].[Table].[Case] as Cases
  17. LEFT JOIN [Warehouse].[Table].[CaseParticipant] as Participant
  18. ON Cases.CaseNumber = Participant.CaseNumber
  19. LEFT JOIN [Warehouse].[Table].[CaseIssue] as Issues
  20. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  21. LEFT JOIN [Warehouse].[Table].[CaseAction] as CaseAction
  22. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  23. LEFT JOIN [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  24. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  25. WHERE [Cases].[CaseNumber] = '123'
  26. </details>
  27. # 答案1
  28. **得分**: 0
  29. 根据您的查询,我无法确定应该是视图还是存储过程,但我建议使用后者。以下是使用存储过程实现预期输出的示例。输出如下:
  30. ```sql
  31. USE [Warehouse]
  32. GO
  33. SET ANSI_NULLS ON
  34. GO
  35. SET QUOTED_IDENTIFIER ON
  36. GO
  37. CREATE PROCEDURE [dbo].[SP_GetCaseResult]
  38. -- 添加存储过程的参数
  39. @caseNumber as int
  40. AS
  41. BEGIN
  42. SET NOCOUNT ON;
  43. DROP TABLE IF EXISTS #Temp_table
  44. -- 1. 将原始查询结果存储到临时表
  45. SELECT
  46. Cases.CaseNumber
  47. ,Cases.DateOpened
  48. ,Cases.DateClosed
  49. ,Participant.CaseParticipantId
  50. ,Participant.CaseParticipantKey
  51. ,Participant.ParticipantAliasId
  52. ,Participant.RoleInCase
  53. ,Participant.RelToOrganization
  54. ,Participant.Practice
  55. ,Participant.City
  56. ,Issues.Issue
  57. ,Issues.IssueSubCategory
  58. ,Issues.CaseIssueId
  59. ,Issues.Outcome
  60. ,ParticipantAlias.ParticipantName
  61. ,CaseAction.ActionTaken
  62. INTO #Temp_table
  63. FROM [Warehouse].[Table].[Case] as Cases
  64. LEFT JOIN
  65. [Warehouse].[Table].[CaseParticipant] as Participant
  66. ON Cases.CaseNumber = Participant.CaseNumber
  67. LEFT JOIN
  68. [Warehouse].[Table].[CaseIssue] as Issues
  69. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  70. LEFT JOIN
  71. [Warehouse].[Table].[CaseAction] as CaseAction
  72. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  73. LEFT JOIN
  74. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  75. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  76. WHERE [Cases].[CaseNumber] = @caseNumber
  77. -- 2. 从临时表中查询,排除 "查看以前的操作"
  78. SELECT * FROM #Temp_table t1 WHERE t1.ActionTaken <> '查看以前的操作'
  79. -- "联接"两个结果
  80. UNION ALL
  81. -- 3. 从临时表中查询,联接两个带和不带 "查看以前的操作" 的表,并获取所需的输出
  82. SELECT
  83. t1.CaseNumber
  84. ,t1.DateOpened
  85. ,t1.DateClosed
  86. ,t1.CaseParticipantId
  87. ,t1.CaseParticipantKey
  88. ,t1.ParticipantAliasId
  89. ,t1.RoleInCase
  90. ,t1.RelToOrganization
  91. ,t1.Practice
  92. ,t1.City
  93. ,t1.Issue
  94. ,t1.IssueSubCategory
  95. ,t1.CaseIssueId
  96. ,t1.Outcome
  97. ,t1.ParticipantName
  98. ,t2.ActionTaken
  99. FROM #Temp_table t2
  100. INNER JOIN #Temp_table t1 ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
  101. WHERE t1.ActionTaken = '查看以前的操作' AND t2.ActionTaken <> '查看以前的操作';
  102. END
  103. GO

或者,如果您真的希望不使用存储过程,以下是一个简单查询的示例:

  1. DECLARE @caseNumber int = '123'
  2. SELECT DISTINCT
  3. t1.CaseNumber
  4. ,t1.DateOpened
  5. ,t1.DateClosed
  6. ,t1.CaseParticipantId
  7. ,t1.CaseParticipantKey
  8. ,t1.ParticipantAliasId
  9. ,t1.RoleInCase
  10. ,t1.RelToOrganization
  11. ,t1.Practice
  12. ,t1.City
  13. ,t1.Issue
  14. ,t1.IssueSubCategory
  15. ,t1.CaseIssueId
  16. ,t1.Outcome
  17. ,t1.ParticipantName
  18. ,t2.ActionTaken
  19. FROM (
  20. SELECT
  21. Cases.CaseNumber
  22. ,Cases.DateOpened
  23. ,Cases.DateClosed
  24. ,Participant.CaseParticipantId
  25. ,Participant.CaseParticipantKey
  26. ,Participant.ParticipantAliasId
  27. ,Participant.RoleInCase
  28. ,Participant.RelToOrganization
  29. ,Participant.Practice
  30. ,Participant.City
  31. ,Issues.Issue
  32. ,Issues.IssueSubCategory
  33. ,Issues.CaseIssueId
  34. ,Issues.Outcome
  35. ,ParticipantAlias.ParticipantName
  36. ,CaseAction.ActionTaken
  37. FROM [Warehouse].[Table].[Case] as Cases
  38. LEFT JOIN
  39. [Warehouse].[Table].[CaseParticipant] as Participant
  40. ON Cases.CaseNumber = Participant.CaseNumber
  41. LEFT JOIN
  42. [Warehouse].[Table].[CaseIssue] as Issues
  43. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  44. LEFT JOIN
  45. [Warehouse].[Table].[CaseAction] as CaseAction
  46. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  47. LEFT JOIN
  48. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  49. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  50. WHERE [Cases].[CaseNumber] = @caseNumber --123
  51. ) t1
  52. FULL OUTER JOIN (
  53. SELECT
  54. Cases.CaseNumber
  55. ,Cases.DateOpened
  56. ,Cases.DateClosed
  57. ,Participant.CaseParticipantId
  58. ,Participant.CaseParticipantKey
  59. ,Participant.ParticipantAliasId
  60. ,Participant.RoleInCase
  61. ,Participant.RelToOrganization
  62. ,Participant.Practice
  63. ,Participant.City
  64. ,Issues.Issue
  65. ,Issues.IssueSubCategory
  66. ,Issues.CaseIssueId
  67. ,Issues.Outcome
  68. ,ParticipantAlias.ParticipantName
  69. ,CaseAction.ActionTaken
  70. FROM [Warehouse].[Table].[Case] as Cases
  71. LEFT JOIN
  72. [Warehouse].[Table].[CaseParticipant] as Participant
  73. ON Cases.CaseNumber = Participant.CaseNumber
  74. LEFT JOIN
  75. [Warehouse].[Table].[CaseIssue] as Issues
  76. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  77. LEFT JOIN
  78. [Warehouse].[Table].[CaseAction] as CaseAction
  79. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  80. LEFT JOIN
  81. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  82. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  83. WHERE [Cases].[CaseNumber] = @caseNumber --123
  84. ) t2
  85. ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
  86. WHERE t2.ActionTaken <> '查看以前的操作';
英文:

Based on your query, I cannot tell whether it should be a view or procedure, but I'd suggest making the latter.
Here is an example of how to achieve the expected output with a stored procedure.
The output:
Stored Procedure Sample Output

  1. USE [Warehouse]
  2. GO
  3. SET ANSI_NULLS ON
  4. GO
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7. CREATE PROCEDURE [dbo].[SP_GetCaseResult]
  8. -- Add the parameters for the stored procedure here
  9. @caseNumber as int
  10. AS
  11. BEGIN
  12. SET NOCOUNT ON;
  13. DROP TABLE IF EXISTS #Temp_table
  14. -- 1. Original query into temp table
  15. SELECT
  16. Cases.CaseNumber
  17. ,Cases.DateOpened
  18. ,Cases.DateClosed
  19. ,Participant.CaseParticipantId
  20. ,Participant.CaseParticipantKey
  21. ,Participant.ParticipantAliasId
  22. ,Participant.RoleInCase
  23. ,Participant.RelToOrganization
  24. ,Participant.Practice
  25. ,Participant.City
  26. ,Issues.Issue
  27. ,Issues.IssueSubCategory
  28. ,Issues.CaseIssueId
  29. ,Issues.Outcome
  30. ,ParticipantAlias.ParticipantName
  31. ,CaseAction.ActionTaken
  32. INTO #Temp_table
  33. FROM [Warehouse].[Table].[Case] as Cases
  34. LEFT JOIN
  35. [Warehouse].[Table].[CaseParticipant] as Participant
  36. ON Cases.CaseNumber = Participant.CaseNumber
  37. LEFT JOIN
  38. [Warehouse].[Table].[CaseIssue] as Issues
  39. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  40. LEFT JOIN
  41. [Warehouse].[Table].[CaseAction] as CaseAction
  42. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  43. LEFT JOIN
  44. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  45. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  46. WHERE [Cases].[CaseNumber] = @caseNumber
  47. -- 2. Query from temptable without &quot;See previous actions&quot;
  48. SELECT * FROM #Temp_table t1 WHERE t1.ActionTaken &lt;&gt; &#39;See Previous Actions&#39;
  49. -- &quot;Joining&quot; the two results
  50. UNION ALL
  51. -- 3. Query from temptable with joining the two table with and without &quot;See previous actions&quot; and getting needed output.
  52. SELECT
  53. t1.CaseNumber
  54. ,t1.DateOpened
  55. ,t1.DateClosed
  56. ,t1.CaseParticipantId
  57. ,t1.CaseParticipantKey
  58. ,t1.ParticipantAliasId
  59. ,t1.RoleInCase
  60. ,t1.RelToOrganization
  61. ,t1.Practice
  62. ,t1.City
  63. ,t1.Issue
  64. ,t1.IssueSubCategory
  65. ,t1.CaseIssueId
  66. ,t1.Outcome
  67. ,t1.ParticipantName
  68. ,t2.ActionTaken
  69. FROM #Temp_table t2
  70. INNER JOIN #Temp_table t1 ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
  71. WHERE t1.ActionTaken = &#39;See Previous Actions&#39; AND t2.ActionTaken &lt;&gt; &#39;See Previous Actions&#39;
  72. END
  73. GO

OR If you really want it without stored-procedure: Here is an example for it:

Example of simple query

  1. DECLARE @caseNumber int = &#39;123&#39;
  2. SELECT DISTINCT
  3. t1.CaseNumber
  4. ,t1.DateOpened
  5. ,t1.DateClosed
  6. ,t1.CaseParticipantId
  7. ,t1.CaseParticipantKey
  8. ,t1.ParticipantAliasId
  9. ,t1.RoleInCase
  10. ,t1.RelToOrganization
  11. ,t1.Practice
  12. ,t1.City
  13. ,t1.Issue
  14. ,t1.IssueSubCategory
  15. ,t1.CaseIssueId
  16. ,t1.Outcome
  17. ,t1.ParticipantName
  18. ,t2.ActionTaken
  19. FROM (
  20. SELECT
  21. Cases.CaseNumber
  22. ,Cases.DateOpened
  23. ,Cases.DateClosed
  24. ,Participant.CaseParticipantId
  25. ,Participant.CaseParticipantKey
  26. ,Participant.ParticipantAliasId
  27. ,Participant.RoleInCase
  28. ,Participant.RelToOrganization
  29. ,Participant.Practice
  30. ,Participant.City
  31. ,Issues.Issue
  32. ,Issues.IssueSubCategory
  33. ,Issues.CaseIssueId
  34. ,Issues.Outcome
  35. ,ParticipantAlias.ParticipantName
  36. ,CaseAction.ActionTaken
  37. FROM [Warehouse].[Table].[Case] as Cases
  38. LEFT JOIN
  39. [Warehouse].[Table].[CaseParticipant] as Participant
  40. ON Cases.CaseNumber = Participant.CaseNumber
  41. LEFT JOIN
  42. [Warehouse].[Table].[CaseIssue] as Issues
  43. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  44. LEFT JOIN
  45. [Warehouse].[Table].[CaseAction] as CaseAction
  46. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  47. LEFT JOIN
  48. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  49. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  50. WHERE [Cases].[CaseNumber] = @caseNumber --123
  51. ) t1
  52. FULL OUTER JOIN (
  53. SELECT
  54. Cases.CaseNumber
  55. ,Cases.DateOpened
  56. ,Cases.DateClosed
  57. ,Participant.CaseParticipantId
  58. ,Participant.CaseParticipantKey
  59. ,Participant.ParticipantAliasId
  60. ,Participant.RoleInCase
  61. ,Participant.RelToOrganization
  62. ,Participant.Practice
  63. ,Participant.City
  64. ,Issues.Issue
  65. ,Issues.IssueSubCategory
  66. ,Issues.CaseIssueId
  67. ,Issues.Outcome
  68. ,ParticipantAlias.ParticipantName
  69. ,CaseAction.ActionTaken
  70. FROM [Warehouse].[Table].[Case] as Cases
  71. LEFT JOIN
  72. [Warehouse].[Table].[CaseParticipant] as Participant
  73. ON Cases.CaseNumber = Participant.CaseNumber
  74. LEFT JOIN
  75. [Warehouse].[Table].[CaseIssue] as Issues
  76. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  77. LEFT JOIN
  78. [Warehouse].[Table].[CaseAction] as CaseAction
  79. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  80. LEFT JOIN
  81. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  82. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  83. WHERE [Cases].[CaseNumber] = @caseNumber --123
  84. ) t2
  85. ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
  86. WHERE t2.ActionTaken &lt;&gt; &#39;See Previous Actions&#39;

答案2

得分: 0

以下是 UNION 示例的翻译部分:

然而,从性能考虑,如果您的表包含数十万条记录,那么存储过程是“最快”的,因为它只查询主要数据一次!(之后临时表正在使用)。

在另外两个示例中,您查询表的次数越多(其中包含大量数据),性能就会越差。如果表中没有太多数据,性能差异不大。
所以您可以选择使用哪种方法来获取所期望的输出。

UNION 示例结果

  1. DECLARE @caseNumber int = '123'
  2. SELECT
  3. Cases.CaseNumber
  4. ,Cases.DateOpened
  5. ,Cases.DateClosed
  6. ,Participant.CaseParticipantId
  7. ,Participant.CaseParticipantKey
  8. ,Participant.ParticipantAliasId
  9. ,Participant.RoleInCase
  10. ,Participant.RelToOrganization
  11. ,Participant.Practice
  12. ,Participant.City
  13. ,Issues.Issue
  14. ,Issues.IssueSubCategory
  15. ,Issues.CaseIssueId
  16. ,Issues.Outcome
  17. ,ParticipantAlias.ParticipantName
  18. ,CaseAction.ActionTaken
  19. FROM [Warehouse].[Table].[Case] as Cases
  20. LEFT JOIN
  21. [Warehouse].[Table].[CaseParticipant] as Participant
  22. ON Cases.CaseNumber = Participant.CaseNumber
  23. LEFT JOIN
  24. [Warehouse].[Table].[CaseIssue] as Issues
  25. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  26. LEFT JOIN
  27. [Warehouse].[Table].[CaseAction] as CaseAction
  28. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  29. LEFT JOIN
  30. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  31. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  32. WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken &lt;&gt; 'See Previous Actions';
  33. UNION ALL
  34. SELECT DISTINCT
  35. t1.CaseNumber
  36. ,t1.DateOpened
  37. ,t1.DateClosed
  38. ,t1.CaseParticipantId
  39. ,t1.CaseParticipantKey
  40. ,t1.ParticipantAliasId
  41. ,t1.RoleInCase
  42. ,t1.RelToOrganization
  43. ,t1.Practice
  44. ,t1.City
  45. ,t1.Issue
  46. ,t1.IssueSubCategory
  47. ,t1.CaseIssueId
  48. ,t1.Outcome
  49. ,t1.ParticipantName
  50. ,t2.ActionTaken
  51. FROM (
  52. SELECT
  53. Cases.CaseNumber
  54. ,Cases.DateOpened
  55. ,Cases.DateClosed
  56. ,Participant.CaseParticipantId
  57. ,Participant.CaseParticipantKey
  58. ,Participant.ParticipantAliasId
  59. ,Participant.RoleInCase
  60. ,Participant.RelToOrganization
  61. ,Participant.Practice
  62. ,Participant.City
  63. ,Issues.Issue
  64. ,Issues.IssueSubCategory
  65. ,Issues.CaseIssueId
  66. ,Issues.Outcome
  67. ,ParticipantAlias.ParticipantName
  68. ,CaseAction.ActionTaken
  69. FROM [Warehouse].[Table].[Case] as Cases
  70. LEFT JOIN
  71. [Warehouse].[Table].[CaseParticipant] as Participant
  72. ON Cases.CaseNumber = Participant.CaseNumber
  73. LEFT JOIN
  74. [Warehouse].[Table].[CaseIssue] as Issues
  75. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  76. LEFT JOIN
  77. [Warehouse].[Table].[CaseAction] as CaseAction
  78. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  79. LEFT JOIN
  80. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  81. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  82. WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken = 'See Previous Actions'
  83. ) t1
  84. INNER JOIN (
  85. SELECT
  86. Cases.CaseNumber
  87. ,Cases.DateOpened
  88. ,Cases.DateClosed
  89. ,Participant.CaseParticipantId
  90. ,Participant.CaseParticipantKey
  91. ,Participant.ParticipantAliasId
  92. ,Participant.RoleInCase
  93. ,Participant.RelToOrganization
  94. ,Participant.Practice
  95. ,Participant.City
  96. ,Issues.Issue
  97. ,Issues.IssueSubCategory
  98. ,Issues.CaseIssueId
  99. ,Issues.Outcome
  100. ,ParticipantAlias.ParticipantName
  101. ,CaseAction.ActionTaken
  102. FROM [Warehouse].[Table].[Case] as Cases
  103. LEFT JOIN
  104. [Warehouse].[Table].[CaseParticipant] as Participant
  105. ON Cases.CaseNumber = Participant.CaseNumber
  106. LEFT JOIN
  107. [Warehouse].[Table].[CaseIssue] as Issues
  108. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  109. LEFT JOIN
  110. [Warehouse].[Table].[CaseAction] as CaseAction
  111. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  112. LEFT JOIN
  113. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  114. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  115. WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken &lt;&gt; 'See Previous Actions'
  116. ) t2
  117. ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
英文:

Here is the UNION sample also:

However considering performance, if your tables contain hundreds of thousands of records, then the stored procedure is the "fastest" as it only queries the main data once ! (afterwards the temporary tables are in use).

In the other 2 examples, the more you query the tables (which contains many data) the less the performance will be. There won't be much difference if the tables don't contain many data inside.
So it's up to you what way method you'd choose getting the expected output.

UNION example result

  1. DECLARE @caseNumber int = &#39;123&#39;
  2. SELECT
  3. Cases.CaseNumber
  4. ,Cases.DateOpened
  5. ,Cases.DateClosed
  6. ,Participant.CaseParticipantId
  7. ,Participant.CaseParticipantKey
  8. ,Participant.ParticipantAliasId
  9. ,Participant.RoleInCase
  10. ,Participant.RelToOrganization
  11. ,Participant.Practice
  12. ,Participant.City
  13. ,Issues.Issue
  14. ,Issues.IssueSubCategory
  15. ,Issues.CaseIssueId
  16. ,Issues.Outcome
  17. ,ParticipantAlias.ParticipantName
  18. ,CaseAction.ActionTaken
  19. FROM [Warehouse].[Table].[Case] as Cases
  20. LEFT JOIN
  21. [Warehouse].[Table].[CaseParticipant] as Participant
  22. ON Cases.CaseNumber = Participant.CaseNumber
  23. LEFT JOIN
  24. [Warehouse].[Table].[CaseIssue] as Issues
  25. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  26. LEFT JOIN
  27. [Warehouse].[Table].[CaseAction] as CaseAction
  28. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  29. LEFT JOIN
  30. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  31. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  32. WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken &lt;&gt; &#39;See Previous Actions&#39;
  33. UNION ALL
  34. SELECT DISTINCT
  35. t1.CaseNumber
  36. ,t1.DateOpened
  37. ,t1.DateClosed
  38. ,t1.CaseParticipantId
  39. ,t1.CaseParticipantKey
  40. ,t1.ParticipantAliasId
  41. ,t1.RoleInCase
  42. ,t1.RelToOrganization
  43. ,t1.Practice
  44. ,t1.City
  45. ,t1.Issue
  46. ,t1.IssueSubCategory
  47. ,t1.CaseIssueId
  48. ,t1.Outcome
  49. ,t1.ParticipantName
  50. ,t2.ActionTaken
  51. FROM (
  52. SELECT
  53. Cases.CaseNumber
  54. ,Cases.DateOpened
  55. ,Cases.DateClosed
  56. ,Participant.CaseParticipantId
  57. ,Participant.CaseParticipantKey
  58. ,Participant.ParticipantAliasId
  59. ,Participant.RoleInCase
  60. ,Participant.RelToOrganization
  61. ,Participant.Practice
  62. ,Participant.City
  63. ,Issues.Issue
  64. ,Issues.IssueSubCategory
  65. ,Issues.CaseIssueId
  66. ,Issues.Outcome
  67. ,ParticipantAlias.ParticipantName
  68. ,CaseAction.ActionTaken
  69. FROM [Warehouse].[Table].[Case] as Cases
  70. LEFT JOIN
  71. [Warehouse].[Table].[CaseParticipant] as Participant
  72. ON Cases.CaseNumber = Participant.CaseNumber
  73. LEFT JOIN
  74. [Warehouse].[Table].[CaseIssue] as Issues
  75. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  76. LEFT JOIN
  77. [Warehouse].[Table].[CaseAction] as CaseAction
  78. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  79. LEFT JOIN
  80. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  81. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  82. WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken = &#39;See Previous Actions&#39;
  83. ) t1
  84. INNER JOIN (
  85. SELECT
  86. Cases.CaseNumber
  87. ,Cases.DateOpened
  88. ,Cases.DateClosed
  89. ,Participant.CaseParticipantId
  90. ,Participant.CaseParticipantKey
  91. ,Participant.ParticipantAliasId
  92. ,Participant.RoleInCase
  93. ,Participant.RelToOrganization
  94. ,Participant.Practice
  95. ,Participant.City
  96. ,Issues.Issue
  97. ,Issues.IssueSubCategory
  98. ,Issues.CaseIssueId
  99. ,Issues.Outcome
  100. ,ParticipantAlias.ParticipantName
  101. ,CaseAction.ActionTaken
  102. FROM [Warehouse].[Table].[Case] as Cases
  103. LEFT JOIN
  104. [Warehouse].[Table].[CaseParticipant] as Participant
  105. ON Cases.CaseNumber = Participant.CaseNumber
  106. LEFT JOIN
  107. [Warehouse].[Table].[CaseIssue] as Issues
  108. ON Participant.CaseParticipantId = Issues.CaseParticipantId
  109. LEFT JOIN
  110. [Warehouse].[Table].[CaseAction] as CaseAction
  111. ON Issues.CaseIssueId = CaseAction.CaseIssueId
  112. LEFT JOIN
  113. [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
  114. ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
  115. WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken &lt;&gt; &#39;See Previous Actions&#39;
  116. ) t2
  117. ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId

huangapple
  • 本文由 发表于 2023年2月19日 03:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定