我在获取 table 中的 sql 数据时遇到问题。我得到不正确的值
I'm having problems getting sql data in a table to pivot. I'm getting incorrect values
我需要转换以下数据:
还有更多数据,但这是一个很好的表示。有几个样本。结果列是派生字段。
我试过了pivot (max(results) for question in [OFFSET DIRECTION],[OFFSET DISTANCE],[REFERENCE LINE],[STATION NUMBER],[THICKNESS]
我得到了旋转数据,第一组行(每行都有一个新样本)有很好的数据,但从 scmn(样本编号)开始是从其他样本中提取数据。我尝试了各种 row_number() over (partition by sample, scmn order by sample, scmn) as [control]
但没有任何效果。
我需要的是:
[
我在这上面花了很多天,现在碰壁了。任何帮助将不胜感激。
示例代码如下:
drop table if exists smpl_rslt;
CREATE TABLE SMPL_RSLT
([SAMPLE] INT, QUESTION VARCHAR(100),VAL_NUM decimal(13,5), VAL_TXT VARCHAR(10),TST_STEP INT, SCMN INT)
INSERT INTO SMPL_RSLT VALUES
(732171,'Offset Direction',null, 'L',11,1),
(732171,'Offset Direction', null, 'L', 11, 2),
(732171,'Offset Direction',null,'L', 11,3),
(732171,'Offset Distance', 0.0000000, null, 13,1),
(732171,'Offset Distance', 0.0000000, null, 13,2),
(732171,'Offset Distance', 0.0000000, null, 13,3),
(732171,'Refence Line', null,'Centerline', 10,1),
(732171,'Refence Line', null,'Centerline', 10,2),
(732171,'Refence Line', null,'Centerline', 10,3),
(732171,'Station Number', null,'101+00', 5,1),
(732171,'Station Number', null,'101+05', 5,2),
(732171,'Station Number', null,'101+10', 5,3),
(732171,'Thickness', 6.500000,null, 14,1),
(732171,'Thickness', 6.500000,null, 14,2),
(732171,'Thickness', 6.500000,null, 14,3),
(732172,'Offset Direction',null, 'R',11,1),
(732172,'Offset Direction', null, 'R', 11, 2),
(732172,'Offset Direction',null,'R', 11,3),
(732172,'Offset Distance', 0.0000000, null, 13,1),
(732172,'Offset Distance', 0.0000000, null, 13,2),
(732172,'Offset Distance', 0.0000000, null, 13,3),
(732172,'Refence Line', null,'Right Edge', 10,1),
(732172,'Refence Line', null,'Right Edge', 10,2),
(732172,'Refence Line', null,'Right Edge', 10,3),
(732172,'Station Number', null,'210+00', 5,1),
(732172,'Station Number', null,'210+00', 5,2),
(732172,'Station Number', null,'210+00', 5,3),
(732172,'Thickness', 10.500000,null, 14,1),
(732172,'Thickness', 10.200000,null, 14,2),
(732172,'Thickness', 10.000000,null, 14,3);
select * from SMPL_RSLT
use test;
select [sample],[station number], [REFERENCE LINE],[OFFSET DIRECTION], [OFFSET DISTANCE],[THICKNESS]
from (
SELECT [sample],question,IIF(COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT) = '','TEST IN PROGRESS',COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT))AS RESULT
,row_number() over (partition by [sample],question order BY [sample], question) spcmn
from SMPL_RSLT) t
pivot (max(result) for question in
([station number], [REFERENCE LINE],[OFFSET DIRECTION], [OFFSET DISTANCE],[THICKNESS])) p
参考线未显示值
您为 PIVOT 使用了错误的列值,导致您的引用为 NULL。在 PIVOTS 中使用列名时,请确保复制并粘贴它们。这就是这里的问题。错误是 [REFENCE LINE] 有拼写错误。避免这种情况,在 PIVOTS 中使用它们时复制并粘贴列值作为处理行值的 PIVOTS。
第一个没有块列名称
select [sample],[Station Number], [Refence Line],[Offset Direction], [Offset Distance] ,[Thickness]
from (
SELECT
[sample]
,question,IIF(COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT) = '','TEST IN PROGRESS',COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT))AS RESULT
,row_number() over (partition by [sample],question order BY [sample], question) spcmn
from SMPL_RSLT
) t
pivot
(
max(result)
for question in ([Station Number], [Refence Line],[Offset Direction], [Offset Distance] ,[Thickness])
) p
ORDER BY P.[Refence Line]
如果您需要在查询中获取大写列名称。
第 2 个带有块列名称
select [sample],[Station Number], [Refence Line],[Offset Direction], [Offset Distance] ,[Thickness]
from (
SELECT
[sample]
,question AS [question]
,IIF(COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT) = '','TEST IN PROGRESS',COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT))AS RESULT
,row_number() over (partition by [sample],question order BY [sample], question) spcmn
from SMPL_RSLT
) t
pivot
(
max(result)
for question in ([STATION NUMBER], [REFENCE LINE],[OFFSET DIRECTION], [OFFSET DISTANCE] ,[THICKNESS])
) p
ORDER BY P.[Refence Line]
我需要转换以下数据:
还有更多数据,但这是一个很好的表示。有几个样本。结果列是派生字段。
我试过了pivot (max(results) for question in [OFFSET DIRECTION],[OFFSET DISTANCE],[REFERENCE LINE],[STATION NUMBER],[THICKNESS]
我得到了旋转数据,第一组行(每行都有一个新样本)有很好的数据,但从 scmn(样本编号)开始是从其他样本中提取数据。我尝试了各种 row_number() over (partition by sample, scmn order by sample, scmn) as [control]
但没有任何效果。
我需要的是:
[
我在这上面花了很多天,现在碰壁了。任何帮助将不胜感激。
示例代码如下:
drop table if exists smpl_rslt;
CREATE TABLE SMPL_RSLT
([SAMPLE] INT, QUESTION VARCHAR(100),VAL_NUM decimal(13,5), VAL_TXT VARCHAR(10),TST_STEP INT, SCMN INT)
INSERT INTO SMPL_RSLT VALUES
(732171,'Offset Direction',null, 'L',11,1),
(732171,'Offset Direction', null, 'L', 11, 2),
(732171,'Offset Direction',null,'L', 11,3),
(732171,'Offset Distance', 0.0000000, null, 13,1),
(732171,'Offset Distance', 0.0000000, null, 13,2),
(732171,'Offset Distance', 0.0000000, null, 13,3),
(732171,'Refence Line', null,'Centerline', 10,1),
(732171,'Refence Line', null,'Centerline', 10,2),
(732171,'Refence Line', null,'Centerline', 10,3),
(732171,'Station Number', null,'101+00', 5,1),
(732171,'Station Number', null,'101+05', 5,2),
(732171,'Station Number', null,'101+10', 5,3),
(732171,'Thickness', 6.500000,null, 14,1),
(732171,'Thickness', 6.500000,null, 14,2),
(732171,'Thickness', 6.500000,null, 14,3),
(732172,'Offset Direction',null, 'R',11,1),
(732172,'Offset Direction', null, 'R', 11, 2),
(732172,'Offset Direction',null,'R', 11,3),
(732172,'Offset Distance', 0.0000000, null, 13,1),
(732172,'Offset Distance', 0.0000000, null, 13,2),
(732172,'Offset Distance', 0.0000000, null, 13,3),
(732172,'Refence Line', null,'Right Edge', 10,1),
(732172,'Refence Line', null,'Right Edge', 10,2),
(732172,'Refence Line', null,'Right Edge', 10,3),
(732172,'Station Number', null,'210+00', 5,1),
(732172,'Station Number', null,'210+00', 5,2),
(732172,'Station Number', null,'210+00', 5,3),
(732172,'Thickness', 10.500000,null, 14,1),
(732172,'Thickness', 10.200000,null, 14,2),
(732172,'Thickness', 10.000000,null, 14,3);
select * from SMPL_RSLT
use test;
select [sample],[station number], [REFERENCE LINE],[OFFSET DIRECTION], [OFFSET DISTANCE],[THICKNESS]
from (
SELECT [sample],question,IIF(COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT) = '','TEST IN PROGRESS',COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT))AS RESULT
,row_number() over (partition by [sample],question order BY [sample], question) spcmn
from SMPL_RSLT) t
pivot (max(result) for question in
([station number], [REFERENCE LINE],[OFFSET DIRECTION], [OFFSET DISTANCE],[THICKNESS])) p
参考线未显示值
您为 PIVOT 使用了错误的列值,导致您的引用为 NULL。在 PIVOTS 中使用列名时,请确保复制并粘贴它们。这就是这里的问题。错误是 [REFENCE LINE] 有拼写错误。避免这种情况,在 PIVOTS 中使用它们时复制并粘贴列值作为处理行值的 PIVOTS。
第一个没有块列名称
select [sample],[Station Number], [Refence Line],[Offset Direction], [Offset Distance] ,[Thickness]
from (
SELECT
[sample]
,question,IIF(COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT) = '','TEST IN PROGRESS',COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT))AS RESULT
,row_number() over (partition by [sample],question order BY [sample], question) spcmn
from SMPL_RSLT
) t
pivot
(
max(result)
for question in ([Station Number], [Refence Line],[Offset Direction], [Offset Distance] ,[Thickness])
) p
ORDER BY P.[Refence Line]
如果您需要在查询中获取大写列名称。
第 2 个带有块列名称
select [sample],[Station Number], [Refence Line],[Offset Direction], [Offset Distance] ,[Thickness]
from (
SELECT
[sample]
,question AS [question]
,IIF(COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT) = '','TEST IN PROGRESS',COALESCE(CAST(VAL_NUM AS VARCHAR(10)), VAL_TXT))AS RESULT
,row_number() over (partition by [sample],question order BY [sample], question) spcmn
from SMPL_RSLT
) t
pivot
(
max(result)
for question in ([STATION NUMBER], [REFENCE LINE],[OFFSET DIRECTION], [OFFSET DISTANCE] ,[THICKNESS])
) p
ORDER BY P.[Refence Line]