Having more than one EffectiveDate for product

Viewed 46

I Have a table Product_feed in which we have ProductNumber , Effectivedate , Transactiontype , SourceSystemName. It has multiple EffectiveDate for a Particular Product Number , but it also has a column which tells what kind of transaction has been done on a Particular Product. Below is the table.

ProductNumber EffectiveDate Transactiontype SourceSystemName
47-CAO-01 25-01-2021 MDE DOM_SUBM_01
47-CAO-01 24-01-2022 Drtbd INT_NA_01

I need to find out which Product have more than one EffectiveDate and what ProductNumber it has. And once I find that I have to update the EffectiveDate with EffectiveDate of the MDE Transaction.

1 Answers

You have to be very carefully when doing such things. There are some risks/things you should take care of:

  • What should happen with NULL values?
  • What should be done when there is no entry for a certain date having a MDE transaction?
  • What should be done when there are multiple MDE transactions at the same date?
  • Do you just need to count or even count distinct?
  • Maybe further points I did not think about

So, overall, this is quite risky and you should be really sure about what you're doing. But this is a matter of your setup that you need to check on your own.

What I can tell you is just a general way do do these things and then you have to check what exactly you will do in your case. In order to simply select all product numbers having multiple dates, you can do this:

SELECT productnumber
FROM yourtable
GROUP BY productnumber
HAVING COUNT(effectivedate) > 1;

As I wrote above, you might add a DISTINCT if you don't want to count the same date twice: ...COUNT(DISTINCT effectivedate).

To find the product number and the effectivedate for transactiontype MDE, you can do this query:

SELECT productnumber, effectivedate FROM yourtable 
WHERE transactiontype = 'MDE'

So you can JOIN these two results to see the product number and effectivedate for those product numbers only that occur with multiple dates and have a MDE transaction:

SELECT t1.productnumber, t2.effectivedate FROM
(SELECT productnumber
FROM yourtable
GROUP BY productnumber
HAVING COUNT(effectivedate) > 1) t1
JOIN
(SELECT productnumber, effectivedate FROM yourtable 
WHERE transactiontype = 'MDE') t2 ON t1.productnumber=t2.productnumber

If you are sure you really want to update your rows, you can use this join and do an update like this:

UPDATE yourtable SET effectivedate = x.newdate FROM
(SELECT t1.productnumber AS refnumber, t2.effectivedate AS newdate FROM
(SELECT productnumber
FROM yourtable
GROUP BY productnumber
HAVING COUNT(effectivedate) > 1) t1
JOIN
(SELECT productnumber, effectivedate FROM yourtable 
WHERE transactiontype = 'MDE') t2 ON t1.productnumber=t2.productnumber) x
WHERE productnumber = x.refnumber;

Please see here a working example with sample data: db<>fiddle

As already said, I don't recommend such risky data modifications, but prefer to make sure to set correct data within the appilcation which fills this table.

Related