Thursday, 6 September 2012
#26 Daftar Code Error di SQL Server
SELECT * FROM sysmessages where msglangid=1033
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
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
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)
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)
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)
Subscribe to:
Posts (Atom)