Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all: why is Operation.StatusID of type Text, when it's filled with values from integer field? The same may be asked about RegionID, but as Region table is not listed here I can't tell, maybe you need Text in that particular case. But having Operation.StatusID as Text is totally unjustified and will give you performance penalty.</p> <p>Nevertheless, there is an old trick, called pivoting but it will work only for a fixed number of columns, in your case - Surveys:</p> <pre><code>SELECT Sample.BuildingID, MAX(Sample.BuildingName_EN) BuildingID, MAX(CASE WHEN Operation.SurveyTypeID=1 THEN Operation.StatusID ELSE " " END) AS Survey1, MAX(CASE WHEN Operation.SurveyTypeID=2 THEN Operation.StatusID ELSE " " END) AS Survey2, MAX(CASE WHEN Operation.SurveyTypeID=3 THEN Operation.StatusID ELSE " " END) AS Survey3 FROM Sample,Operation WHERE Operation.SampleID=Sample.ID GROUP BY Sample.BuildingID </code></pre> <p>Update: To ensure expandability of this solution, you will have to generate this query each time before use with another one:</p> <pre><code>SELECT ' SELECT Sample.BuildingID,MAX(Sample.BuildingName_EN) BuildingID,' UNION ALL SELECT concat(' MAX(CASE WHEN Operation.SurveyTypeID=',SurveyType.ID,' THEN Operation.StatusID ELSE " " END) AS "',SurveyType.LongName,'",') from SurveyType UNION ALL SELECT '0 as dummy FROM Sample,Operation WHERE Operation.SampleID=Sample.ID GROUP BY Sample.BuildingID' </code></pre> <p>Or, for Oracle and SQLite:</p> <pre><code>SELECT ' SELECT Sample.BuildingID,MAX(Sample.BuildingName_EN) BuildingID,' UNION ALL SELECT ' MAX(CASE WHEN Operation.SurveyTypeID='||SurveyType.I||' THEN Operation.StatusID ELSE " " END) AS "'||SurveyType.LongName||'",' from SurveyType UNION ALL SELECT '0 as dummy FROM Sample,Operation WHERE Operation.SampleID=Sample.ID GROUP BY Sample.BuildingID' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload