动态评估存储在表列中的表达式

时间:2014-01-26 22:32:40

标签: sql sql-server

我有下表

PnlId LineTotalisationId   Designation Totalisation
    1   A   Gross Fees  Formule         A01+A02+A03+A04+A05
    2   A01 GF1         Comptes         B01+B02+B03+B04+B05
    3   A02 GF2         Comptes         C01+C02+C03+C04+C05
    4   A03 GF3         Comptes         99991
    5   A04 GF4         Comptes         99996
    6   A05 GF5         Comptes         999995
    14  B1  Perm            Comptes         12+14+25

我希望得到

如果Designation =总计中的Formule LineTotalisationId为PnlId内容编号的名称,那么我应该为所有Totalisation汇总Totalisation,否则我只需添加行

我尝试以下代码

SELECT Ref.*, 
       CASE 
         WHEN Charindex('+', Ref.totalisation) > 0 THEN '+' 
         WHEN Charindex('/', Ref.totalisation) > 0 THEN '/' 
         WHEN Charindex('*', Ref.totalisation) > 0 THEN '*' 
         WHEN Charindex('*', Ref.totalisation) > 0 THEN '-' 
       END AS Operator 
INTO   ##ttresults 
FROM   [dbo].[pnlreference] Ref 
WHERE  [typetotalisation] = 'Formule' 
       AND totalisation <> '' 
       AND designation NOT LIKE '%[0-9]%' 
       AND designation != '' 

SELECT split.linetotalisationid AS PNLParentId, 
       NULL                     AS Totalisation 
INTO   ##tempresults 
FROM   (SELECT tbl.designation, 
               tbl.linetotalisationid, 
               tbl.typetotalisation, 
               tbl.pnlid, 
               tbl.totalisation, 
               Cast('<t>' 
                    + Replace(tbl.totalisation, tbl.operator, '</t><t>') 
                    + '</t>' AS XML) x, 
               tbl.operator 
        FROM   ##ttresults AS tbl) split 
       CROSS apply x.nodes('/t') t(c) 
       INNER JOIN [dbo].[pnlreference] Ref 
               ON Ref.linetotalisationid = t.c.value('.', 'nvarchar(255)') 
WHERE  Ref.designation LIKE '%[0-9]%' 
        OR Ref.designation = '' 
GROUP  BY split.linetotalisationid ;

上面的代码作为结果返回

PNLParentId
A

有没有办法修改它以获得描述的总计?

3 个答案:

答案 0 :(得分:3)

如果您的示例数据代表您的数据,您可以尝试下面的sql

假设Panel是您的表名,Line字段只有1个字符串

Declare @sql nvarchar(max);


;
set @sql = 
'with cte as
(
 select 
    case
        when patindex(''%[A-Z]%'',Line) > 0
        then
            substring(line,patindex(''%[A-Z]%'',Line),1)
    end as LineId
    ,Totalisation
 from panel
 where line in (' +''''+ (select replace(a.totalisation,'+',''',''') from Panel a where Designation='formule' ) + '''' +')
 union all
 select 
    Line as LineId,
    Totalisation
 from panel 
 where line not in (' +''''+ (select replace(a.totalisation,'+',''',''') from Panel a where Designation='formule' ) + '''' +') 
 and Designation <> ''Formule''
'
+ ')
select 
    distinct 
    p.pnlId, p.Line, p.TotalisationId--, p.Designation ,P.Totalisation
    ,LineId, LTRIM(substring(stuff
        (
            (
             select '' | '' + c2.Totalisation from cte c2 where c.LineId = c2.LineId for xml path('''')
            )
            ,1,0,''''
        ),3,len(stuff
        (
            (
             select '' | '' + c2.Totalisation from cte c2 where c.LineId = c2.LineId for xml path('''')
            )
            ,1,0,''''
        )))
    ) as Totalisation
from cte c 
right join panel p on c.LineId = p.Line
where c.Totalisation is not null
'
;

exec(@sql)

/*
RESULT
pnlId       Line  TotalisationId LineId Totalisation
----------- ----- -------------- ------ --------------------------------------
1           A     Gross Fees     A      99999 | 99998 | 99991 | 99996 | 999995
14          B1    Perm           B1     12+14+25

*/

<强>已更新
使用@roopesh样本数据B1&#39; Formule&#39;

declare @formula nvarchar(max);
;
with cte as
(
select
    distinct 1 as id, p.Totalisation

from 
 panel2 p
 where Designation = 'formule'
) 
select
    distinct @formula =  '''' + Replace(replace(substring(stuff
            (
                (
                 select ',' + c2.Totalisation from cte c2 where c.id = c2.id for xml path('')
                )
                ,1,0,''
         ),2,len(stuff
            (
                (
                 select ',' + c2.Totalisation from cte c2 where c.id = c2.id for xml path('')
                )
                ,1,0,''
         ))),',',''','''),'+',''',''') + ''''
from cte c 

;
Declare @sql nvarchar(max);
;
set @sql = 
'
;with cte as
(
 select 
    case
        when patindex(''%[A-Z]%'',Line) > 0
        then
            substring(line,patindex(''%[A-Z]%'',Line),1)
    end as LineId
    ,Totalisation
 from panel2
 where line in (' + @formula +')
 union all
 select 
    Line as LineId,
    Totalisation
 from panel2 
 where line not in (' + @formula +') 
 and Designation <> ''Formule''
'
+ ')
select 
    distinct 
    p.pnlId, p.Line, p.TotalisationId--, p.Designation ,    p.totalisation
    ,LineId, Case when c.totalisation is null and p.designation=''Formule'' then p.totalisation
    else
        LTRIM(substring(stuff
        (
            (
             select '' | '' + c2.Totalisation from cte c2 where c.LineId = c2.LineId for xml path('''')
            )
            ,1,0,''''
        ),3,len(stuff
        (
            (
             select '' | '' + c2.Totalisation from cte c2 where c.LineId = c2.LineId for xml path('''')
            )
            ,1,0,''''
        )))
    )

    end as Totalisation
from cte c 
right join panel2 p on c.LineId = p.Line
where p.Designation = ''Formule''


'
;

exec(@sql)

答案 1 :(得分:2)

此存储过程可作为您的问题的解决方案。它正在使用游标。可能有一种方法可以删除光标,但直到现在才能使用。所以得到了这个解决方案。

CREATE Procedure [dbo].[spGetResult]
As
Begin
declare @curPNL cursor

declare @pnlid int
declare @Line varchar(10), @TotalisationId varchar(20), @Totalisation varchar(50)
declare @spresult table(PNLId int, Line varchar(10), TotalisationId varchar(20), result varchar(4000));

--declare the cursor
set @curPNL = cursor 
       for select PnlId, Line, TotalisationId, totalisation 
                 from PNLTable where designation = 'Formule'

open @curPNL

Fetch Next From @curPNL into @pnlId, @Line, @TotalisationId, @Totalisation

While @@FETCH_STATUS = 0
Begin

    declare @nsql nvarchar(4000);

    declare @table table(tname varchar(50));
    declare @result varchar(4000)

    delete from @table
    --get the totalisation data for specific column
    set @nsql = 'select totalisation from PNLTable Where Line in (''' + replace(@Totalisation,'+',''',''') + ''')';
    print 'Calling child'

    insert into @table
    exec(@nsql);

    set @result = '';

    if not exists (select 1 from @table)
        Begin
        set @result = replace(@Totalisation,'+','|') 
        End
    else
        Begin
        --get the values of totalisation in a pipe separated string
        select @result = case when @result = '' then '' else @result + '|' end + tname from @table;
        End

    --insert the values in the temporary table
    insert into @spresult(PNLId, Line, TotalisationId, result)
    select @pnlid, @Line, @TotalisationId, @result

    Fetch Next From @curPNL into @pnlId, @Line, @TotalisationId, @Totalisation
End

close @curPNL
deallocate @curPNL

select * from @spresult;
End

虽然桌面结构对我来说不太清楚。但我使用以下脚本来创建表并插入数据。

CREATE TABLE [dbo].[PNLTable](
    [PnlId] [int] NOT NULL,
    [Line] [varchar](10) NULL,
    [TotalisationId] [varchar](20) NULL,
    [Designation] [varchar](20) NULL,
    [Totalisation] [varchar](50) NULL,
PRIMARY KEY CLUSTERED 
(
    [PnlId] ASC
)
)

- 插入数据

INSERT [PNLTable] 
    ([PnlId], [Line], [TotalisationId], [Designation], [Totalisation]) 
    VALUES (1, N'A', N'Gross Fees', N'Formule', N'A01+A02+A03+A04+A05'), (2, N'A01', N'GF1', N'Comptes', N'99999')
   ,(3, N'A02', N'GF2', N'Comptes', N'99998'), (4, N'A03', N'GF3', N'Comptes', N'99991'), (5, N'A04', N'GF4', N'Comptes', N'99996')
   , (6, N'A05', N'GF5', N'Comptes', N'999995'), (14, N'B1', N'Perm', N'Formule', N'12+14+25')

答案 2 :(得分:0)

您必须使用Designation=Formule中的公式生成动态sql查询,而不是使用大型静态查询。然后使用EXEC运行代码。

查询可能是这样的:

SELECT
    (SELECT Totalisation WHERE Line = 'A01')
    +
    (SELECT Totalisation WHERE Line = 'A02')
    +
    (SELECT Totalisation WHERE Line = 'A03')
    +
    (SELECT Totalisation WHERE Line = 'A04')
    +
    (SELECT Totalisation WHERE Line = 'A05')
FROM [dbo].[pnlreference] AS Ref
WHERE Designation = 'Formule'
AND LEFT(Line, 1) = 'A'

可能将公式内容拆分为单个条目和运算符,并将其加载到包含公式(perhabs A01和A02等)和id的临时表中。 然后生成代码:代码可能是这样的:

EXEC 'SELECT
    (SELECT Totalisation WHERE Line = ' + CAST((SELECT Formular FROM #formular WHERE id = 0) AS VARCHAR(MAX)) +')
    ' + CAST((SELECT Operator FROM #formular WHERE id = 1) AS VARCHAR(MAX)) +'
    (SELECT Totalisation WHERE Line = ' + CAST((SELECT Formular FROM #formular WHERE id = 1) AS VARCHAR(MAX)) +')
    ' + CAST((SELECT Operator FROM #formular WHERE id = 2) AS VARCHAR(MAX)) +'
    (SELECT Totalisation WHERE Line = ' + CAST((SELECT Formular FROM #formular WHERE id = 2) AS VARCHAR(MAX)) +')
    ' + CAST((SELECT Operator FROM #formular WHERE id = 3) AS VARCHAR(MAX)) +'
    (SELECT Totalisation WHERE Line = ' + CAST((SELECT Formular FROM #formular WHERE id = 3) AS VARCHAR(MAX)) +')
    ' + CAST((SELECT Operator FROM #formular WHERE id = 4) AS VARCHAR(MAX)) +'
    (SELECT Totalisation WHERE Line = ' + CAST((SELECT Formular FROM #formular WHERE id = 4) AS VARCHAR(MAX)) +')
FROM [dbo].[pnlreference] AS Ref
WHERE Designation = ''Formule''
AND LEFT(Line, 1) = ' + LEFT(CAST((SELECT Formular FROM #formular WHERE id = 0) AS VARCHAR(MAX)), 1);