Alright, so I have a table let's call Profiles and another table called Rates. Due to some application changes over time, some accounts are missing their Rates data. I need to write an INSERT that takes all the IDs from Profiles and checks to see if they match to any rows (ProfileID column) in Rates. If there are no matches for Profiles.ID to Rates.ProfileID, then I need to insert 4 rows with default values and the ProfileID.
Searching the stack, most people only need to insert one row, so I'm finding it challenging to connect CROSS JOIN with multiple VALUES entries, just not sure which is the right way to go with it. I also don't have vast experience with SQL.
I'm somewhere around here:
INSERT into Rates (Discount, MinVal, MaxVal, ProfileID)
SELECT ID FROM [DBName].[Prefix].[Profiles]
CROSS JOIN (VALUES ((0.000, 0.00000, 249.00000, ID),
(0.000, 250.00000, 499.00000, ID),
(0.000, 500.00000, 999.00000, ID),
(0.000, 999.00000, 99999.99999, ID)))
-- ^ this doesn't work, it gives column name invalid
LEFT JOIN Rates ON Profiles.ID = Rates.ProfileID
WHERE Rates.ProfileID IS NULL
Am I close or way off?
1条答案
按热度按时间guykilcj1#
Thanks to Thom A for the nudge. Leaving for Posterity