简体   繁体   English

具有多个条件的 SQL case 语句的自动化

[英]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.我被赋予了自动化一段相当长/复杂的 SQL 代码 (Teradata) 的任务,该代码具有相当长的 case 语句,用于跨多个列识别多个条件。

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):这个语句有时看起来像一个简短的例子(想象一下它长 10 倍):

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.我需要能够自动执行此 case 语句,以便具有最少 SQL 经验的分析师可以运行代码,而不必每次都手写此 case 语句。

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.我尝试了多种不同的解决方案,例如在临时表中存储标准(它作为 excel 文件交付给我们),然后在源数据上使用 JOIN 条件,但这不起作用。 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.如果可以的话,很高兴使用其他编程语言,如 python - 但实际上只是在寻找想法/想法。

Thanks.谢谢。

as data come via Excel they are (at least some kind of) structured.由于数据来自 Excel,因此它们(至少在某种程度上)是结构化的。 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.使用 Teradata Studio 或一些小的 Python 脚本,您甚至可以将上传到 ReferenceTable 给业务,以减轻您的生活。

I had some similar task some years ago and a join + reference table was the most easy & stable way.几年前我有一些类似的任务,连接 + 参考表是最简单和稳定的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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