SQL tables Joins and generating new column based on query [closed]
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
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.
|
show 1 more comment
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
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 oftransaction_fid
for every product in where week_id is in the range of201815 -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
|
show 1 more comment
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
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
sql oracle
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 oftransaction_fid
for every product in where week_id is in the range of201815 -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
|
show 1 more comment
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 oftransaction_fid
for every product in where week_id is in the range of201815 -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
|
show 1 more comment
3 Answers
3
active
oldest
votes
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.
add a comment |
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
You seem to be missing aGROUP BY pr.prod_id
. And theINNER 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. UseEXISTS
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 26 '18 at 10:07
answered Nov 26 '18 at 9:59
Thorsten KettnerThorsten Kettner
52.8k32643
52.8k32643
add a comment |
add a comment |
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
You seem to be missing aGROUP BY pr.prod_id
. And theINNER 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. UseEXISTS
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
add a comment |
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
You seem to be missing aGROUP BY pr.prod_id
. And theINNER 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. UseEXISTS
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
add a comment |
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
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
answered Nov 26 '18 at 9:39
Seema MudgilSeema Mudgil
7410
7410
You seem to be missing aGROUP BY pr.prod_id
. And theINNER 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. UseEXISTS
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
add a comment |
You seem to be missing aGROUP BY pr.prod_id
. And theINNER 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. UseEXISTS
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 26 '18 at 10:44
answered Nov 26 '18 at 9:12
LukStormsLukStorms
14.1k31734
14.1k31734
add a comment |
add a comment |
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 oftransaction_fid
for every product in where week_id is in the range of201815 -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