TSQL corte necessário para a obtenção de um filtro para dados
Pergunta
A interface do usuário (antes dos shows relatório) mostra um olhar para cima (Combo) que tem
- (ID = 0) .Todos Organização Unidades
- (ID = 4) .HR
- (ID = 5) .dev
eu preciso:
- Ser capaz de mostrar dados de (4) + (5) se (0) é selecionado.
- Só (4) ou (5) se HR ou DEV é seleccionado.
código de combinação Lookup (feeds selecionados do parâmetro no abaixo consulta.)
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')
Relatório de linha de dados de consulta ??p>
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));
Qualquer maneira que eu posso alcançá-lo a partir da aparência dele? 0 = 0 iria mostrar todos os dados de outros departamentos também ..
Qualquer um:? -O
Solução
Em primeiro lugar, o seu código de combinação de pesquisa poderia ser apertado um pouco:
-- 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')
Para a consulta do relatório, a forma mais simples seria:
WHERE
((@OrganizationUnit > 0 AND Employee.OrganizationUnit = @OrganizationUnit) OR
(@OrganizationUnit = 0 AND Employee.OrganizationUnit IN (4,5)))
Outras dicas
algo como isso deve funcionar
Where (Employee.OrganizationUnit = case when @OrganizationUnit=0 then 4 else @OrganizationUnit end OR case when @OrganizationUnit=0 then 5 else @OrganizationUnit end)
Tente este, que deve utilizar índices em sua consulta ...
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 .....
versão do KM vai funcionar, mas essa consulta não precisa de uma tabela temporária ...
SELECT *
FROM Employee
WHERE (
@OrganizationUnit = 0
OR
(
@OrganizationUnit <> 0
AND
Employee.OrganizationUnit = @OrganizationUnit
)
)
Como cerca
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));