SQL statement question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans

    #1

    SQL statement question

    Hello group,

    I have a table with the next contents.
    It lists data about : Who sent what kind of message at what time.
    For the Level column: The highest level is Critical, the middle is Warning,
    the lowest is Info.

    Id Timestamp Level
    1 12:00 Info
    1 13:00 Warning
    1 14:00 Critical
    2 12:00 Info
    2 13:00 Warning
    2 14:00 Info
    2 15:00 Warning
    3 12:00 Critical
    3 13:00 Warning
    4 15:00 Info
    5 15:00 Info
    5 16:00 Warning

    What I want to get out of his list is:
    The first message with the highest level of each Id. So let the Level-column
    prevail above the Timestamp column,so I want to get this result:

    1 14:00 Critical // The only 'Critical'
    2 13:00 Warning // the first 'Warning'
    3 12:00 Critical // The only 'Critical'
    4 15:00 Info // The only one, so simple
    5 16:00 Warning // The highest level, the only
    'Warning'


    So: First find the highest level of message of each Id and then find the
    first one.
    The first part I tried with CASE .. THEN .. END construction.

    Any hints appreciated.

    Gr, Hans


  • Leif

    #2
    Re: SQL statement question


    "Hans" <nieuwslezer@zo nnet.nl> skrev i en meddelelse
    news:410681c4$0 $93324$e4fe514c @news.xs4all.nl ...[color=blue]
    > Hello group,
    >
    > I have a table with the next contents.
    > It lists data about : Who sent what kind of message at what time.
    > For the Level column: The highest level is Critical, the middle is[/color]
    Warning,[color=blue]
    > the lowest is Info.
    >
    > Id Timestamp Level
    > 1 12:00 Info
    > 1 13:00 Warning
    > 1 14:00 Critical
    > 2 12:00 Info
    > 2 13:00 Warning
    > 2 14:00 Info
    > 2 15:00 Warning
    > 3 12:00 Critical
    > 3 13:00 Warning
    > 4 15:00 Info
    > 5 15:00 Info
    > 5 16:00 Warning
    >
    > What I want to get out of his list is:
    > The first message with the highest level of each Id. So let the[/color]
    Level-column[color=blue]
    > prevail above the Timestamp column,so I want to get this result:
    >
    > 1 14:00 Critical // The only 'Critical'
    > 2 13:00 Warning // the first 'Warning'
    > 3 12:00 Critical // The only 'Critical'
    > 4 15:00 Info // The only one, so[/color]
    simple[color=blue]
    > 5 16:00 Warning // The highest level, the only
    > 'Warning'
    >
    >
    > So: First find the highest level of message of each Id and then find the
    > first one.[/color]


    I hope the level is an enum, so you can sort on it.

    Will this work?

    select id,min(time),le vel from table a where (id,level) in (select
    id,max(level) from table b group by b.id) group by a.id

    Leif


    Comment

    • Hans

      #3
      Re: SQL statement question


      "Leif" <idkadm@interne t.dk> wrote in message
      news:4108f169$0 $4202$d40e179e@ nntp03.dk.telia .net...[color=blue]
      >
      > "Hans" <nieuwslezer@zo nnet.nl> skrev i en meddelelse
      > news:410681c4$0 $93324$e4fe514c @news.xs4all.nl ...[color=green]
      > > Hello group,
      > >[/color][/color]
      [color=blue]
      >
      >
      > I hope the level is an enum, so you can sort on it.
      >[/color]
      Nope.
      [color=blue]
      > Will this work?
      >
      > select id,min(time),le vel from table a where (id,level) in (select
      > id,max(level) from table b group by b.id) group by a.id
      >[/color]

      That's a query that will cost a lot of time for multiple 1000s of records.

      I already found a solution myself; I created a new table with the
      'enum'-values of all levels ,MessageLevels:
      Value Name
      1 Info
      2 Warning
      3 Critical

      And thenthis query:
      SELECT
      `SubQuery`.`Id_ 1`,
      `MessageLevels` .`Name`
      FROM (
      SELECT
      `MyTable`.`Id` AS `Id_1`,
      `MessageLevels` .`Value` AS `Message_Value`
      FROM
      `MyTable`
      INNER JOIN `MessageLevels` ON (`MyTable`.`Nam e` = `MessageLevels` .`Name`)
      ORDER BY `Id`,
      `Message_Value` DESC
      ) AS `SubQuery`
      INNER JOIN
      `MessageLevels` ON (`SubQuery`.`Me ssage_Value` = `MessageLevels` .`Value`)
      GROUP BY `SubQuery`.`Id_ 1`


      Which in the subquery creates a table with all ID's with ALL their
      messagelevels, sorted by ID, and then MessageLevel DESC,
      And the from this subquery's result, id do GROUP BY Id, which causes only
      the first record for each Id to be selected.

      BTW This is not exactly what I asked in my first post, (No timestamp) but it
      is indeed what I want ;-)

      BTW-2 The query you propesed is running now for more than an hour, so that's
      not really a good solution, but thanks anyway for thinking with me.

      Gr, Hans


      Comment

      • Hans

        #4
        Re: SQL statement question


        "Leif" <idkadm@interne t.dk> wrote in message
        news:4108f169$0 $4202$d40e179e@ nntp03.dk.telia .net...[color=blue]
        >
        > "Hans" <nieuwslezer@zo nnet.nl> skrev i en meddelelse
        > news:410681c4$0 $93324$e4fe514c @news.xs4all.nl ...[color=green]
        > > Hello group,
        > >[/color][/color]
        [color=blue]
        >
        >
        > I hope the level is an enum, so you can sort on it.
        >[/color]
        Nope.
        [color=blue]
        > Will this work?
        >
        > select id,min(time),le vel from table a where (id,level) in (select
        > id,max(level) from table b group by b.id) group by a.id
        >[/color]

        That's a query that will cost a lot of time for multiple 1000s of records.

        I already found a solution myself; I created a new table with the
        'enum'-values of all levels ,MessageLevels:
        Value Name
        1 Info
        2 Warning
        3 Critical

        And thenthis query:
        SELECT
        `SubQuery`.`Id_ 1`,
        `MessageLevels` .`Name`
        FROM (
        SELECT
        `MyTable`.`Id` AS `Id_1`,
        `MessageLevels` .`Value` AS `Message_Value`
        FROM
        `MyTable`
        INNER JOIN `MessageLevels` ON (`MyTable`.`Nam e` = `MessageLevels` .`Name`)
        ORDER BY `Id`,
        `Message_Value` DESC
        ) AS `SubQuery`
        INNER JOIN
        `MessageLevels` ON (`SubQuery`.`Me ssage_Value` = `MessageLevels` .`Value`)
        GROUP BY `SubQuery`.`Id_ 1`


        Which in the subquery creates a table with all ID's with ALL their
        messagelevels, sorted by ID, and then MessageLevel DESC,
        And the from this subquery's result, id do GROUP BY Id, which causes only
        the first record for each Id to be selected.

        BTW This is not exactly what I asked in my first post, (No timestamp) but it
        is indeed what I want ;-)

        BTW-2 The query you propesed is running now for almost 45 minutes, so that's
        not really a good solution, but thanks anyway for thinking with me.

        Gr, Hans



        Comment

        • Siemel Naran

          #5
          Re: SQL statement question

          "Hans" <nieuwslezer@zo nnet.nl> wrote in message news:4108fb97$0 $93324[color=blue]
          > "Leif" <idkadm@interne t.dk> wrote in message[/color]
          [color=blue][color=green]
          > > Will this work?
          > >
          > > select id,min(time),le vel from table a where (id,level) in (select
          > > id,max(level) from table b group by b.id) group by a.id[/color]
          >
          > That's a query that will cost a lot of time for multiple 1000s of records.[/color]
          [color=blue]
          > BTW-2 The query you propesed is running now for more than an hour, so[/color]
          that's[color=blue]
          > not really a good solution, but thanks anyway for thinking with me.[/color]

          What is the exact query you ran? I think MySql supports function indexes,
          so for example you could try to create index (id, case level when 'High'
          then 3 ... end), or create index (id, if(level='High' , 3, if(...))). Will
          need to look it up, but have to run to work.

          Also, what does the query plan say is the driving table and index?


          Comment

          Working...