Tuesday, 28 August 2012

#25 Split Data

Misalnya kita punya data:
123,345,678
234,567,789

dan kita ingin menampilkan data
123
345
678
234
567
789

Berikut script sederhana untuk men-split data.

select requester, b.Value approver, settingapproval_step from (
    select requestby_posid requester, approvedby_posid approver, settingapproval_step
    FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=HRMSDB;User ID=developer;Password=d3v3l0p3r').anugerah_prod.dbo.vw_request_approval a
    where requestapproval_id=3 and requestby_posid=15351 and requestby_posid not in
    (
        select position_id FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=HRMSDB;User ID=developer;Password=d3v3l0p3r').anugerah_prod.dbo.thrmposition where position_parentpath like '%11361%'
    ) and approvedby_posid not in( cast (14120 as varchar(100)) )--HR Officer   
) a
    cross apply
    (
        select  *  from  dbo.fn_split(a.approver, ',')
    ) b
    inner join (select * from OPENDATASOURCE('SQLOLEDB', 'Data Source=HRMSDB;User ID=developer;Password=d3v3l0p3r').anugerah_prod.dbo.vw_employee_info) c on (b.Value=c.position_id)
order by settingapproval_step

#24 Membuat Function di SQL Server

USE [ssreport]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_Split]    Script Date: 08/29/2012 10:54:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER function [dbo].[fn_Split](
@String nvarchar (4000),
@Delimiter nvarchar (10)
)
returns @ValueTable table ([Value] nvarchar(4000))
begin
declare @NextString nvarchar(4000)
declare @Pos int
declare @NextPos int
declare @CommaCheck nvarchar(1)

--Initialize
set @NextString = ''
set @CommaCheck = right(@String,1)

--Check for trailing Comma, if not exists, INSERT
if (@CommaCheck <> @Delimiter )
set @String = @String + @Delimiter

--Get position of first Comma
set @Pos = charindex(@Delimiter,@String)
set @NextPos = 1

--Loop while there is still a comma in the String of levels
while (@pos <> 0)
begin
set @NextString = substring(@String,1,@Pos - 1)
insert into @ValueTable ( [Value]) Values (@NextString)
set @String = substring(@String,@pos +1,len(@String))
set @NextPos = @Pos
set @pos = charindex(@Delimiter,@String)
end

return
end

GO


Thursday, 9 August 2012

#23 Penggunaan Case pada kondisi

select a.budgetid, budget, b.totalcost, amount,
case when c.budgetid is null then b.totalcost else b.totalcost+amount end x
from vbudgetcapex a
inner join (
    select budgetid, SUM(totalcost) totalcost from trcapexdetail    a
    inner join TrCapexHeader b on (a.capexid=b.capexid)
    where a.capexid=85
    group by budgetid
) b on (a.budgetid=b.budgetid)
left join [vcapex_pemakaianbudget] c on (a.budgetid=c.budgetid)
where budget < (case when c.budgetid is null then b.totalcost else b.totalcost+amount end)
Friday, 3 August 2012

#22 Penggunaan Multi Join dan Subquery di sql server

create view vbudgetcapex as
select a.budgetid, YEAR, month, assetclassid, assetclass, costcenter, allocationtypeid, allocationtype, c.price, pic, company, a.description,
qty_ori, isnull(qty_pemakaian,0) qty_pemakaian, /*qty_ori-qty_pemakaian qty,*/ budget, cast(budget/c.price as int) qty, isnull(totalcost,0) totalcost, budget - isnull(totalcost,0) amount, isnull(costsettlement,0) costsettlement
from (
    select a.budgetid, YEAR, month, a.assetclass assetclassid, c.description assetclass, costcenter, a.allocationtype allocationtypeid, d.allocationtype, pic, company, a.description, max(qty) qty_ori,
    SUM(amount) budget
    from trbudgetcapex a
    inner join MAssetClass c on (a.assetclass=c.assetclass)
    left join MAllocationType d on (a.allocationtype=d.allocationtypeid)
    where statusid=3 --and a.budgetid=1418
    group by a.budgetid, YEAR, month, a.assetclass, c.description, costcenter, a.allocationtype, d.allocationtype, pic, company, a.description
)a left join
(
    select budgetid, sum(qty_pemakaian) qty_pemakaian, SUM(totalcost) totalcost, SUM(costsettlement) costsettlement
    from (
        select a.budgetid, a.qty qty_pemakaian, case when isnull(costsettlement,0)>0 then costsettlement else a.totalcost end totalcost, isnull(costsettlement,0) costsettlement
        from TrCapexDetail a
        left join TrCapexSettlement b on (a.id=b.id)
        inner join TrCapexHeader c on (a.capexid=c.capexid)
        where statusid <>7
    )x group by budgetid   
) b on (a.budgetid=b.budgetid)
inner join (
    select budgetid, price from (
        select id, budgetid, price from trbudgetcapex a
    )x
    inner join (select max(id) id from TrBudgetCapex group by budgetid) b on (x.id=b.id)
) c on (a.budgetid=c.budgetid)
Wednesday, 1 August 2012

#21 Union di SQL Server

create view [dbo].[vbudgetcapex_download] as
select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
qty JanuariUnit, e.amount JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0    JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=1 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, qty FebruariUnit, e.amount FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0    JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=2 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, qty MaretUnit, e.amount MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0    JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=3 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, qty AprilUnit, e.amount AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0    JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=4 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, qty MeiUnit, e.amount MeiValue,
0 JuniUnit, 0    JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=5 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
qty JuniUnit, e.amount JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=6 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0 JuniValue, qty JuliUnit, e.amount JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=7 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0 JuniValue, 0 JuliUnit, 0 JuliValue, qty AgustusUnit, e.amount AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=8 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0 JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, qty SeptemberUnit, e.amount SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=9 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0 JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, qty OktoberUnit, e.amount OktoberValue,
0 NovemberUnit, 0 NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=10 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0 JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
qty NovemberUnit, e.amount NovemberValue, 0 DesemberUnit, 0 DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=11 and a.statusid=3

union

select year, a.budgetid, a.assetclass TypeOfRequesitionCode, b.description TypeofRequisitionName, a.description ItemDescription,
c.allocationtype AllocationType, PIC, CostCenter, Company, Price,
0 JanuariUnit, 0 JanuariValue, 0 FebruariUnit, 0 FebruariValue, 0 MaretUnit, 0 MaretValue, 0 AprilUnit, 0 AprilValue, 0 MeiUnit, 0 MeiValue,
0 JuniUnit, 0 JuniValue, 0 JuliUnit, 0 JuliValue, 0 AgustusUnit, 0 AgustusValue, 0 SeptemberUnit, 0 SeptemberValue, 0 OktoberUnit, 0 OktoberValue,
0 NovemberUnit, 0 NovemberValue, qty DesemberUnit, e.amount DesemberValue, a.description Remarks
from TrBudgetCapex a
inner join MAssetClass b on (a.assetclass=b.assetclass)
left join MAllocationType c on (a.allocationtype=c.allocationtypeid)
inner join (select budgetid, MAX(id) id from TrBudgetCapex group by budgetid) d on (a.id=d.id)
inner join (select budgetid, sum(amount) amount from TrBudgetCapex a where statusid=3 group by budgetid) e on (a.budgetid=e.budgetid)
where month=12 and a.statusid=3


GO