MS Server 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
Блок повторяется 6 раз для каждого TrialSampleID, давая 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