Friday, April 24, 2015

Convert rows to columns

>> I have a table with birth_date and test results, I am trying to pull the data from the table and convert rows columns, below is the table I am using. <<

Good SQL programmers would do this with a report writer, not with a query. You also have no key, so this is not a table at all! You also have no idea how to use ISO-11179 for data elements test_names, constraints or proper data types. A birth_date is a property of a patient, not a test. Want to include his shoe size, too? 

What is “id”? A patient? A magical generic thing from physical storage? If it is really an identifier, then it cannot be a numeric value! Basic data modeling defines this. 

CREATE TABLE Test_Results
(patient_id CHAR(10) NOT NULL
  REFERENCES Patients (patient_id),
 test_name VARCHAR(10) NOT NULL, 
 test_seq INTEGER NOT NULL
  CHECK(test_seq BETWEEN 1 and 5)
 test_results INTETGER NOT NULL
 CHECK(test_results >= 0),
 PRIMARY KEY (patient_id, test_name, test_seq)
);

Until we get valid DDL, it is not worth guessing at DML. 

--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking in Sets / Trees and Hierarchies in SQL

No comments:

Post a Comment