TNM Staging
I am doing my first project. I have some data t_stage is from t0 to t4, n_stage n0 to n3 and m_stage is m0 and m1.
Different combos of these give different results. So t1,n2,m0 is Stage3a, t4,n1,m1 would be stage 4.
What I’m looking to do is for a query to have these specific patterns already in the syntax so when the query is run there’s an extra column specifying the Stage dependent upon the t, n and m numbers that are in the table for each record.
So as above if t is t1, n is n2 and m is 0 there would be an extra column titled Stage with stage 3a for that record and so on for the variations. (See image for combinations).
I just can’t seem to figure out how to create that extra ‘Stage’ column and populate with the answer when the query is run.
I’m probably missing something very obvious. As I say it’s my first project with mySQL. I’m used to MSAccess I keep thinking a lot of the automation will still be there.
I HAVE to keep the separate T,N and M scores for each record within the schema for potential interrogation later.
I’ve looked at DERIVED, UNION, WHERE and HAVING and I just don’t know if I’m on the right track.
CREATE TABLE pathology (
pathology_id VARCHAR(100) NOT NULL PRIMARY KEY,
medical_data_id VARCHAR(100),
grade INT
CONSTRAINT grade CHECK (grade IN (1, 2, 3, 4)),
tumour_weight DECIMAL (3,3) -- in grams
tumour_circumference DECIMAL (2,2) -- in cms
tumour_stage ENUM('t0','t1', 't2', 't3', 't4'),
node_stage ENUM('n0', 'n1', 'n2', 'n3'),
met_stage ENUM('m0', 'm1')
);
2
Answers
You should have the table every possible combination written in your DB. Then, you can join the cases table with the matching rows in the combinations table, using fields T, N and M, which should give you the result you are looking for.
If your cases table name was X and you combinations table was Y, the predicate in you SQL should read somthing similar to this:
According to the table you posted, there are some cases which allow any T or any N. For such cases, you should create some additional OR groups. Let me know if you need more elaboration on this.
If you have a table similar to this:
… you can create a cte (or a table) that defines different combinations of T, N and M stages giving them overall stage (as in your link above). I commented out the last 3 rows because they are distinctive and will be processed by Case expression in the Main SQL code below …
… this cte, when left joined to your_table, should give you the expected overall_stage for every row leaving your individual stages intact …
See the fiddle here.
NOTE:
Case expression is sequential – that is – the first When condition satisfied will return it’s Then value and exit Case. Only issue could be which overall_stage has to be selected if you have a combination of T4-N3-M0 is it Stage 3C or 3B – I put 3C if that is opposite just switch those two rows within the Case expression….
P.S. The best solution would be to create a permanent table with all explicite combinations of the three stages with apropriate overall_stage for each. Then you would’t need Case expression at all – just the join selecting overall_stage from that new table for every joined row in your_table.