有条件地修改SQL查询 [英] Conditionally modify SQL query

查看:108
本文介绍了有条件地修改SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sql查询,比如下面的表格:

从表t中选择a,b,c,其中ta =hello和tb =there和tc =world group by a,b,c

我想要一个布尔条件(d == true)来确定上面的计算,这样当d设置为true时,执行上面的查询,当d为false时,查询将缩减为以下内容并编译:

从表t中选择a,b,其中ta =hello,tb =theregroup by a,b。



关于如何使用oracle 11g(sql / pl-sql / t-sql)实现这一点的任何建议?



我尝试了什么:



我尝试过设置标志和执行查询的简单逻辑,但我需要根据预设条件有条件地编译部分查询。

I have a sql query, say of the following form:
select a, b, c from table t where t.a = "hello" and t.b = "there" and t.c = "world" group by a,b,c
I would like to have a Boolean condition (d == true) to determine the computation above such that when say d is set to true, the above query get executed, and when d is false, the query gets reduced to the following and compiled:
select a, b from table t where t.a = "hello" and t.b = "there" group by a,b.

Any suggestions as to how this could be achieved using oracle 11g (sql/pl-sql/t-sql)?

What I have tried:

I have tried a simple logic of setting the flag and executing the query but I need to conditionally compile part of the query based on a preset condition.

推荐答案

你可以将case放在group by中。检查 - SQL查询中的动态和条件分组 - 从单个查询获得灵活的结果(哦和CUBE运算符的有用情况) - AMIS Oracle和Java Blog [ ^ ]。
You can put case in group by. Check - Dynamic and Conditional grouping in SQL queries - for flexible results from single query (oh and a useful case for the CUBE operator) - AMIS Oracle and Java Blog[^].


这篇关于有条件地修改SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆