Table with list of database administrators in IBM i - db2

I want a list of users on an IBMi Machine who have access to create RCAC permissions (Create Mask, Row permission etc). Is this data stored in any table?

Not just in one table but I think this is a good start to list users with administative authority
SELECT
distinct AUTHORIZATION_NAME
FROM QSYS2.USER_INFO cross
join lateral (select * from table(systools.split(SPECIAL_AUTHORITIES,' '))) a
where element in ('*SECADM','*ALLOBJ')
union
SELECT
user_name
FROM qsys2.function_usage
where function_id = 'QIBM_DB_SECADM'
and usage = 'ALLOWED'

Related

Postgres SQL query group by get most recent record instead of an aggregate

This is a current postgres query I have:
sql = """
SELECT
vms.campaign_id,
avg(vms.open_rate_uplift) as open_rate_average,
avg(vms.click_rate_uplift) as click_rate_average,
avg(vms.conversion_rate_uplift) as conversion_rate_average,
avg(cms.incremental_opens),
avg(cms.incremental_clicks),
avg(cms.incremental_conversions)
FROM
experiments.variant_metric_snapshot vms
INNER JOIN experiments.campaign_metric_snapshot cms ON vms.campaign_id = cms.campaign_id
WHERE
vms.campaign_id IN %(campaign_ids)s
GROUP BY
vms.campaign_id
"""
whereby I get the average incremental_opens, incremental_clicks, and incremental_conversions per campaign group from the cms table. However, instead of the average, I want the most recent values for the 3 fields. See the cms table screenshot below - I want the values from the record with the greatest (i.e. most recent) event_id (instead of an average for all records) for a given group).
How can I do this? Thanks
It sounds like you want a lateral join.
FROM
experiments.variant_metric_snapshot vms
CROSS JOIN LATERAL (select * from experiments.campaign_metric_snapshot cms where vms.campaign_id = cms.campaign_id order by event_id desc LIMIT 1) cms
WHERE...
If you are after a quick and dirty solution you can use array_agg function with minimal change to your query.
SELECT
vms.campaign_id,
avg(vms.open_rate_uplift) as open_rate_average,
avg(vms.click_rate_uplift) as click_rate_average,
avg(vms.conversion_rate_uplift) as conversion_rate_average,
(array_agg(cms.incremental_opens ORDER BY cms.event_id DESC))[1] AS incremental_opens,
..
FROM
experiments.variant_metric_snapshot vms
INNER JOIN experiments.campaign_metric_snapshot cms ON vms.campaign_id = cms.campaign_id
WHERE
vms.campaign_id IN %(campaign_ids)s
GROUP BY
vms.campaign_id;

DB2 SQL: query for discovery if a group has only one client[CD_CLI]

I would need a query to return which groups have only one client and if possible if a client [CD_CLI] in a group with only him.
A try with the limited information provided
SELECT CD_GR_PSZD, CD_CLI
FROM CLI_GR_PSZD
WHERE CD_GR_PSZD in (
SELECT CD_GR_PSZD
FROM CLI_GR_PSZD
GROUP BY CD_GR_PSZD
HAVING count(*) = 1)
The subselect checks which group have only a single entry in the CLI_GR_PSZD table and the outer SELECT is ment to select whatever columns you need.

Get the list of users who are using database/schema/table

There is one use case in my project where i want to show the user who has got the access to use that database/schema/table in postgresql. Suppose I have created a database employee. So I want list the users who are accessing this database. Same for schema and tables. I tried this:
SELECT
*
FROM
information_schema.tables
WHERE
table_schema not in ('pg_catalog', 'information_schema') AND
table_schema not like 'pg_toast%'
But it gives information about current user has access to. I want the list of accessing users that are using that database/table/schema/column.
You can use the function has_table_privilege() to achieve what you want.
select has_table_privilege('user_name', 'schema.table', 'select');
More info here.
I suppose you need to be a superuser to get all results. Below shows all the users who have privileges, not necessarily whether they are accessing the tables.
Now you can tweak the query to join all users with list of tables -
SET search_path TO public,
schema1;
SELECT *,
usename,
has_table_privilege(usename, table_schema||'.'||table_name, 'select') as has_privilege
FROM SVV_TABLES
JOIN PG_USER
ON 1=1
WHERE table_schema = 'schema1';

How to select row from table but if it doesn't exist in that table, select from different table?

This may be a really simple problem that I'm over-looking but i have this query:
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
(the customer id will vary, I'm using a node js script to grab a bunch of different customer ids to find out if they're accountants or not)
If there is no result, I want to search in a table called deleted_users for the same customer id ie:
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
Is there a way to do this within Postgresql?
SELECT coalesce(u.is_accountant, d.is_accountant)
from deleted_users d
full outer join users u on u.id = d.id
where 'cus_4znUZe3lAy26FT' in (d.customer_id, u.customer_id)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union all
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
and not exists
(SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT')
or, as you are interested to know if user "is (or was)" accountant:
(does it matter in which table the user is? Agree?)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'

How to list tables from accessible via database links?

I have an access to a database, and sure I can get all tables/columns accessible for me just using:
select * from ALL_TAB_COLUMNS
I can also access some tables using "#", as I understand a database link mechanism, like this:
select * from aaa.bbb_ddd#ffgh where jj = 55688
where aaa.bbb_ddd#ffgh corresponds to some table with a column jj
BUT I don't see this aaa.bbb_ddd#ffgh table in ALL_TAB_COLUMNS.
How can I request all tables (and columns inside them) accessible for me via these database links (or so)?
You can't, easily, get all columns accessible via all database links; you can get all columns accessible via one database link by querying ALL_TAB_COLUMNS on the remote database
select * from all_tab_columns#<remote_server>
where <remote_server> in your example would be ffgh.
If you want to get this same information for all database links in your current schema, you'd either have to manually enumerate them and UNION the results together:
select * from all_tab_columns#dblink1
union all
select * from all_tab_columns#dblink2
Or, do something dynamically.
As Justin says, it's clearer if you add which database the data is coming from; you can do this either by just writing it in the query:
select 'dblink1' as dblink, a.* from all_tab_columns#dblink1 a
union all
select 'dblink2', a.* from all_tab_columns#dblink2 a
Or by using an Oracle built-in to work, for example the GLOBAL_NAME table (there's lots more ways):
select db1g.global_name, db1a.*
from all_tab_columns#dblink1 db1a
cross join global_name#dblink1 db1g
union all
select db2g.global_name, db2a.*
from all_tab_columns#dblink2 db2a
cross join global_name#dblink2 db2g