質問

UI (レポートが表示される前) には、次のルックアップ (コンボ) が表示されます。

  • (ID = 0).すべての組織単位
  • (ID =4).HR
  • (ID = 5).DEV

する必要がある:

  1. (4) +(5)のデータを表示できます(0)が選択されています。
  2. HR または DEV のいずれかが選択されている場合は、(4) または (5) のみ。

ルックアップ コンボ コード (選択すると、以下のクエリのパラメーターがフィードされます。)


Select 0 AS ID,'All Org' AS Name from  DP_ORG_OrganizationUnit
where DP_ORG_OrganizationUnit.Code IN {AccessData}
Union
SELECT 
DP_ORG_OrganizationUnit.ID,
DP_ORG_OrganizationUnit.Name
FROM DP_ORG_OrganizationUnit  where DP_ORG_OrganizationUnit.Code IN ('HR','DEV')


レポートデータ行クエリ


SET CONCAT_NULL_YIELDS_NULL OFF

DECLARE @EmpID as int; 
DECLARE @OrganizationUnit as int; 
DECLARE @StartDate as datetime;
DECLARE @EndDate as datetime;

SET @EmpID = ?;
SET @StartDate = ?;
SET @EndDate = ?;
SET @OrganizationUnit = ?;

SELECT
Employee.Code,
Employee.Name1+' '+Employee.Name2+' '+Employee.Name3+' '+Employee.Name4+' '+Employee.Name5 AS FullName,
Employee.OrganizationUnit,  
ContractType.Name,
EmployeeContract.StartDate,
EmployeeContract.EndDate
FROM Employee INNER JOIN (ContractType INNER JOIN EmployeeContract 
ON ContractType.ID = EmployeeContract.ContractType) 
ON Employee.ID = EmployeeContract.Employee
WHERE (Employee.ID = @EmpID  OR  @EmpID=0)
AND
(Employee.OrganizationUnit = @OrganizationUnit  OR  @OrganizationUnit=0)
AND  NOT((EndDate <  @StartDate or StartDate > @EndDate)); 

見た目からそれを達成できる方法はありますか?0 = 0は、他の部門からのすべてのデータも表示されます。

誰か:-o?

役に立ちましたか?

解決

まず第一に、あなたのルックアップコンボコードが少しアップ締めることができます:

-- the FROM clause was superfluous
SELECT 0 AS ID,'All Org' AS Name 
UNION ALL
-- the two-part identifiers were superfluous (only one table)
SELECT ID, Name
FROM DP_ORG_OrganizationUnit
WHERE Code IN ('HR','DEV')

レポートクエリの場合は、最も単純な形式は以下のようになります:

WHERE 
  ((@OrganizationUnit > 0 AND Employee.OrganizationUnit = @OrganizationUnit) OR 
   (@OrganizationUnit = 0 AND Employee.OrganizationUnit IN (4,5)))

他のヒント

このような何かが動作するはずです。

Where (Employee.OrganizationUnit = case when @OrganizationUnit=0 then 4 else @OrganizationUnit end OR case when @OrganizationUnit=0 then 5 else @OrganizationUnit end)

タグ...あなたのクエリにインデックスを使用すべき、これを試してみてください
DECALRE @FilterValues (FilterValue   int not null primary key)

IF @Param=0
BEGIN
    INSERT INTO @FilterValues VALUES (4)
    INSERT INTO @FilterValues VALUES (5)
END
ELSE ID @PAram IS NOT NULL
BEGIN
    INSERT INTO @FilterValues VALUES (@Param)
END

SELECT
    ....
    FROM YourTable                y
        INNER JOIN @FilterValues  f ON y.Value=f.Value
    WHERE .....

KM のバージョンは機能しますが、このクエリには一時テーブルは必要ありません...

SELECT *
FROM Employee
WHERE (
         @OrganizationUnit = 0
         OR 
         (
             @OrganizationUnit <> 0
             AND
             Employee.OrganizationUnit = @OrganizationUnit
         )
      )

どの程度

WHERE (Employee.ID = @EmpID  OR  @EmpID=0)
AND
(Employee.OrganizationUnit BETWEEN ISNULL(NULLIF(@OrganizationUnit,0),0) AND ISNULL(NULLIF(@OrganizationUnit,0),99))
AND  NOT((EndDate <  @StartDate or StartDate > @EndDate));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top