SQL SERVER: как выбрать данные, если они содержат нулевое значение - PullRequest
0 голосов
/ 15 октября 2019

Я пытаюсь выполнить следующий запрос, но получаю некоторые проблемы ...

EmployeeDomainName, ManagerName, TaskID, ProgramID, DepartmentID, ProgramOwner, CostCenter - параметры, допускающие множественные значения

Myпроблема в том, что ProgramId, ProgramName, ProgramOwner, CostCenter могут иметь значение NULL, поэтому, когда эти поля имеют значение NULL, и я применяю следующее, где условие

AND ProgramOwner IN (SELECT Item from [dbo].[SplitString](ISNULL(null,ProgramOwner),',')) 
AND ProductFamily IN (SELECT Item from [dbo].[SplitString](ISNULL(@InProductFamily,ProductFamily),','))
AND  CostCenter IN (SELECT Item from [dbo].[SplitString](ISNULL(@InCostCenter,CostCenter),','))

, тогда данные содержат нулевые значения, которые не отображаются в выходных данных, и когдаЯ комментирую эти два, где условия, то данные показывают. Так как я могу управлять, когда ProgramId, ProgramName, ProgramOwner, CostCenter содержат нулевые значения

       `declare   @InFromDate           DATETIME =  N'06/06/2019',
@InToDate             DATETIME= N'11/21/2019',
@InEmployeeDomainName NVARCHAR(50) = null,
@InBillableType          NVARCHAR(10) =null,
@InLogStatus              NVARCHAR(10) =null,
@InManagerName        NVARCHAR(max) =null,
@InTaskID             NVARCHAR(max) =null,
@InProgramID          NVARCHAR(max) =null,
@InProgramOwner       NVARCHAR(max) =null,
@InProductFamily      NVARCHAR(max) =null,
@InCostCenter         NVARCHAR(max)=null,
@InDepartmentID       NVARCHAR(max) =null



Select 
        prog.ProgramID,
        prog.ProgramName,
        prog.ProgramOwner, 
        ProductFamily,
        CostCenter,
        DepartmentName,
        logmaster.EmployeeCode,
        logmaster.EmployeeName,
        logmaster.EmployeeDomainName,
        ManagerName,
        logs.TaskID,
        TaskName,
        (CASE  WHEN IsBillable = 1 THEN 'Yes' WHEN IsBillable = 0 THEN 'NO' END) as IsBillables ,
        (CASE  WHEN IsMachineCostInvolve = 1 THEN 'Yes' WHEN IsMachineCostInvolve = 0 THEN 'NO' END) as IsMachineCostInvolve,
        MachineCostPerHour,
        (CASE When IsMachineCostInvolve = 1  then (MachineCostPerHour * sum(LoggedHours)) else null END) as MachineCost,
        (CASE  WHEN IsResourceCostInvolve = 1 THEN 'Yes' WHEN IsResourceCostInvolve = 0 THEN 'NO' END) as IsResourceCostInvolve, 
        rcost.HourlyResourceCost,
        (CASE When IsResourceCostInvolve = 1  then ( rcost.HourlyResourceCost * sum(LoggedHours)) else null END) as TotalResourceCost,
        sum(LoggedHours) TotalHrs ,
        ROW_NUMBER() OVER (ORDER BY prog.ProgramID ) AS Row


    from [dbo].defProgramMaster prog right outer join datTaskLogTransactionDetails logs 
    ON prog.ProgramID = logs.ProgramID 
    inner JOIN datTaskLogMaster logmaster ON logmaster.TaskLogID = logs.TaskLogID 
    INNER JOIN defTaskMaster taskmaster ON  taskmaster.TaskID= logs.TaskID 
    left outer JOIN defResourceCosts rcost on  rcost.EmployeeCode =logmaster.EmployeeCode and rcost.DepartmentID= logmaster.DepartmentID

WHERE LogDate BETWEEN  convert(varchar(10), @InFromDate, 23) AND convert(varchar(10), @InToDate, 23) 
    AND IsBillable = ( CASE WHEN @InBillableType IS NULL THEN IsBillable ELSE @InBillableType  END) 
    AND LogStatus = ( CASE WHEN @InLogStatus IS NULL THEN LogStatus ELSE @InLogStatus  END) 
    AND logmaster.EmployeeDomainName  IN ( (SELECT Item from [dbo].[SplitString](ISNULL(@InEmployeeDomainName,EmployeeDomainName),',')))
    AND logmaster.ManagerName IN (SELECT Item from [dbo].[SplitString](ISNULL(@InManagerName,ManagerName),','))
    AND logs.TaskID IN (SELECT Item from [dbo].[SplitString](ISNULL(@InTaskID,logs.TaskID),','))
    AND logs.ProgramID IN (SELECT Item  from [dbo].[SplitString](ISNULL(@InProgramID,logs.ProgramID),','))
    AND ProgramOwner IN (SELECT Item from [dbo].[SplitString](ISNULL(null,ProgramOwner),',')) 
    AND ProductFamily IN (SELECT Item from [dbo].[SplitString](ISNULL(@InProductFamily,ProductFamily),','))
    AND  CostCenter IN (SELECT Item from [dbo].[SplitString](ISNULL(@InCostCenter,CostCenter),','))
    and logmaster.DepartmentID IN (SELECT Item from [dbo].[SplitString](ISNULL(@InDepartmentID,logmaster.DepartmentID),','))


    group by 
    prog.ProgramID,
    prog.ProgramName,
    prog.ProgramOwner,
    prog.ProductFamily,
    CostCenter,
    DepartmentName,
    logmaster.EmployeeCode,
    logmaster.EmployeeName,
    logmaster.EmployeeDomainName,
    ManagerName,
    logs.TaskID,
    TaskName,
    IsBillable,
    IsMachineCostInvolve,
    MachineCostPerHour,
    IsResourceCostInvolve,
    HourlyResourceCost,
    IsMachineCostInvolve,
    IsResourceCostInvolve`

1 Ответ

0 голосов
/ 22 октября 2019

Здесь я создал #temp Table и использую функцию IFNULL, если данные равны NULL, тогда я заменил на 1

 ISNULL(prog.ProgramOwner,1) ProgramOwner, 
 ISNULL(ProductFamily,1) ProductFamily,
 ISNULL(CostCenter,1) CostCenter ,

, после этого я использую case, если data равен 1, тогда показать данные, вставленные в таблицу

case when ProgramOwner ='1' then NULL else ProgramOwner end as ProgramOwner,    
case when ProductFamily ='1' then NULL else ProductFamily end as ProductFamily, 
case when CostCenter ='1' then NULL else CostCenter end as CostCenter,

Вот запрос

Declare    @InFromDate           DATETIME = '2019-08-01',
@InToDate             DATETIME= '2019-08-31',
@InEmployeeDomainName NVARCHAR(50) =  null,--'C3ITDOMAIN\test3',--'C3ITDOMAIN\test3,C3ITDOMAIN\test7'
@InBillableType       INT = null,
@InLogStatus         INT = null,
@InManagerName        NVARCHAR(max) = null,--'Test Two',-- 'Test Two',
@InTaskID             NVARCHAR(max) = null,--'2039,2040',-- '2039,2040',
@InProgramID          NVARCHAR(max) = null,--null,-- '1024,2015',
@InProgramOwner       NVARCHAR(max) = null,--'Dhiraj kulkarni',-- 'Dhiraj kulkarni',
@InProductFamily      NVARCHAR(max) =  'cipla',--'Woodies',-- 'Woodies',
@InCostCenter         NVARCHAR(max) = null,--'ABD',-- 'ABD',
@InDepartmentID       NVARCHAR(max) = null,-- '2',
@InPageSize           BIGINT        = 10,           
@InPageIndex          INT           = 2,
@InSortingKey         VARCHAR(100)  = 'ROW',
@InIsAscending        TINYINT       = 2 



IF OBJECT_ID('tempdb..#Panging_Data_List') IS NOT NULL
DROP TABLE #Panging_Data_List



CREATE TABLE #Panging_Data_List
(ProgramID          [INT],
ProgramName         [nvarchar](max),
ProgramOwner        [nvarchar](max),
ProductFamily       [nvarchar](max),
CostCenter          [nvarchar](max),
DepartmentName      [nvarchar](max),
EmployeeCode        [nvarchar](max),
EmployeeName        [nvarchar](max), 
EmployeeDomainName  [nvarchar](max), 
ManagerName         [nvarchar](max), 
TaskName            [nvarchar](max), 
IsBillable          [nvarchar](max), 
IsMachineCostInvolve[nvarchar](max), 
MachineCostPerHour  DECIMAL(18,2),
MachineHours        DECIMAL(18,2),
MachineCost         DECIMAL(18,2),
IsResourceCostInvolve [nvarchar](max), 
HourlyResourceCost  DECIMAL(18,2),
TotalResourceCost   DECIMAL(18,2),
TotalHrs            DECIMAL(18,2),
[Row]                [INT],             
)


;WITH CTE AS
(
Select 
        prog.ProgramID,
        prog.ProgramName,
        ISNULL(prog.ProgramOwner,1) ProgramOwner, 
        ISNULL(ProductFamily,1) ProductFamily,
        ISNULL(CostCenter,1) CostCenter ,
        DepartmentName,
        logmaster.EmployeeCode,
        logmaster.EmployeeName,
        logmaster.EmployeeDomainName,
        ManagerName,
        TaskName,
        (CASE  WHEN IsBillable = 1 THEN 'Yes' WHEN IsBillable = 0 THEN 'NO' END) as IsBillable ,
        (CASE  WHEN IsMachineCostInvolve = 1 THEN 'Yes' WHEN IsMachineCostInvolve = 0 THEN 'NO' END) as IsMachineCostInvolve,
        MachineCostPerHour,
        sum(logs.MachineHours) MachineHours,
        (CASE When IsMachineCostInvolve = 1  then (MachineCostPerHour * sum(MachineHours)) else null END) as MachineCost,
        (CASE  WHEN IsResourceCostInvolve = 1 THEN 'Yes' WHEN IsResourceCostInvolve = 0 THEN 'NO' END) as IsResourceCostInvolve, 
        rcost.HourlyResourceCost,
        (CASE When IsResourceCostInvolve = 1  then ( rcost.HourlyResourceCost * sum(LoggedHours)) else null END) as TotalResourceCost,
        sum(logs.LoggedHours) TotalHrs ,
        ROW_NUMBER() OVER (ORDER BY prog.ProgramID ) AS Row


    from [dbo].defProgramMaster prog 
    left outer join datTaskLogTransactionDetails logs 
    ON prog.ProgramID = logs.ProgramID 
    inner JOIN datTaskLogMaster logmaster 
    ON logmaster.TaskLogID = logs.TaskLogID 
    Left JOIN defTaskMaster taskmaster 
    ON  taskmaster.TaskID= logs.TaskID 
    left outer JOIN defResourceCosts rcost 
    on  rcost.EmployeeCode =logmaster.EmployeeCode and rcost.DepartmentID= logmaster.DepartmentID

    WHERE LogDate BETWEEN  convert(varchar(10), @InFromDate, 23) AND convert(varchar(10), @InToDate, 23)
    and IsBillable = ( CASE WHEN @InBillableType IS NULL THEN IsBillable ELSE @InBillableType  END) 
    and LogStatus = ( CASE WHEN @InLogStatus IS NULL THEN LogStatus ELSE @InLogStatus  END) 
    and logmaster.EmployeeDomainName  IN ( (SELECT Item from [dbo].[SplitString](ISNULL(@InEmployeeDomainName,EmployeeDomainName),',')))
    and logmaster.ManagerName IN (SELECT Item from [dbo].[SplitString](ISNULL(@InManagerName,ManagerName),','))
    and logs.TaskID IN (SELECT Item from [dbo].[SplitString](ISNULL(@InTaskID,logs.TaskID),','))
    and logs.ProgramID IN (SELECT Item  from [dbo].[SplitString](ISNULL(@InProgramID,logs.ProgramID),','))
    and logmaster.DepartmentID IN (SELECT Item from [dbo].[SplitString](ISNULL(@InDepartmentID,logmaster.DepartmentID),','))

    group by 
    prog.ProgramID,
    prog.ProgramName,
    prog.ProgramOwner,
    prog.ProductFamily,
    CostCenter,
    DepartmentName,
    logmaster.EmployeeCode,
    logmaster.EmployeeName,
    logmaster.EmployeeDomainName,
    ManagerName,
    TaskName,
    IsBillable,
    IsMachineCostInvolve,
    MachineCostPerHour,
    IsResourceCostInvolve,
    HourlyResourceCost,
    IsMachineCostInvolve,
    IsResourceCostInvolve
    )

    insert into #Panging_Data_List 
    SELECT 
        ProgramID,  ProgramName,    
case when ProgramOwner ='1' then NULL else ProgramOwner end as ProgramOwner,    
case when ProductFamily ='1' then NULL else ProductFamily end as ProductFamily, 
case when CostCenter ='1' then NULL else CostCenter end as CostCenter,
    DepartmentName, EmployeeCode,
    EmployeeName,   EmployeeDomainName, ManagerName,    TaskName,   IsBillable, IsMachineCostInvolve,
        MachineCostPerHour, MachineHours, MachineCost,  IsResourceCostInvolve,  HourlyResourceCost, TotalResourceCost,
            TotalHrs,   ROW_NUMBER() OVER (ORDER BY ProgramID ) AS Row
       FROM CTE 
    WHERE   (ProgramOwner IN (SELECT Item from [dbo].[SplitString](ISNULL(@InProgramOwner,ProgramOwner),',')) 
    AND ProductFamily IN (SELECT Item from [dbo].[SplitString](ISNULL(@InProductFamily,ProductFamily),','))
    AND  CostCenter IN (SELECT Item from [dbo].[SplitString](ISNULL(@InCostCenter,CostCenter),',')))


select * from #Panging_Data_List 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...