SQL tables Joins and generating new column based on query [closed]












-4















I have 3 tables




  • transaction

  • product

  • date


Transaction table has columns





  • date_id, prod_id,spend_amt, qty, transaction_fid .


Product table has




  • prod_id, desc, category


and date table has





  • date_id, week_id .


I need to flag products active if they are sold in last 4 weeks else non-active
How to go about this.










share|improve this question















closed as unclear what you're asking by Tim Biegeleisen, jarlh, LukStorms, Kaushik Nayak, gnat Nov 26 '18 at 10:09


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 6





    what you have tried Please explain? What is week id ?Please explain with some data

    – Gagan Burde
    Nov 26 '18 at 7:02













  • week_id is the unique indicator for each week. What I tried is to find count of transaction_fid for every product in where week_id is in the range of 201815 -201818 .

    – Seema Mudgil
    Nov 26 '18 at 7:04








  • 3





    Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments)

    – a_horse_with_no_name
    Nov 26 '18 at 7:06











  • transaction, product, date, transaction_fid are 4 tables...

    – jarlh
    Nov 26 '18 at 7:08






  • 1





    Heh, I find it kinda perculiar that there's a foreign key used that links to a date table. More commonly you would just see a DATE/DATETIME/TIMESTAMP in the table, and the "calendar" table would have a primary key on a DATE type field. Unless of course that date_id is a DATE in this case, and not an INT.

    – LukStorms
    Nov 26 '18 at 8:23
















-4















I have 3 tables




  • transaction

  • product

  • date


Transaction table has columns





  • date_id, prod_id,spend_amt, qty, transaction_fid .


Product table has




  • prod_id, desc, category


and date table has





  • date_id, week_id .


I need to flag products active if they are sold in last 4 weeks else non-active
How to go about this.










share|improve this question















closed as unclear what you're asking by Tim Biegeleisen, jarlh, LukStorms, Kaushik Nayak, gnat Nov 26 '18 at 10:09


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 6





    what you have tried Please explain? What is week id ?Please explain with some data

    – Gagan Burde
    Nov 26 '18 at 7:02













  • week_id is the unique indicator for each week. What I tried is to find count of transaction_fid for every product in where week_id is in the range of 201815 -201818 .

    – Seema Mudgil
    Nov 26 '18 at 7:04








  • 3





    Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments)

    – a_horse_with_no_name
    Nov 26 '18 at 7:06











  • transaction, product, date, transaction_fid are 4 tables...

    – jarlh
    Nov 26 '18 at 7:08






  • 1





    Heh, I find it kinda perculiar that there's a foreign key used that links to a date table. More commonly you would just see a DATE/DATETIME/TIMESTAMP in the table, and the "calendar" table would have a primary key on a DATE type field. Unless of course that date_id is a DATE in this case, and not an INT.

    – LukStorms
    Nov 26 '18 at 8:23














-4












-4








-4








I have 3 tables




  • transaction

  • product

  • date


Transaction table has columns





  • date_id, prod_id,spend_amt, qty, transaction_fid .


Product table has




  • prod_id, desc, category


and date table has





  • date_id, week_id .


I need to flag products active if they are sold in last 4 weeks else non-active
How to go about this.










share|improve this question
















I have 3 tables




  • transaction

  • product

  • date


Transaction table has columns





  • date_id, prod_id,spend_amt, qty, transaction_fid .


Product table has




  • prod_id, desc, category


and date table has





  • date_id, week_id .


I need to flag products active if they are sold in last 4 weeks else non-active
How to go about this.







sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 7:43









a_horse_with_no_name

305k46466562




305k46466562










asked Nov 26 '18 at 6:54









Seema MudgilSeema Mudgil

7410




7410




closed as unclear what you're asking by Tim Biegeleisen, jarlh, LukStorms, Kaushik Nayak, gnat Nov 26 '18 at 10:09


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by Tim Biegeleisen, jarlh, LukStorms, Kaushik Nayak, gnat Nov 26 '18 at 10:09


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 6





    what you have tried Please explain? What is week id ?Please explain with some data

    – Gagan Burde
    Nov 26 '18 at 7:02













  • week_id is the unique indicator for each week. What I tried is to find count of transaction_fid for every product in where week_id is in the range of 201815 -201818 .

    – Seema Mudgil
    Nov 26 '18 at 7:04








  • 3





    Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments)

    – a_horse_with_no_name
    Nov 26 '18 at 7:06











  • transaction, product, date, transaction_fid are 4 tables...

    – jarlh
    Nov 26 '18 at 7:08






  • 1





    Heh, I find it kinda perculiar that there's a foreign key used that links to a date table. More commonly you would just see a DATE/DATETIME/TIMESTAMP in the table, and the "calendar" table would have a primary key on a DATE type field. Unless of course that date_id is a DATE in this case, and not an INT.

    – LukStorms
    Nov 26 '18 at 8:23














  • 6





    what you have tried Please explain? What is week id ?Please explain with some data

    – Gagan Burde
    Nov 26 '18 at 7:02













  • week_id is the unique indicator for each week. What I tried is to find count of transaction_fid for every product in where week_id is in the range of 201815 -201818 .

    – Seema Mudgil
    Nov 26 '18 at 7:04








  • 3





    Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments)

    – a_horse_with_no_name
    Nov 26 '18 at 7:06











  • transaction, product, date, transaction_fid are 4 tables...

    – jarlh
    Nov 26 '18 at 7:08






  • 1





    Heh, I find it kinda perculiar that there's a foreign key used that links to a date table. More commonly you would just see a DATE/DATETIME/TIMESTAMP in the table, and the "calendar" table would have a primary key on a DATE type field. Unless of course that date_id is a DATE in this case, and not an INT.

    – LukStorms
    Nov 26 '18 at 8:23








6




6





what you have tried Please explain? What is week id ?Please explain with some data

– Gagan Burde
Nov 26 '18 at 7:02







what you have tried Please explain? What is week id ?Please explain with some data

– Gagan Burde
Nov 26 '18 at 7:02















week_id is the unique indicator for each week. What I tried is to find count of transaction_fid for every product in where week_id is in the range of 201815 -201818 .

– Seema Mudgil
Nov 26 '18 at 7:04







week_id is the unique indicator for each week. What I tried is to find count of transaction_fid for every product in where week_id is in the range of 201815 -201818 .

– Seema Mudgil
Nov 26 '18 at 7:04






3




3





Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments)

– a_horse_with_no_name
Nov 26 '18 at 7:06





Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments)

– a_horse_with_no_name
Nov 26 '18 at 7:06













transaction, product, date, transaction_fid are 4 tables...

– jarlh
Nov 26 '18 at 7:08





transaction, product, date, transaction_fid are 4 tables...

– jarlh
Nov 26 '18 at 7:08




1




1





Heh, I find it kinda perculiar that there's a foreign key used that links to a date table. More commonly you would just see a DATE/DATETIME/TIMESTAMP in the table, and the "calendar" table would have a primary key on a DATE type field. Unless of course that date_id is a DATE in this case, and not an INT.

– LukStorms
Nov 26 '18 at 8:23





Heh, I find it kinda perculiar that there's a foreign key used that links to a date table. More commonly you would just see a DATE/DATETIME/TIMESTAMP in the table, and the "calendar" table would have a primary key on a DATE type field. Unless of course that date_id is a DATE in this case, and not an INT.

– LukStorms
Nov 26 '18 at 8:23












3 Answers
3






active

oldest

votes


















1














Use an EXISTS clause to check whether a transaction in a date range exists for a product.



select
p.*
case when exists
(
select *
from transaction t
join date d on d.date_id = t.date_id
where t.prod_id = p.prod_id
and d.week_id between 201815 and 201818
) then 1 else 0 end as is_active
from product p;


It is strange not to see any date in your tables. In your request you are asking for the last four weeks, which is not possible without knowing a transaction's date of course. In your request comments, however, you say you want to look at the range week_id between 201815 and 201818, so I used this.






share|improve this answer

































    0














    Finally I got the way to do this.



    SELECT pr.prod_id, (case when count(distinct tr.transaction_fid)>=1 THEN 'Y' ELSE 'N' end) as active_flag
    FROM transaction tr
    INNER JOIN product pr ON pr.prod_id = tr.prod_id
    INNER JOIN
    (
    SELECT date_id, week_id
    FROM date
    WHERE week_id BETWEEN 201815 AND 201818
    ) d ON d.date_id = tr.date_id





    share|improve this answer
























    • You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

      – Thorsten Kettner
      Nov 26 '18 at 10:04





















    0














    If you left join to the product_id's that have a transaction in the last 4 weeks then you could use a CASE for the flag.



    Example:



    SELECT p.*
    , (CASE WHEN wk4.prod_id IS NOT NULL THEN 'Y' ELSE 'N' END) AS isActive
    FROM Product p
    LEFT JOIN (
    SELECT t.prod_id
    FROM Transaction t
    JOIN "date" d ON d.date_id = t.date_id
    WHERE d.week_id >= to_number(to_char(current_date-(7*4), 'YYYYWW'))
    GROUP BY t.prod_id
    ) wk4 ON wk4.prod_id = p.prod_id





    share|improve this answer
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Use an EXISTS clause to check whether a transaction in a date range exists for a product.



      select
      p.*
      case when exists
      (
      select *
      from transaction t
      join date d on d.date_id = t.date_id
      where t.prod_id = p.prod_id
      and d.week_id between 201815 and 201818
      ) then 1 else 0 end as is_active
      from product p;


      It is strange not to see any date in your tables. In your request you are asking for the last four weeks, which is not possible without knowing a transaction's date of course. In your request comments, however, you say you want to look at the range week_id between 201815 and 201818, so I used this.






      share|improve this answer






























        1














        Use an EXISTS clause to check whether a transaction in a date range exists for a product.



        select
        p.*
        case when exists
        (
        select *
        from transaction t
        join date d on d.date_id = t.date_id
        where t.prod_id = p.prod_id
        and d.week_id between 201815 and 201818
        ) then 1 else 0 end as is_active
        from product p;


        It is strange not to see any date in your tables. In your request you are asking for the last four weeks, which is not possible without knowing a transaction's date of course. In your request comments, however, you say you want to look at the range week_id between 201815 and 201818, so I used this.






        share|improve this answer




























          1












          1








          1







          Use an EXISTS clause to check whether a transaction in a date range exists for a product.



          select
          p.*
          case when exists
          (
          select *
          from transaction t
          join date d on d.date_id = t.date_id
          where t.prod_id = p.prod_id
          and d.week_id between 201815 and 201818
          ) then 1 else 0 end as is_active
          from product p;


          It is strange not to see any date in your tables. In your request you are asking for the last four weeks, which is not possible without knowing a transaction's date of course. In your request comments, however, you say you want to look at the range week_id between 201815 and 201818, so I used this.






          share|improve this answer















          Use an EXISTS clause to check whether a transaction in a date range exists for a product.



          select
          p.*
          case when exists
          (
          select *
          from transaction t
          join date d on d.date_id = t.date_id
          where t.prod_id = p.prod_id
          and d.week_id between 201815 and 201818
          ) then 1 else 0 end as is_active
          from product p;


          It is strange not to see any date in your tables. In your request you are asking for the last four weeks, which is not possible without knowing a transaction's date of course. In your request comments, however, you say you want to look at the range week_id between 201815 and 201818, so I used this.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 10:07

























          answered Nov 26 '18 at 9:59









          Thorsten KettnerThorsten Kettner

          52.8k32643




          52.8k32643

























              0














              Finally I got the way to do this.



              SELECT pr.prod_id, (case when count(distinct tr.transaction_fid)>=1 THEN 'Y' ELSE 'N' end) as active_flag
              FROM transaction tr
              INNER JOIN product pr ON pr.prod_id = tr.prod_id
              INNER JOIN
              (
              SELECT date_id, week_id
              FROM date
              WHERE week_id BETWEEN 201815 AND 201818
              ) d ON d.date_id = tr.date_id





              share|improve this answer
























              • You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

                – Thorsten Kettner
                Nov 26 '18 at 10:04


















              0














              Finally I got the way to do this.



              SELECT pr.prod_id, (case when count(distinct tr.transaction_fid)>=1 THEN 'Y' ELSE 'N' end) as active_flag
              FROM transaction tr
              INNER JOIN product pr ON pr.prod_id = tr.prod_id
              INNER JOIN
              (
              SELECT date_id, week_id
              FROM date
              WHERE week_id BETWEEN 201815 AND 201818
              ) d ON d.date_id = tr.date_id





              share|improve this answer
























              • You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

                – Thorsten Kettner
                Nov 26 '18 at 10:04
















              0












              0








              0







              Finally I got the way to do this.



              SELECT pr.prod_id, (case when count(distinct tr.transaction_fid)>=1 THEN 'Y' ELSE 'N' end) as active_flag
              FROM transaction tr
              INNER JOIN product pr ON pr.prod_id = tr.prod_id
              INNER JOIN
              (
              SELECT date_id, week_id
              FROM date
              WHERE week_id BETWEEN 201815 AND 201818
              ) d ON d.date_id = tr.date_id





              share|improve this answer













              Finally I got the way to do this.



              SELECT pr.prod_id, (case when count(distinct tr.transaction_fid)>=1 THEN 'Y' ELSE 'N' end) as active_flag
              FROM transaction tr
              INNER JOIN product pr ON pr.prod_id = tr.prod_id
              INNER JOIN
              (
              SELECT date_id, week_id
              FROM date
              WHERE week_id BETWEEN 201815 AND 201818
              ) d ON d.date_id = tr.date_id






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 26 '18 at 9:39









              Seema MudgilSeema Mudgil

              7410




              7410













              • You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

                – Thorsten Kettner
                Nov 26 '18 at 10:04





















              • You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

                – Thorsten Kettner
                Nov 26 '18 at 10:04



















              You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

              – Thorsten Kettner
              Nov 26 '18 at 10:04







              You seem to be missing a GROUP BY pr.prod_id. And the INNER JOIN only shows you products in the given range (i.e. active_flag = 'Y'), so you won't find any other products. You would need an outer join instead. But I wouldn't join anyway. Use EXISTS to check whether a transaction exists in the data range for a product. Please see my answer on this.

              – Thorsten Kettner
              Nov 26 '18 at 10:04













              0














              If you left join to the product_id's that have a transaction in the last 4 weeks then you could use a CASE for the flag.



              Example:



              SELECT p.*
              , (CASE WHEN wk4.prod_id IS NOT NULL THEN 'Y' ELSE 'N' END) AS isActive
              FROM Product p
              LEFT JOIN (
              SELECT t.prod_id
              FROM Transaction t
              JOIN "date" d ON d.date_id = t.date_id
              WHERE d.week_id >= to_number(to_char(current_date-(7*4), 'YYYYWW'))
              GROUP BY t.prod_id
              ) wk4 ON wk4.prod_id = p.prod_id





              share|improve this answer






























                0














                If you left join to the product_id's that have a transaction in the last 4 weeks then you could use a CASE for the flag.



                Example:



                SELECT p.*
                , (CASE WHEN wk4.prod_id IS NOT NULL THEN 'Y' ELSE 'N' END) AS isActive
                FROM Product p
                LEFT JOIN (
                SELECT t.prod_id
                FROM Transaction t
                JOIN "date" d ON d.date_id = t.date_id
                WHERE d.week_id >= to_number(to_char(current_date-(7*4), 'YYYYWW'))
                GROUP BY t.prod_id
                ) wk4 ON wk4.prod_id = p.prod_id





                share|improve this answer




























                  0












                  0








                  0







                  If you left join to the product_id's that have a transaction in the last 4 weeks then you could use a CASE for the flag.



                  Example:



                  SELECT p.*
                  , (CASE WHEN wk4.prod_id IS NOT NULL THEN 'Y' ELSE 'N' END) AS isActive
                  FROM Product p
                  LEFT JOIN (
                  SELECT t.prod_id
                  FROM Transaction t
                  JOIN "date" d ON d.date_id = t.date_id
                  WHERE d.week_id >= to_number(to_char(current_date-(7*4), 'YYYYWW'))
                  GROUP BY t.prod_id
                  ) wk4 ON wk4.prod_id = p.prod_id





                  share|improve this answer















                  If you left join to the product_id's that have a transaction in the last 4 weeks then you could use a CASE for the flag.



                  Example:



                  SELECT p.*
                  , (CASE WHEN wk4.prod_id IS NOT NULL THEN 'Y' ELSE 'N' END) AS isActive
                  FROM Product p
                  LEFT JOIN (
                  SELECT t.prod_id
                  FROM Transaction t
                  JOIN "date" d ON d.date_id = t.date_id
                  WHERE d.week_id >= to_number(to_char(current_date-(7*4), 'YYYYWW'))
                  GROUP BY t.prod_id
                  ) wk4 ON wk4.prod_id = p.prod_id






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 26 '18 at 10:44

























                  answered Nov 26 '18 at 9:12









                  LukStormsLukStorms

                  14.1k31734




                  14.1k31734















                      Popular posts from this blog

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga