Hello all once again,
I need help in writing a query. Please look at the ProductIdentifier Row below. I have inserted a column called LineItemInIIFFile. I need to write a code that will:
1. Find each ProductIdentifier = 'AWRPNS0003%%'
2.Go 2 rows upward from this product identifier and replace that ProductIdentifier with 'AWRPNS0003%%'.
3. After the replacements, I will insert a 'RS_PHN' in the LineItemInIIFFile column where the new ProductIdentifiers are 'AWRPNS0003%%'.
After all is found and all the 'RS_PHN' are inserted, I need to put all of this into a temp table somehow.
I recieved help yesterday for finding id's and then replacing that id with the id that is directly below it. I got help from a knowledgable member here and I tried to apply the same logic again but it does not work. Here is the code:
Select SipID,
SaleInvoiceID,
AssociationNumber,
Priority,
TotalPrice,
TotalCost,
SerialNumber,
ContractNumber,
ActivatedThroughPAW,
DateCreatedatMidnight,
ReceivedDate,
InvoiceIDByStore,
Location,
ProductIdentifier,
Description,
ShortDescription,
GLAccountID1,
GLAccountID2,
GLAccountID3,
GLAccountID4,
CategoryName,
RevenueStreamID,
RevenueType
into PREPSTREAM
from TABLE_A3
order by InvoiceIDByStore,
Priority, AssociationNumber
--2. anything below a revenueidstream of 7, assign that number to replace that 7. ex. if you have a revenuestreamid of 7, go to the revenuestreamid below it, grab that id, replace the 7 with that id
;WITH cte (rowID, SipID,
SaleInvoiceID, AssociationNumber, Priority, TotalPrice, TotalCost,
SerialNumber, ContractNumber, ActivatedThroughPAW,
DateCreatedatMidnight, ReceivedDate, InvoiceIDByStore,
Location, ProductIdentifier, Description, ShortDescription,
GLAccountID1, GLAccountID2, GLAccountID3, GLAccountID4,
CategoryName, RevenueStreamID, RevenueType) AS
(SELECT row_number() OVER (ORDER BY InvoiceIDByStore) AS rowID, SipID,
SaleInvoiceID, AssociationNumber, Priority, TotalPrice, TotalCost,
SerialNumber, ContractNumber, ActivatedThroughPAW, DateCreatedatMidnight,
ReceivedDate, InvoiceIDByStore, Location, ProductIdentifier, Description,
ShortDescription, GLAccountID1, GLAccountID2, GLAccountID3, GLAccountID4,
CategoryName, RevenueStreamID, RevenueType
FROM PREPSTREAM AS tn
)
SELECT rowID, SipID, SaleInvoiceID, AssociationNumber,
Priority, TotalPrice, TotalCost, SerialNumber,
ContractNumber, ActivatedThroughPAW, DateCreatedatMidnight,
ReceivedDate, InvoiceIDByStore, Location, ProductIdentifier,
Description, ShortDescription, GLAccountID1, GLAccountID2,
GLAccountID3, GLAccountID4, CategoryName, RevenueStreamID,
RevenueType, CASE WHEN cte.revenueStreamID = 7
THEN
( SELECT cte2.RevenueStreamID
FROM cte AS cte2
WHERE cte2.rowID = cte.rowID + 1)
ELSE cte.revenueStreamID
END as RevenueStreamID_Mod
FROM cte
order by InvoiceIDByStore, Priority, AssociationNumber
I am only a beginning DBA of about 8 months and I have been put in charge of changing our Sales Import Process of the company that I am working for so please bear with me. Any help would be greatly appreciated.

How do you know the order of items? (ie what's your SELECT statement that brings back the data set that contains your AWRPNS000343 record and the record to be adjusted)?
Here is what I have so far:
Select SipID, SaleInvoiceID, AssociationNumber, Priority, TotalPrice, TotalCost, SerialNumber, ContractNumber, ActivatedThroughPAW, DateCreatedatMidnight, ReceivedDate, InvoiceIDByStore, Location, ProductIdentifier, Description, ShortDescription, GLAccountID1, GLAccountID2, GLAccountID3, GLAccountID4, CategoryName, RevenueStreamID, RevenueType
into REVISEDTABLE
from TABLE_A3
order by InvoiceIDByStore, Priority, AssociationNumber
--add a column to put the line item description. ex RS_PHN
ALTER TABLE REVISEDTABLE
ADD LineItemInIIFFile varchar (50)
I have noticed when the ProductIdentifier = 'AWRPNS000343', The Association is 1, and the Priority is 3.
I am just needing a query that will find the ProductIdentifiers that equal 'AWRPNS000343'. When this is found (hence, the Association will be 1, and the priority will be 1), I need to go up 2 rows. When I go up 2 rows, I need to put 'RSPHN' in the LineItemInIIFFile column. I need for this process to continue until I reach the end of the records.