Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

  1. SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),'DATE_OF_BIRTH')), '%Y') + 0 AS age

    SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),’DATE_OF_BIRTH’)), ‘%Y’) + 0 AS age

    See less
  2. you can use this query DELETE relations.*, taxes.*, terms.* FROM wp_term_relationships AS relations INNER JOIN wp_term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN wp_terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT ID FROM wp_posts WHERERead more

    you can use this query

    DELETE relations.*, taxes.*, terms.*
    FROM wp_term_relationships AS relations
    INNER JOIN wp_term_taxonomy AS taxes
    ON relations.term_taxonomy_id=taxes.term_taxonomy_id
    INNER JOIN wp_terms AS terms
    ON taxes.term_id=terms.term_id
    WHERE object_id IN (SELECT ID FROM wp_posts WHERE post_type IN ('product','product_variation'));
    
    DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type IN ('product','product_variation'));
    DELETE FROM wp_posts WHERE post_type IN ('product','product_variation');
    
    See less
  3. Part of the problem is that you add a year and not subtract. Another thing is that there are different types of empty - it could be an empty space like you are looking for, an empty string or null. So if you don't know what to expect there, cover all cases. Change the where clause to: where ldate inRead more

    Part of the problem is that you add a year and not subtract.
    Another thing is that there are different types of empty – it could be an empty space like you are looking for, an empty string or null.
    So if you don’t know what to expect there, cover all cases.
    Change the where clause to:

    where ldate in ('',' ') 
        or ldate is null
        or ldate > dateadd(year,-1,getDate())
    

    there are other approached for the last part if you think these functions aren’t supported (yet you didn’t say that there are errors), or if you date field is of a wrong type. But then it would be best if you provide an exmaple for that.

    See less
  4. Create your custom order using ROW_NUMBER() and CTE : WITH Orderd_db AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY articleId ORDER BY modifiedDate DESC) AS RowNumber FROM db WHERE content LIKE '%blabla%' ) SELECT * FROM Orderd_db WHERE Orderd_db.RowNumber = 1

    Create your custom order using ROW_NUMBER() and CTE :

    WITH Orderd_db AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY articleId ORDER BY modifiedDate DESC) AS RowNumber
    FROM db
    WHERE content LIKE '%blabla%'
    )
    SELECT * FROM Orderd_db WHERE Orderd_db.RowNumber = 1
    
    See less
  5. I need to update the column Date_alert with the date less than two months compared to the column Next_control only for most recent row for uID 45265 UPDATE dotable SET Date_alert = dt - INTERVAL 2 MONTH WHERE uID = 45265 ORDER BY dt DESC LIMIT 1

    I need to update the column Date_alert with the date less than two months compared to the column Next_control only for most recent row for uID 45265

    UPDATE dotable
    SET Date_alert = dt - INTERVAL 2 MONTH
    WHERE uID = 45265
    ORDER BY dt DESC LIMIT 1
    
    See less
  6. Maybe I'm overthinking it, I'm not sure, but the solution below works for your test data and is somewhat understandable (to me at least). The idea behind it is to add a root ID to all ID pairs using a recursive query. Then we use window functions partitioned by roots to find out the latest ID. withRead more

    Maybe I’m overthinking it, I’m not sure, but the solution below works for your test data and is somewhat understandable (to me at least).

    The idea behind it is to add a root ID to all ID pairs using a recursive query. Then we use window functions partitioned by roots to find out the latest ID.

    with recursive
      --
      -- Test data
      --
      changes(old_id, new_id) as (values
        (1,2),
        (2,3),
        (3,4),
        (999,1000)
      ),
    
      --
      -- Obtain all root IDs, i.e., all `old_id` values that don't appear in the
      -- `new_id` column.
      --
      roots(root_id) as (
        select old_id from changes where old_id not in (select new_id from changes)
      ),
    
      --
      -- Add a root ID to all pairs.
      --
      rooted_changes(root_id, old_id, new_id) as (
        select old_id, old_id, new_id
        from changes
        join roots on changes.old_id = roots.root_id
    
        union all
    
        select older.root_id, newer.old_id, newer.new_id
        from rooted_changes older
        join changes newer on older.new_id = newer.old_id
      ),
    
      --
      -- Partition each row over the root ID that it belongs too and extract
      -- the max `new_id` value (alternatives in terms of `first_value` and
      -- `last_value` are given).
      --
      result as (
        select
          old_id,
          max(new_id) over roots,
    
          -- alternatively
          first_value(new_id) over (roots order by new_id desc),
    
          -- alternatively
          last_value(new_id) over (roots
            order by new_id asc
            rows between current row and unbounded following
          )
        from rooted_changes
        window roots as (partition by root_id)
      )
    
    -- Uncomment selectively to debug various stages:
    
    -- select * from roots
    
    -- select * from rooted_changes order by root_id asc
    
    select * from result order by old_id asc;
    

    Final result:

    ┌────────┬──────┬─────────────┬────────────┐
    │ old_id │ max  │ first_value │ last_value │
    ├────────┼──────┼─────────────┼────────────┤
    │      1 │    4 │           4 │          4 │
    │      2 │    4 │           4 │          4 │
    │      3 │    4 │           4 │          4 │
    │    999 │ 1000 │        1000 │       1000 │
    └────────┴──────┴─────────────┴────────────┘
    

    Intermediate result of select * from roots:

    ┌─────────┐
    │ root_id │
    ├─────────┤
    │       1 │
    │     999 │
    └─────────┘
    

    Intermediate result of select * from rooted_changes order by root_id asc:

    ┌─────────┬────────┬────────┐
    │ root_id │ old_id │ new_id │
    ├─────────┼────────┼────────┤
    │       1 │      1 │      2 │
    │       1 │      2 │      3 │
    │       1 │      3 │      4 │
    │     999 │    999 │   1000 │
    └─────────┴────────┴────────┘
    
    See less
  7. Indeed, NOT IN isn't optimized as well as NOT EXIST in Postgres. So an equivalent not exists () condition is typically faster. However, in neither case do you need to apply a (costly) DISTINCT on the rows in the sub-query. with perimeter_per_day_table1 as ( select t1.id, date_trunc('day', t1.time) aRead more

    Indeed, NOT IN isn’t optimized as well as NOT EXIST in Postgres. So an equivalent not exists () condition is typically faster.

    However, in neither case do you need to apply a (costly) DISTINCT on the rows in the sub-query.

    with perimeter_per_day_table1 as (
      select t1.id, 
             date_trunc('day', t1.time) as day
      from table1 t1
      where t1.day >= (select min(time) from table2)
        and t1.day <= (select max(time) from table2)
        and not exists (select * 
                        from table2 t2 
                        where t1.id = t2.id
                          and t1.day = t2.day)
    )
    select * 
    from perimeter_per_day_table1;
    

    You can even avoid querying table2 twice for the min/max, but I doubt that will make a huge difference if there is an index on the time column:

    with min_max as ( 
       select min(time) as min_time, 
              max(time) as max_time
       from table2
    ), perimeter_per_day_table1 as (
      select t1.id, 
             date_trunc('day', t1.time) as day
      from table1 t1
        cross join min_max 
      where t1.day >= min_max.min_time
        and t1.day <= min_max.max_time
        and not exists (select * 
                        from table2 t2 
                        where t1.id = t2.id
                          and t1.day = t2.day)
    )
    select * 
    from perimeter_per_day_table1;
    
    See less
  8. Do you need in this: SELECT [serialno], [storedatetime], MAX(CASE WHEN [stepid]= 50 THEN {some_column_name} END) [value1], MAX(CASE WHEN [stepid]= 80 THEN {some_column_name} END) [value2], ROW_NUMBER() OVER (PARTITION BY [serialno] ORDER BY [storedatetime] desc) AS RowNumber FROM [database] WHERE [pRead more

    Do you need in this:

    SELECT [serialno],
           [storedatetime], 
           MAX(CASE WHEN [stepid]= 50 THEN {some_column_name} END) [value1],
           MAX(CASE WHEN [stepid]= 80 THEN {some_column_name} END) [value2],
           ROW_NUMBER() OVER (PARTITION BY [serialno] ORDER BY [storedatetime] desc) AS RowNumber 
    FROM [database] 
    WHERE [processid] = 610 and [stepid] IN (50, 80)
    GROUP BY 1, 2
    
    See less
  9. One option is to use conditional aggregation as the following: SELECT T.IDPARENT, T.NAME FROM T_PARENT T JOIN T_CHILDREN D ON T.IDPARENT = D.IDPARENT GROUP BY T.IDPARENT, T.NAME HAVING COUNT(CASE WHEN D.NAME='Pablo' THEN 1 END) > 0 AND COUNT(CASE WHEN D.NAME='Juan' AND D.AGE=9 THEN 1 END) > 0Read more

    One option is to use conditional aggregation as the following:

    SELECT T.IDPARENT, T.NAME
    FROM T_PARENT T JOIN T_CHILDREN D
    ON T.IDPARENT = D.IDPARENT
    GROUP BY T.IDPARENT, T.NAME
    HAVING
    COUNT(CASE WHEN D.NAME='Pablo' THEN 1 END) > 0 AND
    COUNT(CASE WHEN D.NAME='Juan' AND D.AGE=9 THEN 1 END) > 0 AND
    COUNT(CASE WHEN D.NAME='Diego' AND D.AGE=9 AND D.HEIGHT=110 THEN 1 END) > 0 AND
    COUNT(CASE WHEN D.NAME='Diego' AND D.AGE=9 AND D.HEIGHT=120 THEN 1 END) > 0
    ORDER BY T.IDPARENT, T.NAME
    

    See a demo.

    Another option is to use EXISTS operator as the following:

    SELECT T.IDPARENT, T.NAME
    FROM T_PARENT T 
    WHERE EXISTS(SELECT 1 FROM T_CHILDREN D WHERE T.IDPARENT = D.IDPARENT AND D.NAME='Pablo')
    AND EXISTS(SELECT 1 FROM T_CHILDREN D WHERE T.IDPARENT = D.IDPARENT AND  D.NAME='Juan' AND D.AGE=9)
    AND EXISTS(SELECT 1 FROM T_CHILDREN D WHERE T.IDPARENT = D.IDPARENT AND D.NAME='Diego' AND D.AGE=9 AND D.HEIGHT=110)
    AND EXISTS(SELECT 1 FROM T_CHILDREN D WHERE T.IDPARENT = D.IDPARENT AND D.NAME='Diego' AND D.AGE=9 AND D.HEIGHT=120)
    

    See a demo.

    See less