0
SELECT bau.username, cc.key
FROM ba_users bau
INNER JOIN billings ba
  ON ba.id = bau.billing_account_id
INNER JOIN accc_once_only_cc_map acooc
  ON acooc.billing_account_id = ba.id
INNER JOIN account_components_map acm
  on acooc.account_component_id = acm.account_component_id
INNER JOIN cuoffers cuo
  ON cuo.id = acm.customized_offer_id
INNER JOIN charges cc
  ON cc.key = acooc.charge_cluster_key;

result is for example

USERNAME        KEY
1182925980_0    RT_FEE
1182926277_0    RT_FEE
1182926574_0    RT_FEE
1182924785_0    RT_FEE
1182925980_0    RT_FEE2
1182923514_0    RT_FEE
1182932481_0    RT_FEE
1182936581_0    RT_FEE
1182941631_0    RT_FEE

now I would like to found which user has more then one key and print out the user with keys.

like:

1182925980_0

has keys

RT_FEE and RT_FEE2

How to do this with plsql?

mbrc
  • 3,523
  • 13
  • 40
  • 64

3 Answers3

4

The first part (finding users with > 1 key)

WITH CTE AS
(
  SELECT bau.username, cc.key
  FROM ba_users bau
  INNER JOIN billings ba
    ON ba.id = bau.billing_account_id
  INNER JOIN accc_once_only_cc_map acooc
    ON acooc.billing_account_id = ba.id
  INNER JOIN account_components_map acm
    on acooc.account_component_id = acm.account_component_id
  INNER JOIN cuoffers cuo
    ON cuo.id = acm.customized_offer_id
  INNER JOIN charges cc
    ON cc.key = acooc.charge_cluster_key
)
SELECT username, COUNT(*)
  FROM CTE
  GROUP BY username
  HAVING COUNT(*) > 1;      

The second part of your query (showing a list of all keys projected in a single column) will will depend on whether you can use listagg or not.

WITH CTE AS
(
  SELECT bau.username, cc.key
  FROM ba_users bau
  INNER JOIN billings ba
    ON ba.id = bau.billing_account_id
  INNER JOIN accc_once_only_cc_map acooc
    ON acooc.billing_account_id = ba.id
  INNER JOIN account_components_map acm
    on acooc.account_component_id = acm.account_component_id
  INNER JOIN cuoffers cuo
    ON cuo.id = acm.customized_offer_id
  INNER JOIN charges cc
    ON cc.key = acooc.charge_cluster_key
)
SELECT username, LISTAGG(key, ', ') WITHIN GROUP (ORDER BY key) the_keys
  FROM CTE
  GROUP BY username
  HAVING COUNT(*) > 1;      
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
0

You can try something like this:

SELECT username,key,COUNT(*)
FROM ba_users
GROUP BY username,key
HAVING COUNT(*) > 1

This should display the username with more than one key, hope this helps.

XcisioN
  • 107
  • 9
0

try this query:

SELECT username,COUNT(username) FROM
(SELECT bau.username, cc.key
FROM ba_users bau
INNER JOIN billings ba
  ON ba.id = bau.billing_account_id
INNER JOIN accc_once_only_cc_map acooc
  ON acooc.billing_account_id = ba.id
INNER JOIN account_components_map acm
  on acooc.account_component_id = acm.account_component_id
INNER JOIN cuoffers cuo
  ON cuo.id = acm.customized_offer_id
INNER JOIN charges cc
  ON cc.key = acooc.charge_cluster_key)t1 
GROUP BY username
HAVING(COUNT(username) > 1);
Hamidreza
  • 3,038
  • 1
  • 18
  • 15