iT邦幫忙

0

SQL語法問題,結果=0的不要撈

tlcric 2017-05-05 16:15:2527879 瀏覽

下面這段結果=0的不要撈,該如何修改
declare @RemarkCol as nvarchar(200)
declare @Col as nvarchar(200)
declare @nDecimal float
set @RemarkCol = '0'
declare @Type as nvarchar(200)
declare @TypeCol as nvarchar(200)
select @Col = '' from Corporation where 1=0 set @RemarkCol = '0'
select @nDecimal = CONVERT(decimal(20,10),@RemarkCol)
select @Col = '' from Corporation where 1=0 set @TypeCol = '1'

declare @ItemName as nvarchar(4000)
declare @ItemNameTemp as nvarchar(4000)
declare @ItemName1 as nvarchar(300) --免稅加項
declare @SalaryTaxTypeId1 as nvarchar(30) set @SalaryTaxTypeId1 = '0SalaryItemTaxType_003'
declare @ItemName2 as nvarchar(300) --應稅加項
declare @SalaryTaxTypeId2 as nvarchar(30) set @SalaryTaxTypeId2 = '0SalaryItemTaxType_001'
declare @ItemName3 as nvarchar(300) --免稅扣項
declare @SalaryTaxTypeId3 as nvarchar(30) set @SalaryTaxTypeId3 = '1SalaryItemTaxType_003'
declare @ItemName4 as nvarchar(300) --應稅扣項
declare @SalaryTaxTypeId4 as nvarchar(30) set @SalaryTaxTypeId4 = '1SalaryItemTaxType_001'
declare @勞保 as int
declare @健保 as int
declare @補充保費 as int
declare @所得稅 as int
declare @自提 as int
declare @年月 as nvarchar(20)

select @年月 = cast(min(SalaryMonth.SalaryYear100+SalaryMonth.Month) as nvarchar(6))+' - '+cast(max(SalaryMonth.SalaryYear100+SalaryMonth.Month) as nvarchar(6)) from SalaryMonth where (0=0)

select top 1 @ItemName =
stuff((
select ','+
case when SalaryItem.SalaryItemTaxTypeId = 'SalaryItemTaxType_002' then '0SalaryItemTaxType_003'+SalaryItem.Name else cast(SalaryItem.ItemCat as nvarchar(1))+SalaryItem.SalaryItemTaxTypeId +SalaryItem.Name end
from SalaryResult
left join SalaryResultDetail on SalaryResult.SalaryResultId = SalaryResultDetail.SalaryResultId
left join SalaryItem on SalaryResultDetail.SalaryItemId = SalaryItem.SalaryItemId
left join SalaryMonth on SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
where SalaryItem.IsSalary =1
and SalaryResultDetail.ItemValue <> 0
--and SalaryItem.SalaryItemTaxTypeId in('SalaryItemTaxType_001','SalaryItemTaxType_003')
and (0=0)
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'
group by SalaryItem.ItemCat ,SalaryItem.SalaryItemTaxTypeId ,SalaryItem.ItemID,SalaryItem.Name
order by SalaryItem.ItemCat,SalaryItem.SalaryItemTaxTypeId desc,SalaryItem.ItemID
--order by case when SalaryItem.SalaryItemTaxTypeId = 'SalaryItemTaxType_002' then '0' else SalaryItem.ItemCat end,case when SalaryItem.SalaryItemTaxTypeId = 'SalaryItemTaxType_002' then 'SalaryItemTaxType_003' else SalaryItem.SalaryItemTaxTypeId end desc,SalaryItem.ItemID
for XML path('') ),1,1,'')
from Corporation
--where Corporation.CorporationId = 'root'

--免稅加項
if(CHARINDEX(@SalaryTaxTypeId1,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId1,@ItemName)+LEN(@SalaryTaxTypeId1),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId1,@ItemName)+LEN(@SalaryTaxTypeId1)),@SalaryTaxTypeId1,'')
set @ItemName1 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName1+',','') end
end
else set @ItemName1 = ''

--應稅加項
if(CHARINDEX(@SalaryTaxTypeId2,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId2,@ItemName)+LEN(@SalaryTaxTypeId2),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId2,@ItemName)+LEN(@SalaryTaxTypeId2)),@SalaryTaxTypeId2,'')
set @ItemName2 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName2+',','') end
end
else set @ItemName2 = ''

--免稅扣項
if(CHARINDEX(@SalaryTaxTypeId3,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId3,@ItemName)+LEN(@SalaryTaxTypeId3),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId3,@ItemName)+LEN(@SalaryTaxTypeId3)),@SalaryTaxTypeId3,'')
set @ItemName3 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName3+',','') end
end
else set @ItemName3 = ''

--應稅扣項
if(CHARINDEX(@SalaryTaxTypeId4,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId4,@ItemName)+LEN(@SalaryTaxTypeId4),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId4,@ItemName)+LEN(@SalaryTaxTypeId4)),@SalaryTaxTypeId4,'')
set @ItemName4 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName4+',','') end
end
else set @ItemName4 = ''

--勞健稅自提
select @勞保 = sum(SalaryResult.LabourEmpFee) ,@健保 = SUM(SalaryResult.HealthFee) ,
@所得稅 = sum(SalaryResult.EmpTax), @自提 = sum(SalaryResult.PensionEmpFee),
@補充保費 = SUM(SalaryResult.Emp2ndNHI)
from SalaryResult,SalaryMonth
where SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
and (0=0)
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'

if @勞保 >0 set @ItemName3 = @ItemName3+',勞保費'
if @健保 >0 set @ItemName3 = @ItemName3 +',健保費'
if @補充保費 >0 set @ItemName3 = @ItemName3 +',補充保費'
if @所得稅 >0 set @ItemName3 = @ItemName3 +',所得稅'
if @自提 >0 set @ItemName4 = @ItemName4 +',自提'
if len(@ItemName3) >0 and LEFT(@ItemName3,1) =',' set @ItemName3 = right(@ItemName3,LEN(@ItemName3)-1)
if len(@ItemName4) >0 and LEFT(@ItemName4,1) =',' set @ItemName4 = right(@ItemName4,LEN(@ItemName4)-1)

select distinct cast(SalaryMonth.SalaryYear*100+SalaryMonth.Month as nvarchar(6)) 薪資月, Employee.Code 工號,Employee.CnName 姓名 ,
--Department.Code 部門代號,Department.Name 部門名稱,
部門代號 =case @TypeCol when '1' then Department.Code when '2' then CostCenter.Code when '3' then dept2.Code end,
部門名稱 =case @TypeCol when '1' then Department.Name when '2' then CostCenter.Name when '3' then dept2.Code end,
@ItemName2 應稅加項,@ItemName1 免稅加項,@ItemName4 應稅扣項,@ItemName3 免稅扣項,
',,,'+stuff((
select ',,,' +item.Name+'計算值'+replace(CONVERT(nvarchar(20),cast(sum(detail.ItemValue) as money),1),'.00','')
from SalaryResult result,SalaryResultDetail detail,SalaryItem item
where result.SalaryResultId = detail.SalaryResultId
and result.SalaryMonthId = SalaryMonth.SalaryMonthId
and result.EmployeeId = Employee.EmployeeId
and detail.SalaryItemId = item.SalaryItemId
and item.IsSalary = 1
and detail.ItemValue <> 0
and detail.ItemValue <999999
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'
group by item.ItemID,item.Name,result.EmployeeId,result.SalaryMonthId
order by item.ItemID for xml path('')),1,3,'') +
(
select
isnull(case when SUM(result.LabourEmpFee) >0 then ',,,勞保費計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.LabourEmpFee) as money),1),'.00','') end,'') +
isnull(case when SUM(result.HealthFee) >0 then ',,,健保費計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.HealthFee) as money),1),'.00','') end,'') +
isnull(case when SUM(result.Emp2ndNHI) >0 then ',,,補充保費計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.Emp2ndNHI) as money),1),'.00','') end,'') +
isnull(case when SUM(result.EmpTax) >0 then ',,,所得稅計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.EmpTax) as money),1),'.00','') end,'') +
isnull(case when SUM(result.PensionEmpFee) >0 then ',,,自提計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.PensionEmpFee) as money),1),'.00','') end,'')
from SalaryResult result
where result.SalaryMonthId = SalaryMonth.SalaryMonthId
and result.EmployeeId = Employee.EmployeeId
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'

	) 個人計算項 ,
	'應發合計' 應發合計名,'應扣合計' 應扣合計名,'實發' 實發合計名,
	@年月 年月,
	Corporation.Name 公司別,@nDecimal 小數位數 from SalaryResult 

left join Employee on SalaryResult.EmployeeId = Employee.EmployeeId
left join Department on Employee.DepartmentId = Department.DepartmentId
left join Corporation on Employee.CorporationId = Corporation.CorporationId
left join SalaryMonth on SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
left join CodeInfo on employee.FactoryId = CodeInfo.CodeInfoId
left join CostCenter on employee.CostCenterId = CostCenter.CostCenterId
left join Department dept2 on Department.DirectDeptId = dept2.DepartmentId where (0=0)
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d' AND (1=1) order by Employee.Code,cast(SalaryMonth.SalaryYear*100+SalaryMonth.Month as nvarchar(6)) ;


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言