Skip to content
Advertisement

List the discharged patients who were earlier admitted but did not undergo any operation

This is my code and everything is correct excepted the average temperature since the results are shown that everyone got the same temperature. exp:student a got 4 times observation of temperature whereas student b only got 1 time of observation. so how can I deal with it?

Advertisement

Answer

This is essentially a complete answer. It shows that writing the SQL more clearly, and with the suggested JOIN / ON form, can more clearly point out when join criteria is missing:

Test case (The new schema and some simple data)

Fiddle to the full test case with full data and both queries for comparison

Updated to allow for patients who have no temperature observations

Notice the missing join criteria in the ON clause related to the observation table. Just specifying the observ_type is not sufficient.

Result with sample data added and with the corrected join criteria:

New result with new schema and a little sample data

Old result with the same data and missing join criteria:

enter image description here

Here’s the partial old query result (from the question) with the full data from the OP

enter image description here

Here’s the new result with full data, with the proposed solution above:

This allows patients which have no temperature observations.

enter image description here

Note: There could be other problems in the logic that the question does not ask about. The missing join criteria of the original SQL certainly does create a mess. This is now cleaned up.

Here’s the kind of detail the question should provide, along with some usable test data that produces the result you don’t expect:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement