简体   繁体   English

使用IN语句的Where子句中的Case语句

[英]Case Statement in Where clause using the IN statement

Not sure if this is possible, but I want to specify a select query within an IN statement based on certain criteria. 不知道这是否可能,但是我想基于某些条件在IN语句中指定一个选择查询。 Here is my SQL Query: 这是我的SQL查询:

SET @DepartmentId = (SELECT TOP 1 DepartmentId FROM Organisations WHERE OrganisationId = @OrgId)

CREATE TABLE #JustificationDetails
(
    JustificationId INT,
    Code VARCHAR(5),
    AccCategory CHAR(2),
    Description VARCHAR(200),
    ActualTotals DECIMAL(18, 2),
    CurrentTotals DECIMAL(18, 2),
    FutureTotals DECIMAL(18, 2),
    IncreaseDecreaseTotals DECIMAL(18, 2),
    IncreaseDecreasePerc INT,
    Justification VARCHAR(MAX)
)

INSERT INTO #JustificationDetails
    SELECT 
        ISNULL(J.pkiJustificationId, 0), 
        RIGHT(A.AccountNumber, 5) AS AccVote, 
        LEFT(RIGHT(A.AccountNumber, 5), 2) AS AccCategory, 
        A.Description, 
        0, 0, 0, 0, 0,
        ISNULL(J.JustificationReason, '''')
    FROM 
        Accounts A 
    INNER JOIN 
        Notes_Line_Entries NLE ON A.AccountId = NLE.fkiAccountId
    INNER JOIN 
        MasterBudgets MB ON NLE.budgetId = MB.BudgetId
    INNER JOIN 
        Organisations O ON MB.OrganisationId = O.OrganisationId
    LEFT JOIN 
        Justifications J ON O.OrganisationId = J.fkiOrganisationId
    WHERE 
        fkiNotesColumnId = 3 
        AND O.OrganisationId IN (CASE
                                    WHEN @DepartmentId = 2 
                                       THEN (SELECT OrganisationId FROM Organisations
                                             WHERE ParentOrganisationId IN (SELECT OrganisationId 
                                                                                            FROM Organisations 
            WHERE ParentOrganisationId IN (SELECT OrganisationId 
                                           FROM Organisations 
                                           WHERE ParentOrganisationId = @OrgId)))
                                    WHEN @DepartmentId = 4 
                                       THEN (SELECT OrganisationId FROM Organisations
                                             WHERE ParentOrganisationId IN (SELECT OrganisationId 
            FROM Organisations 
            WHERE ParentOrganisationId IN (SELECT OrganisationId 
                                           FROM Organisations 
                                           WHERE ParentOrganisationId = @OrgId)))
                                    WHEN @DepartmentId = 3 
                                       THEN (SELECT OrganisationId 
                                             FROM Organisations 
                                             WHERE ParentOrganisationId IN (SELECT OrganisationId 
           FROM Organisations 
           WHERE OrganisationId = @OrgId))
                                    WHEN @DepartmentId = 5 
                                       THEN (SELECT OrganisationId
                                             FROM Organisations 
                                             WHERE ParentOrganisationId IN (SELECT OrganisationId 
           FROM Organisations 
           WHERE OrganisationId = @OrgId))
                                 END)
GROUP BY 
    J.pkiJustificationId, A.Category, A.Description, 
    RIGHT(A.AccountNumber, 5), J.JustificationReason
ORDER BY 
    A.Category, RIGHT(A.AccountNumber, 5)

I get the following error: 我收到以下错误:

Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

If I run only one of the select queries in the IN() statement, then it works, so I am only assuming this is not possible ? 如果我仅在IN()语句中运行选择查询之一,那么它将起作用,因此我仅假设这不可能?

Thanks for any help. 谢谢你的帮助。

A CASE statement can't return a dataset, so it can't be used with an IN clause. CASE语句无法返回数据集,因此不能与IN子句一起使用。
I think the logic of your code can be written like this: 我认为您的代码逻辑可以这样写:

WHERE fkiNotesColumnId = 3 AND (      
  (
    @DepartmentId IN (2, 4) AND 
    O.OrganisationId IN (
      SELECT OrganisationId FROM Organisations
      WHERE ParentOrganisationId IN (
        SELECT OrganisationId FROM Organisations 
        WHERE ParentOrganisationId IN (
          SELECT OrganisationId FROM Organisations 
          WHERE ParentOrganisationId = @OrgId
        )
      )
    )
  )
  OR
  (
    @DepartmentId IN (3, 5) AND 
    O.OrganisationId IN (
      SELECT OrganisationId FROM Organisations 
      WHERE ParentOrganisationId IN (
        SELECT OrganisationId FROM Organisations 
        WHERE OrganisationId = @OrgId
      )
    )
  )
)

I ended up finding a different way of achieving what I wanted. 我最终找到了实现自己想要的方式的另一种方式。 Even though the answer that @forpas gave was initially what helped, I needed something that could more easily drill down to the lowest level in my hierarchy structure. 即使@forpas给出的答案最初是有所帮助的,我仍然需要一些可以更轻松地向下钻取到层次结构中最低层的东西。 I made use of a WITH CTE 我使用了WITH CTE

Here is the link that helped me : WITH CTE 这是对我有帮助的链接: CTE

So now I use the With Cte to populate a temp table and I just call the Id's from that temp table , which now replaces that whole Where clause that @forpas provided. 因此,现在我使用With Cte填充临时表,而我只是从该temp table调用ID,它现在替换了@forpas提供的整个Where子句。

Below is my query: 以下是我的查询:

;with tmp (OrganisationId, ParentOrganisationId, DepartmentId) as (
select OrganisationId, ParentOrganisationId, DepartmentId
from Organisations
where OrganisationId = @OrgId
union all
select Organisations.OrganisationId, Organisations.ParentOrganisationId, Organisations.DepartmentId
from tmp
inner join Organisations on tmp.OrganisationId = Organisations.ParentOrganisationId
)

select DISTINCT OrganisationId, ParentOrganisationId, DepartmentId
INTO #MyTempTable
from tmp
WHERE DepartmentId = 6

DepartmentId 6 is my lowest level that I require. DepartmentId 6是我需要的最低级别。

SELECT ISNULL(J.pkiJustificationId,0), 
       RIGHT(A.AccountNumber,5) as AccVote, 
       LEFT(RIGHT(A.AccountNumber,5),2) as AccCategory, 
       A.Description, 
       0,
       0,
       0,
       0,
       0,
       ISNULL(J.JustificationReason,''),
       ISNULL(J.fkiOrganisationId,@OrgId),
       ISNULL(J.fkiUserId,'')
FROM Accounts A 
        INNER JOIN Notes_Line_Entries NLE ON A.AccountId = NLE.fkiAccountId
        INNER JOIN MasterBudgets MB ON NLE.budgetId = MB.BudgetId
        INNER JOIN Organisations O ON MB.OrganisationId = O.OrganisationId
        LEFT JOIN Justifications J ON O.OrganisationId = J.fkiOrganisationId
WHERE fkiNotesColumnId = 3 AND
       O.OrganisationId IN (SELECT OrganisationId FROM #MyTempTable)
GROUP BY J.pkiJustificationId,A.Category, A.Description, RIGHT(A.AccountNumber,5), J.JustificationReason,J.fkiOrganisationId,J.fkiUserId
ORDER BY A.Category,RIGHT(A.AccountNumber,5)

I am not sure if this could help or lead anyone in the right direction, but I thought I would post this in any case. 我不确定这是否可以帮助或引导任何人朝正确的方向前进,但是我认为无论如何我都会发布此信息。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM