无法旋转此数据

Posted

技术标签:

【中文标题】无法旋转此数据【英文标题】:Having trouble pivoting this data 【发布时间】:2019-11-04 14:31:08 【问题描述】:

MS 服务器 2012

以下查询返回正确的数据,但我需要对其进行旋转,但我的尝试失败了,任何 hep 将不胜感激..

查询...

SELECT TrialSampleID, ReferenceReadID, Reference_Antibiotic, CONCAT(Reference_Qualifier, ' ', Reference_MIC, ' ug/ml') AS 'MIC' FROM dbo.ClinicalTrial JOIN dbo.ReferenceDataPointers ON ClinicalTrial.ID = ReferenceDataPointers.ClinicalTrialID JOIN dbo.ReferencePlates ON ReferenceDataPointers.ReferenceSetID = ReferencePlates.ReferenceSetID JOIN dbo.ReferenceReads ON ReferencePlates.ID = ReferenceReads.ReferencePlateID JOIN dbo.ReferenceMICs ON ReferenceReads.ID = ReferenceMICs.ReferenceReadID WHERE TrialSampleID NOT LIKE 'REF-%' AND SpecimenSource LIKE 'Clinical%' AND Reference_ValidAntibiotic = 'True'

结果...

TrialSampleID |ReferenceReadID |Reference_Antibiotic          |MIC          
CC-200001     | 4077           |Amikacin                      | = 8 ug/ml
CC-200001     | 4077           |Ampicillin                    | > 64 ug/ml
CC-200001     | 4077           |Aztreonam                     | > 64 ug/ml
CC-200001     | 4077           |Cefazolin                     | > 16 ug/ml
CC-200001     | 4077           |Cefepime                      | = 64 ug/ml
CC-200001     | 4077           |Ceftazidime                   | > 64 ug/ml
CC-200001     | 4077           |Ceftazidime/Avibactam         | ≤ 2 ug/ml
CC-200001     | 4077           |Ertapenem                     | ≤ 0.125 ug/ml
CC-200001     | 4077           |Gentamicin                    | > 32 ug/ml
CC-200001     | 4077           |Levofloxacin                  | = 8 ug/ml
CC-200001     | 4077           |Meropenem                     | ≤ 0.125 ug/ml
CC-200001     | 4077           |Meropenem/Vaborbactam         | ≤ 0.5 ug/ml
CC-200001     | 4077           |Piperacillin/Tazobactam       | = 128 ug/ml
CC-200001     | 4077           |Trimethoprim/Sulfamethoxazole | > 8 ug/ml

对于每个 TrialSampleID,该块重复 6 次,给出 6 个 MIC

我想要以下形式的数据,但当我对数据进行透视时,MIC 列中的数据为 NULL。

想要的……

TrialSampleID |ReferenceReadID |Reference_Antibiotic          |MIC1             |MIC2               |MIC3
CC-200001     | 4077           |Amikacin                      | = 8 ug/ml       |   = 8 ug/ml       |   = 8 ug/ml
CC-200001     | 4077           |Ampicillin                    | > 64 ug/ml      |   > 64 ug/ml      |   > 64 ug/ml
CC-200001     | 4077           |Aztreonam                     | > 64 ug/ml      |   > 64 ug/ml      |   > 64 ug/ml
CC-200001     | 4077           |Cefazolin                     | > 16 ug/ml      |   > 16 ug/ml      |   > 16 ug/ml
CC-200001     | 4077           |Cefepime                      | = 64 ug/ml      |   = 64 ug/ml      |   = 64 ug/ml
CC-200001     | 4077           |Ceftazidime                   | > 64 ug/ml      |   > 64 ug/ml      |   > 64 ug/ml
CC-200001     | 4077           |Ceftazidime/Avibactam         | ≤ 2 ug/ml       |   ≤ 2 ug/ml       |   ≤ 2 ug/ml
CC-200001     | 4077           |Ertapenem                     | ≤ 0.125 ug/ml   |   ≤ 0.125 ug/ml   |   ≤ 0.125 ug/ml
CC-200001     | 4077           |Gentamicin                    | > 32 ug/ml      |   > 32 ug/ml      |   > 32 ug/ml
CC-200001     | 4077           |Levofloxacin                  | = 8 ug/ml       |   = 8 ug/ml       |   = 8 ug/ml
CC-200001     | 4077           |Meropenem                     | ≤ 0.125 ug/ml   |   ≤ 0.125 ug/ml   |   ≤ 0.125 ug/ml
CC-200001     | 4077           |Meropenem/Vaborbactam         | ≤ 0.5 ug/ml     |   ≤ 0.5 ug/ml     |   ≤ 0.5 ug/ml
CC-200001     | 4077           |Piperacillin/Tazobactam       | = 128 ug/ml     |   = 128 ug/ml     |   = 128 ug/ml
CC-200001     | 4077           |Trimethoprim/Sulfamethoxazole | > 8 ug/ml       |   > 8 ug/ml       |   > 8 ug/ml

试过了……

PIVOT(  
    MIN(ReferenceReadID)
    FOR MIC IN (
                MIC1,
                MIC2,
                MIC3,
                MIC4,
                MIC5,
                MIC6
                )   
    ) pivoted

【问题讨论】:

【参考方案1】:

这解决了它,感谢 D-Shih 的洞察力...

SELECT  TrialSampleID,
        Reference_Antibiotic,
        MAX(CASE WHEN rn = 1 THEN MIC END) 'MIC1',
        MAX(CASE WHEN rn = 2 THEN MIC END) 'MIC2',
        MAX(CASE WHEN rn = 3 THEN MIC END) 'MIC3',
        MAX(CASE WHEN rn = 4 THEN MIC END) 'MIC4',
        MAX(CASE WHEN rn = 5 THEN MIC END) 'MIC5',
        MAX(CASE WHEN rn = 6 THEN MIC END) 'MIC6'
FROM (
        SELECT 
            TrialSampleID,
            Reference_Antibiotic,
            CONCAT(Reference_Qualifier, ' ', Reference_MIC, ' ug/ml') AS 'MIC',
            ROW_NUMBER() OVER(PARTITION BY TrialSampleID, Reference_Antibiotic ORDER BY TrialSampleID, Reference_Antibiotic) rn
        FROM 
            dbo.ClinicalTrial
            JOIN dbo.ReferenceDataPointers ON ClinicalTrial.ID = ReferenceDataPointers.ClinicalTrialID
            JOIN dbo.ReferencePlates ON ReferenceDataPointers.ReferenceSetID = ReferencePlates.ReferenceSetID
            JOIN dbo.ReferenceReads ON ReferencePlates.ID = ReferenceReads.ReferencePlateID
            JOIN dbo.ReferenceMICs ON ReferenceReads.ID = ReferenceMICs.ReferenceReadID
        WHERE 
            TrialSampleID NOT LIKE 'REF-%' 
            AND SpecimenSource LIKE 'Clinical%'
            AND Reference_ValidAntibiotic = 'True'
    ) t1
GROUP BY TrialSampleID, Reference_Antibiotic
ORDER BY TrialSampleID, Reference_Antibiotic

【讨论】:

以上是关于无法旋转此数据的主要内容,如果未能解决你的问题,请参考以下文章

iOS:图像在保存为 PNG 表示数据后旋转 90 度

forge Viewer-无法旋转下面的标记?

无法在 iPad 上正确旋转翻译后的 UIView

java根据文字旋转图片方法

磁场、旋转矩阵和全局坐标

无法基于两列旋转 C# 数据表