【发布时间】:2020-02-11 18:40:38
【问题描述】:
我有以下问题;
我目前有一个存储过程,当我输入 4 个不同的参数时会进行大量计算,然后使用 select 语句输出结果(11 列),如下所示:
此查询的开头如下所示:
ALTER PROCEDURE [AGG].[usp_CalculateIsentropeEfficiency]
(
@InlaatdrukE INT,
@InlaattempC INT,
@UitlaatdrukE INT,
@UitlaattempC INT
)
AS
/*-------------------------- Omrekenen van druk naar absolute druk en temperatuur naar de eenheid Kelvin. --------------------------*/
DECLARE @InlaatdrukA DECIMAL(20,15)
DECLARE @InlaattempK DECIMAL(20,15)
DECLARE @UitlaatdrukA DECIMAL(20,15)
DECLARE @UitlaattempK DECIMAL(20,15)
SET @InlaatdrukA = @InlaatdrukE + 1.01325
SET @InlaattempK = @InlaattempC + 273.15
SET @uitlaatdrukA = @UitlaatdrukE + 1.01325
SET @UitlaattempK = @UitlaattempC + 273.15
在这部分之后,我声明并设置了更多变量。有些在查找表中查找值,有些只是简单的计算。
现在的想法是,这些不同的参数应该根据我使用枢轴执行的另一个选择语句来填充。
这个数据透视表的输出如下所示:
其中PI,PU,TI 和TU 是存储过程的输入。
我的问题是我不能为每一行运行存储过程,所以我想把 Pivot Select 放在存储过程中以创建一个更大的表。这也不起作用,因为我不能使用数据透视表作为我进行计算的变量的输入。
所需的输出需要是数据透视表 + 计算。像这样:
TimeKey Component PI PU TI TU Q Row# InlaatdrukA InlaattempK Tuit_s等
我希望你们中的任何人都知道如何做到最好,因为我相信存储过程可能不是最佳选择。
感谢您分享您的专业知识。
正如 cmets 所问的,这里有更多带有逻辑的 SQL 代码: 在程序中:
--Inlaat
DECLARE @Lookup_inlaat_templower INT
DECLARE @Lookup_inlaat_druklower INT
DECLARE @Lookup_inlaat_temphigher INT
DECLARE @Lookup_inlaat_drukhigher INT
SET @Lookup_inlaat_templower = (SELECT MAX(ref.Temperature) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature <= @InlaattempK)
SET @Lookup_inlaat_druklower = (SELECT MAX(ref.Pressure) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Pressure <= @InlaatdrukA)
SET @Lookup_inlaat_temphigher = (SELECT MIN(ref.Temperature) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature >= @InlaattempK)
SET @Lookup_inlaat_drukhigher = (SELECT MIN(ref.Pressure) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Pressure >= @InlaatdrukA)
--Uitlaat
DECLARE @Lookup_uitlaat_templower INT
DECLARE @Lookup_uitlaat_druklower INT
DECLARE @Lookup_uitlaat_temphigher INT
DECLARE @Lookup_uitlaat_drukhigher INT
SET @Lookup_uitlaat_templower = (SELECT MAX(ref.Temperature) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature <= @uitlaattempK)
SET @Lookup_uitlaat_druklower = (SELECT MAX(ref.Pressure) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Pressure <= @uitlaatdrukA)
SET @Lookup_uitlaat_temphigher = (SELECT MIN(ref.Temperature) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature >= @uitlaattempK)
SET @Lookup_uitlaat_drukhigher = (SELECT MIN(ref.Pressure) FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Pressure >= @uitlaatdrukA)
/*-------------------------- Interpolatie van de gemeten inlaat temperatuur en druk. --------------------------*/
DECLARE @Isentrope_inlaat_lowertemp_lowerdruk DECIMAL(19,17)
DECLARE @Isentrope_inlaat_lowertemp_higherdruk DECIMAL(19,17)
DECLARE @Isentrope_inlaat_highertemp_lowerdruk DECIMAL(19,17)
DECLARE @Isentrope_inlaat_highertemp_higherdruk DECIMAL(19,17)
SET @Isentrope_inlaat_lowertemp_lowerdruk = (SELECT ref.IsentropeCoefficient FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature = @lookup_inlaat_templower AND ref.Pressure = @lookup_inlaat_druklower)
SET @Isentrope_inlaat_lowertemp_higherdruk = (SELECT ref.IsentropeCoefficient FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature = @lookup_inlaat_templower AND ref.Pressure = @lookup_inlaat_drukhigher)
SET @Isentrope_inlaat_highertemp_lowerdruk = (SELECT ref.IsentropeCoefficient FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature = @lookup_inlaat_temphigher AND ref.Pressure = @lookup_inlaat_druklower)
SET @Isentrope_inlaat_highertemp_higherdruk = (SELECT ref.IsentropeCoefficient FROM AGG.tblRefIsentropeEfficiency ref WHERE ref.Temperature = @lookup_inlaat_temphigher AND ref.Pressure = @lookup_inlaat_drukhigher)
DECLARE @IsentropeInlaatG1 DECIMAL(19,17)
DECLARE @IsentropeInlaatG2 DECIMAL(19,17)
SET @IsentropeInlaatG1 = ((@lookup_inlaat_druklower + @Deltax - @InlaatdrukA) * @Isentrope_inlaat_lowertemp_lowerdruk + (@InlaatdrukA - @lookup_inlaat_druklower) * @isentrope_inlaat_lowertemp_higherdruk) / @deltax
SET @IsentropeInlaatG2 = ((@lookup_inlaat_druklower + @Deltax - @InlaatdrukA) * @Isentrope_inlaat_highertemp_lowerdruk + (@InlaatdrukA - @lookup_inlaat_druklower) * @isentrope_inlaat_highertemp_higherdruk) / @deltax
这只是设置变量并使用它们进行计算。 (这仍然不是一切,但应该更深入地了解我如何计算字段。存储过程 atm 的结尾只是:
SELECT InlaatdrukA = @InlaatdrukA,
InlaattempK = @InlaattempK,
UitlaatdrukA = @UitlaatdrukA,
UitlaattempK = @UitlaattempK,
Interpolate_BL = @InlaatInterpolate_BL,
Tuit_s = @Tuit_s,
H1_Interpolate_BL = @H1_Interpolate_BL,
H2_Interpolate_BL = @H2_Interpolate_BL,
H2s_Interpolate_BL = @H2s_Interpolate_BL,
IsentropeEfficiency = @IsentropeEfficiency,
IsentropeInefficiency = @IsentropeInefficiency
整个 Pivot SQL 查询是这样的:
with cte_src as (
SELECT
VH.Value,
VH.TimeKey,
RIGHT(M.MeasurementOriginalName, CHARINDEX('.', REVERSE(M.MeasurementOriginalName))-1) AS Type,
SUBSTRING(M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, 1))+1)+1)+1,charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, 1))+1)+1)+1) - charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, charindex('.', M.MeasurementOriginalName, 1))+1)+1)-1) as Component
FROM [MSTR].[tblDimMeasurement] M
INNER JOIN [AGG].[tblTmpFactMeasurementAnalogueValueHour] VH ON VH.MeasurementKey = M.MeasurementKey
WHERE M.MeasurementOriginalName LIKE ('%A-414-C014%')
and RIGHT(M.MeasurementOriginalName,2) IN ('PU','PI','TI','TU','.Q') ),
cte_piv as(
select *, ROW_NUMBER() OVER (ORDER BY TimeKey) as Row# from cte_src
PIVOT( MAX(Value) FOR TYPE IN ([PI],[PU],[TI],[TU],[Q]) )Pivotname
)
Select * from cte_piv
在这里我选择我需要的字段,并使用 charindex 确保我得到正确的字符串部分。
然后旋转它,以便我得到PI, PU 等的结果,如上所示。
【问题讨论】:
-
消耗品样本数据和预期结果,以及计算背后的逻辑将帮助我们为您提供帮助。您实际上并没有给我们任何执行这里逻辑的 SQL,也没有给我们任何可以使用的数据;恐怕现在我们无法回答。
-
当我在存储过程中创建 Pivot 时,它开始工作了。然后使用临时表并逐行填充它们。然后选择临时表:)
标签: sql-server tsql