简体   繁体   中英

Automation of SQL case statement with multiple conditions

I have been given the task of automating a rather long/complex piece of SQL code (Teradata) that has a rather length case statement for identifying multiple criteria across multiple columns.

The criteria for this case statement changes constantly depending on the analysis requirements and tracks customers purchases against promoted offers for marketing. It deals with travel-related data.

A short example of what this statement sometimes looks like is (just imagine it 10x longer):

CASE 
 WHEN ORIGIN_LOCATION = 'X' AND DESTINATION = 'Y' AND TRAVEL_DATE BETWEEN '2019-01-02' AND '2020-01-02' 
 AND CAR_TYPE = 'A' THEN 'PROMO' 
 WHEN DESTINATION = 'X' AND ORIGIN_LOCATION = 'Y' AND TRAVEL_DATE BETWEEN '2019-01-02' AND '2020-01-02' 
 AND CAR_TYPE = 'A' AND RETURN_INDC = 'Y' THEN 'PROMO' 
ELSE 'NON-PROMO' END AS PROMO_INDC

I need to be able to automate this case statement so that analysts with minimal SQL experience can run the code without having to hand-write this case statement each time.

I've tried a number of different solutions such as storing the criteria (it gets delivered to us as an excel file) in temp tables then using a JOIN condition on the source data, but this does not work. I also looked into macros and stored procedures, but I do not have permission to use these functions - so that's not going to work either.

Does anyone have any thoughts or ideas on how I could potentially automate something like this? Happy to use other programming languages like python if that will do the trick - but really just looking for thoughts/ideas.

Thanks.

as data come via Excel they are (at least some kind of) structured. I would upload these to a reference table and join it (I read your comment, but maybe the right question is about a robust join). In addition I would make the reference table temporal, so you can easily debug over time. With Teradata Studio or some small Python Script you can give the Upload to the ReferenceTable even to the Business, to ease your life.

I had some similar task some years ago and a join + reference table was the most easy & stable way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM