Column names of a select clause that are dynamically provided based on a condition










0















I am using sqlite db from an iphone.



I have a chartdatatable as shown below.



The deviceTypeHash is a hash for a device that can have 2 or 3 SeriesNameHash.



enter image description here



The master table for DevicTypeHash comes from DeviceParamUnit table as shown below



enter image description here



As you'll observe the DeviceTypeHash -represents a device - is common for the first three records and they have 3 different SeriesNameHash which corresponds to Systolic, Diastolic and PulseRate in the DeviceParamUnit table. They are like parameters of the first device which is a BP monitor. (which is True, Blood Pressure has three readings - systolic, dia...)



Similar way, the next device is the Temperature Wand and it has 11 parameters represented by 11 unique series name hash.



What I require is that when I provide the query with DeviceNameHash, it should show me the values (column is present in chartdatatable) for its corresponding SeriesNameHash(parameters). But it should show it pivoted.



Here's what I tried to get the pivot.



SELECT time
, MAX(CASE WHEN SeriesNameHash = -7656137604397428413 THEN value ELSE 0 END) as '-7656137604397428413'
, MAX(CASE WHEN SeriesNameHash = 1538157359584988292 THEN value ELSE 0 END) as '1538157359584988292'
, MAX(CASE WHEN SeriesNameHash = 6366213907772730014 THEN value ELSE 0 END) as '6366213907772730014'
, MAX(CASE WHEN SeriesNameHash = -5604390693208903701 THEN value ELSE 0 END) as '-5604390693208903701'
, MAX(CASE WHEN SeriesNameHash = -5604390693208899606 THEN value ELSE 0 END) as '-5604390693208899606'
, MAX(CASE WHEN SeriesNameHash = -5604390693208928275 THEN value ELSE 0 END) as '-5604390693208928275'
, MAX(CASE WHEN SeriesNameHash = -5604390693208924180 THEN value ELSE 0 END) as '-5604390693208924180'
, MAX(CASE WHEN SeriesNameHash = -5604390693208920081 THEN value ELSE 0 END) as '-5604390693208920081'
, MAX(CASE WHEN SeriesNameHash = -5604390693208915986 THEN value ELSE 0 END) as '-5604390693208915986'
, MAX(CASE WHEN SeriesNameHash = -5604390693208944655 THEN value ELSE 0 END) as '-5604390693208944655'
, MAX(CASE WHEN SeriesNameHash = -5604390693208940560 THEN value ELSE 0 END) as '-5604390693208940560'
, MAX(CASE WHEN SeriesNameHash = -5604390693208936461 THEN value ELSE 0 END) as '-5604390693208936461'
, MAX(CASE WHEN SeriesNameHash = -5604390693208932366 THEN value ELSE 0 END) as '-5604390693208932366'
, MAX(CASE WHEN SeriesNameHash = 4940014074620696541 THEN value ELSE 0 END) as '4940014074620696541'
, MAX(CASE WHEN SeriesNameHash = 4940014074620704732 THEN value ELSE 0 END) as '4940014074620704732'

FROM chartdatatable
GROUP BY time


with result.



enter image description here



( I know I could have named the columns 'systolic', 'diastolic'...but that's beside the point.



The problem is, now, How do I dynamically only display those columns(each column has the SeriesNameHash) when the query is only supplied a DeviceTypeHash.



E.g - If I needed to see all the values (by time) of the B.P Monitor, its seriesHashName would be something like this



SELECT seriesname, seriesnamehash FROM 'DeviceParamUnitTable' where DeviceTypeHash = 6814980142680378626

Systolic -7656137604397428413
Diastolic 1538157359584988292
Pulse Rate 6366213907772730014


How do I modify the parent query to only get the first three columns of the pivot result when supplied by the DeviceTypeHash of BP monitor and get (say) column 4-14 when supplied by the DeviceTypeHash of (say) a TemperatureWand. Currently, it shows all parameter values and its not useful when I require the pivot made above, filtered.




To summarize, I've done a pivot, but how do I now get specific columns
when provided by a value that has those column names mapped to it.




If this is not possible by a query, view, any other approach can be suggested. Any help will be appreciated.










share|improve this question


























    0















    I am using sqlite db from an iphone.



    I have a chartdatatable as shown below.



    The deviceTypeHash is a hash for a device that can have 2 or 3 SeriesNameHash.



    enter image description here



    The master table for DevicTypeHash comes from DeviceParamUnit table as shown below



    enter image description here



    As you'll observe the DeviceTypeHash -represents a device - is common for the first three records and they have 3 different SeriesNameHash which corresponds to Systolic, Diastolic and PulseRate in the DeviceParamUnit table. They are like parameters of the first device which is a BP monitor. (which is True, Blood Pressure has three readings - systolic, dia...)



    Similar way, the next device is the Temperature Wand and it has 11 parameters represented by 11 unique series name hash.



    What I require is that when I provide the query with DeviceNameHash, it should show me the values (column is present in chartdatatable) for its corresponding SeriesNameHash(parameters). But it should show it pivoted.



    Here's what I tried to get the pivot.



    SELECT time
    , MAX(CASE WHEN SeriesNameHash = -7656137604397428413 THEN value ELSE 0 END) as '-7656137604397428413'
    , MAX(CASE WHEN SeriesNameHash = 1538157359584988292 THEN value ELSE 0 END) as '1538157359584988292'
    , MAX(CASE WHEN SeriesNameHash = 6366213907772730014 THEN value ELSE 0 END) as '6366213907772730014'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208903701 THEN value ELSE 0 END) as '-5604390693208903701'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208899606 THEN value ELSE 0 END) as '-5604390693208899606'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208928275 THEN value ELSE 0 END) as '-5604390693208928275'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208924180 THEN value ELSE 0 END) as '-5604390693208924180'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208920081 THEN value ELSE 0 END) as '-5604390693208920081'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208915986 THEN value ELSE 0 END) as '-5604390693208915986'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208944655 THEN value ELSE 0 END) as '-5604390693208944655'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208940560 THEN value ELSE 0 END) as '-5604390693208940560'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208936461 THEN value ELSE 0 END) as '-5604390693208936461'
    , MAX(CASE WHEN SeriesNameHash = -5604390693208932366 THEN value ELSE 0 END) as '-5604390693208932366'
    , MAX(CASE WHEN SeriesNameHash = 4940014074620696541 THEN value ELSE 0 END) as '4940014074620696541'
    , MAX(CASE WHEN SeriesNameHash = 4940014074620704732 THEN value ELSE 0 END) as '4940014074620704732'

    FROM chartdatatable
    GROUP BY time


    with result.



    enter image description here



    ( I know I could have named the columns 'systolic', 'diastolic'...but that's beside the point.



    The problem is, now, How do I dynamically only display those columns(each column has the SeriesNameHash) when the query is only supplied a DeviceTypeHash.



    E.g - If I needed to see all the values (by time) of the B.P Monitor, its seriesHashName would be something like this



    SELECT seriesname, seriesnamehash FROM 'DeviceParamUnitTable' where DeviceTypeHash = 6814980142680378626

    Systolic -7656137604397428413
    Diastolic 1538157359584988292
    Pulse Rate 6366213907772730014


    How do I modify the parent query to only get the first three columns of the pivot result when supplied by the DeviceTypeHash of BP monitor and get (say) column 4-14 when supplied by the DeviceTypeHash of (say) a TemperatureWand. Currently, it shows all parameter values and its not useful when I require the pivot made above, filtered.




    To summarize, I've done a pivot, but how do I now get specific columns
    when provided by a value that has those column names mapped to it.




    If this is not possible by a query, view, any other approach can be suggested. Any help will be appreciated.










    share|improve this question
























      0












      0








      0








      I am using sqlite db from an iphone.



      I have a chartdatatable as shown below.



      The deviceTypeHash is a hash for a device that can have 2 or 3 SeriesNameHash.



      enter image description here



      The master table for DevicTypeHash comes from DeviceParamUnit table as shown below



      enter image description here



      As you'll observe the DeviceTypeHash -represents a device - is common for the first three records and they have 3 different SeriesNameHash which corresponds to Systolic, Diastolic and PulseRate in the DeviceParamUnit table. They are like parameters of the first device which is a BP monitor. (which is True, Blood Pressure has three readings - systolic, dia...)



      Similar way, the next device is the Temperature Wand and it has 11 parameters represented by 11 unique series name hash.



      What I require is that when I provide the query with DeviceNameHash, it should show me the values (column is present in chartdatatable) for its corresponding SeriesNameHash(parameters). But it should show it pivoted.



      Here's what I tried to get the pivot.



      SELECT time
      , MAX(CASE WHEN SeriesNameHash = -7656137604397428413 THEN value ELSE 0 END) as '-7656137604397428413'
      , MAX(CASE WHEN SeriesNameHash = 1538157359584988292 THEN value ELSE 0 END) as '1538157359584988292'
      , MAX(CASE WHEN SeriesNameHash = 6366213907772730014 THEN value ELSE 0 END) as '6366213907772730014'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208903701 THEN value ELSE 0 END) as '-5604390693208903701'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208899606 THEN value ELSE 0 END) as '-5604390693208899606'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208928275 THEN value ELSE 0 END) as '-5604390693208928275'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208924180 THEN value ELSE 0 END) as '-5604390693208924180'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208920081 THEN value ELSE 0 END) as '-5604390693208920081'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208915986 THEN value ELSE 0 END) as '-5604390693208915986'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208944655 THEN value ELSE 0 END) as '-5604390693208944655'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208940560 THEN value ELSE 0 END) as '-5604390693208940560'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208936461 THEN value ELSE 0 END) as '-5604390693208936461'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208932366 THEN value ELSE 0 END) as '-5604390693208932366'
      , MAX(CASE WHEN SeriesNameHash = 4940014074620696541 THEN value ELSE 0 END) as '4940014074620696541'
      , MAX(CASE WHEN SeriesNameHash = 4940014074620704732 THEN value ELSE 0 END) as '4940014074620704732'

      FROM chartdatatable
      GROUP BY time


      with result.



      enter image description here



      ( I know I could have named the columns 'systolic', 'diastolic'...but that's beside the point.



      The problem is, now, How do I dynamically only display those columns(each column has the SeriesNameHash) when the query is only supplied a DeviceTypeHash.



      E.g - If I needed to see all the values (by time) of the B.P Monitor, its seriesHashName would be something like this



      SELECT seriesname, seriesnamehash FROM 'DeviceParamUnitTable' where DeviceTypeHash = 6814980142680378626

      Systolic -7656137604397428413
      Diastolic 1538157359584988292
      Pulse Rate 6366213907772730014


      How do I modify the parent query to only get the first three columns of the pivot result when supplied by the DeviceTypeHash of BP monitor and get (say) column 4-14 when supplied by the DeviceTypeHash of (say) a TemperatureWand. Currently, it shows all parameter values and its not useful when I require the pivot made above, filtered.




      To summarize, I've done a pivot, but how do I now get specific columns
      when provided by a value that has those column names mapped to it.




      If this is not possible by a query, view, any other approach can be suggested. Any help will be appreciated.










      share|improve this question














      I am using sqlite db from an iphone.



      I have a chartdatatable as shown below.



      The deviceTypeHash is a hash for a device that can have 2 or 3 SeriesNameHash.



      enter image description here



      The master table for DevicTypeHash comes from DeviceParamUnit table as shown below



      enter image description here



      As you'll observe the DeviceTypeHash -represents a device - is common for the first three records and they have 3 different SeriesNameHash which corresponds to Systolic, Diastolic and PulseRate in the DeviceParamUnit table. They are like parameters of the first device which is a BP monitor. (which is True, Blood Pressure has three readings - systolic, dia...)



      Similar way, the next device is the Temperature Wand and it has 11 parameters represented by 11 unique series name hash.



      What I require is that when I provide the query with DeviceNameHash, it should show me the values (column is present in chartdatatable) for its corresponding SeriesNameHash(parameters). But it should show it pivoted.



      Here's what I tried to get the pivot.



      SELECT time
      , MAX(CASE WHEN SeriesNameHash = -7656137604397428413 THEN value ELSE 0 END) as '-7656137604397428413'
      , MAX(CASE WHEN SeriesNameHash = 1538157359584988292 THEN value ELSE 0 END) as '1538157359584988292'
      , MAX(CASE WHEN SeriesNameHash = 6366213907772730014 THEN value ELSE 0 END) as '6366213907772730014'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208903701 THEN value ELSE 0 END) as '-5604390693208903701'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208899606 THEN value ELSE 0 END) as '-5604390693208899606'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208928275 THEN value ELSE 0 END) as '-5604390693208928275'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208924180 THEN value ELSE 0 END) as '-5604390693208924180'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208920081 THEN value ELSE 0 END) as '-5604390693208920081'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208915986 THEN value ELSE 0 END) as '-5604390693208915986'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208944655 THEN value ELSE 0 END) as '-5604390693208944655'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208940560 THEN value ELSE 0 END) as '-5604390693208940560'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208936461 THEN value ELSE 0 END) as '-5604390693208936461'
      , MAX(CASE WHEN SeriesNameHash = -5604390693208932366 THEN value ELSE 0 END) as '-5604390693208932366'
      , MAX(CASE WHEN SeriesNameHash = 4940014074620696541 THEN value ELSE 0 END) as '4940014074620696541'
      , MAX(CASE WHEN SeriesNameHash = 4940014074620704732 THEN value ELSE 0 END) as '4940014074620704732'

      FROM chartdatatable
      GROUP BY time


      with result.



      enter image description here



      ( I know I could have named the columns 'systolic', 'diastolic'...but that's beside the point.



      The problem is, now, How do I dynamically only display those columns(each column has the SeriesNameHash) when the query is only supplied a DeviceTypeHash.



      E.g - If I needed to see all the values (by time) of the B.P Monitor, its seriesHashName would be something like this



      SELECT seriesname, seriesnamehash FROM 'DeviceParamUnitTable' where DeviceTypeHash = 6814980142680378626

      Systolic -7656137604397428413
      Diastolic 1538157359584988292
      Pulse Rate 6366213907772730014


      How do I modify the parent query to only get the first three columns of the pivot result when supplied by the DeviceTypeHash of BP monitor and get (say) column 4-14 when supplied by the DeviceTypeHash of (say) a TemperatureWand. Currently, it shows all parameter values and its not useful when I require the pivot made above, filtered.




      To summarize, I've done a pivot, but how do I now get specific columns
      when provided by a value that has those column names mapped to it.




      If this is not possible by a query, view, any other approach can be suggested. Any help will be appreciated.







      sqlite sqlite3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 5:25









      user2181576user2181576

      1237




      1237






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312948%2fcolumn-names-of-a-select-clause-that-are-dynamically-provided-based-on-a-conditi%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312948%2fcolumn-names-of-a-select-clause-that-are-dynamically-provided-based-on-a-conditi%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto