2

超级简化的代码:

SELECT lots_of_stuff.
 , A.more_stuff
 , B.stuff
 , C.things
FROM
 table A,
 table B,
 table C,
where (B.more_stuff = A.stuff)
 and things and stuff
 and lots more things
 and this query has so much crap believe me
 and finally
 and count(select c.things from table C where c.things like 'CRED') 
    > count(select c.things from table C where c.things like 'PUR')
;

所以问题是最后一点不起作用(我确定我完全做错了,这只是对如何做的一个猜测。)我想知道是否有人可以给我一些建议。

我要做的只是在特定字段中包含“CRED”的行数大于特定字段中包含“PUR”的行数的情况下返回所需的字段。(相同的字段,如果可以简化事情的话。)无论“CRED”或“PUR”是较长单词(信用/购买)的一部分还是单独存在,我都希望它们被退回。不过,它们将永远是大写字母。

编辑:

我正在寻找的只是我指定的那些列

| More_Stuff | Stuff | Things |
|   dshsdh   |  dfh  |  tjra  |
|   ddh      |  ash  |  ytra  |
|   shsdh    |  fgh  |  sayh  |
|   hsdh     |  gnh  |  tshn  |

但只有信用代码多于购买计划的客户的行。因此,如果他们在“c.things”中有 3 个不同的条目,例如“PHONE-CREDIT”或“OFFSET CRED”。和“c.things”中的 2 个不同条目,例如“12 M PURCH PLAN”或“PROMO PURCHASE 36”,我希望显示他们的信息。因此,当任何信用代码的行数大于任何购买计划的行数时。我当前的非简化查询已经设置为对所有客户进行排序,我只需要根据此过滤器指定哪些客户。

4

3 回答 3

0

我想你想要这样的东西

 WITH cred_count AS
 (
    SELECT index_field, SUM(CASE WHEN field='CRED' THEN 1 ELSE 0 END) AS cred_count
    FROM some_table
    GROUP BY index_field
 ), pur_count AS
 (
    SELECT index_field, SUM(CASE WHEN field='PUR' THEN 1 ELSE 0 END) AS pur_count
    FROM some_table
    GROUP BY index_field
 )
 SELECT somestuff
 FROM some_table
 LEFT JOIN cred_count ON some_table.index_field = cred_count.index_field
 LEFT JOIN pur_count ON some_table.index_field = pur_count.index_field
 WHERE COALESCE(cred_count.cred_count,0) > COALESCE(pur_count.pur_count,0)

注意:您可以将 WHEN 部分更改为您想要计算的任何值(例如WHEN field like '%PUR%',将计算包含字符串 PUR 的行

此外,我假设没有条目计为 0——您在这种情况下的业务规则可能会有所不同。

于 2018-03-23T17:40:28.170 回答
0

这可以使用 Oracle 中的 WITH 子句来实现。以下代码可能与您正在寻找的内容接近 -

with ds1 as
(
    SELECT 
       lots_of_stuff
       , A.more_stuff
       , B.stuff
       , C.things,
       count(c.things) AS COUNT_CRED
    FROM
       table A,
       table B,
       table C,
    where 
       (B.more_stuff = A.stuff)
       and things and stuff
       and lots more things
       and this query has so much crap believe me
       and finally
       and c.things like 'CRED%'
   group by 
       lots_of_stuff.
       , A.more_stuff
       , B.stuff
       , C.things
   ),
 ds2 as
 (
    SELECT 
       lots_of_stuff.
       , A.more_stuff
       , B.stuff
       , C.things,
       count(c.things) AS COUNT_PUR
    FROM
       table A,
       table B,
       table C,
    where 
       (B.more_stuff = A.stuff)
       and things and stuff
       and lots more things
       and this query has so much crap believe me
       and finally
       and c.things like 'PUR%'
    group by 
       lots_of_stuff.
       , A.more_stuff
       , B.stuff
       , C.things
)
 SELECT DS1.*, ds2.*
 from ds1, ds2
 where count_cred > COUNT_PUR
 ;
于 2018-03-23T17:42:20.093 回答
-1

要过滤较长单词中的 CRED 和 PUR,请在查询中使用通配符,例如 %。

like '%CRED%'  -- if CRED can be anywhere in the string
like 'CRED%'  -- if CRED is always at the beginning of the string

请注意,如果它始终位于字符串的开头,则可以在列上使用索引以使其运行得更快。

您不能在 where 子句中使用 count() 之类的聚合(除非 Oracle 支持??)

您可以对行进行分组并使用 HAVING 但在您的情况下,实际上将 count() 移动到子查询中更容易。

   SELECT lots_of_stuff.
 , A.more_stuff
 , B.stuff
 , C.things
FROM
 table A,
 table B,
 table C,
where (B.more_stuff = A.stuff)
 and things and stuff
 and lots more things
 and this query has so much crap believe me
 and finally
 and (select count(c.things) from table C where c.things like '%CRED%') 
    > (select count(c.things) from table C where c.things like '%PUR%')
;
于 2018-03-23T17:40:39.463 回答