TSQL Hack需要获取数据过滤器

时间:2009-06-08 14:37:25

标签: tsql filtering append

UI(在报告显示之前)显示具有

的查找(组合)
  • (ID = 0)。所有组织单位
  • (ID = 4).HR
  • (ID = 5).DEV

我需要:

  1. 能够显示(4)+(5)的数据 选择(0)。
  2. 如果选择了HR或DEV,则仅(4)或(5)。

  3. 查找组合代码(已选择在以下查询中提供参数。)


    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?

5 个答案:

答案 0 :(得分:2)

首先,您的查找组合代码可能会收紧一点:

-- 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)))

答案 1 :(得分:0)

这样的事情应该起作用

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

答案 2 :(得分:0)

怎么样

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));

答案 3 :(得分:0)

试试这个,应该在查询中使用索引...

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 .....

答案 4 :(得分:0)

KM的版本可以使用,但此查询不需要临时表...

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