How to export multiple queries from DataGrip - postgresql

I have a file that contains multiple select queries.
I run these on a weekly basis to generate internal company reports
For example (these are not the actual queries):
-- Select number of users
SELECT count(id) from users;
-- Select number of active users
SELECT count(id) from users where active = true;
-- Select number of logins this week
SELECT count(id) from users where last_login > current_date - interval '1 week';
--- etc...
Is there a way, in DataGrip, to run all of these queries and export them to csv files?
I am able to run each query and then export the dataset, however, is a time consuming process doing this one by one.

I believe there isn't a convenient way.
As a workaround, will combining all queries into one solve your problem?
It's not the most elegant way, but you'll get all the data displaying in a single result:
SELECT
(SELECT COUNT(id) FROM users) AS "Number of users",
(SELECT COUNT(id) FROM users WHERE active = TRUE) as "Number of active users",
(SELECT COUNT(id) FROM users WHERE last_login > CURRENT_DATE - INTERVAL '1 week') as "Number of logins this week"

It's possible. Select all queries and then use Execute To Files:

Related

Postgres: How to count records with ranged condition?

Working with table with columns:
(PK)sales_log_id
user_id
created_at
With sales_log_id to represent transaction activities for users.
I have been able to query how many users have x amount of transactions.
Now I would like to find out how many users have eg. > 10 AND < 20 transactions in a certain period of time.
Being new with databases and Postgres, I'm learning that you can do a query and another query with the previous result (subquery). So I tried to query first how many users are having < 30 transactions in June and later query the result for users having > 10 transactions.
SELECT COUNT(DISTINCT t.user_id) usercounter
FROM (
SELECT user_id, created_at, sales_log_id
FROM sales_log
WHERE created_at BETWEEN
'2019-06-01' AND '2019-06-30'
GROUP BY user_id, created_at, sales_log_id
HAVING COUNT (sales_log_id) <30
)t
GROUP BY t.user_id
HAVING COUNT (t.sales_log.id) >10;
But it produced an error
ERROR: missing FROM-clause entry for table "sales_log"
LINE 11: HAVING COUNT (t.sales_log.id) >10;
^
SQL state: 42P01
Character: 359
Can anyone please provide the correct way to do this?
I think it is as simple as
SELECT count(*)
FROM (
SELECT user_id, COUNT(*)
FROM sales_log
WHERE created_at BETWEEN '2019-06-01' AND '2019-06-30'
GROUP BY user_id
HAVING COUNT (sales_log_id) BETWEEN 11 AND 29
) AS q;
Only add DISTINCT to a query if you really need it.
It is just one word, but it can have a big performance penalty.

Condition and max reference in redshift window function

I have a list of dates, accounts, and sources of data. I'm taking the latest max date for each account and using that number in my window reference.
In my window reference, I'm using row_number () to assign unique rows to each account and sources of data that we're receiving and sorting it by the max date for each account and source of data. The end result should list out one row for each unique account + source of data combination, with the max date available in that combination. The record with the highest date will have 1 listed.
I'm trying to set a condition on my window function where only rows that populate with 1 are listed in the query, while the other ones are not shown at all. This is what I have below and where I get stuck:
SELECT
date,
account,
data source,
MAX(date) max_date,
ROW_NUMBER () OVER (PARTITION BY account ORDER BY max_date) ROWNUM
FROM table
GROUP BY
date,
account,
data source
Any help is greatly appreciated. I can elaborate on anything if necessary
If I understood your question correctly this SQL would do the trick
SELECT
date,
account,
data source,
MAX(date) max_date
FROM (
SELECT
date,
account,
data source,
MAX(date) max_date,
ROW_NUMBER () OVER (PARTITION BY account ORDER BY max_date) ROWNUM
FROM table
GROUP BY
date,
account,
data source
)
where ROWNUM = 1
If you do not need the row number for anything other than uniqueness then a query like this should work:
select distinct t.account, data_source, date
from table t
join (select account, max(date) max_date from table group by account) m
on t.account=m.account and t.date=m.max_date
This can still generate two records for one account if two records for different data sources have the identical date. If that is a possibility then mdem7's approach is probably best.
It's a bit unclear from the question but if you want each combination of account and data_source with its max date making sure there are no duplicates, then distinct should be enough:
select distinct account, data_source, max(date) max_date
from table t
group by account, data_source

PostgreSQL Query: How to get Group-By to Roll Up

Data:
There are 2 possible statuses, 0 or 1
There are a variety of different Sub Statuses and sources
I am looking to create a query that will accomplish the following:
By Week/Year, show the number of unique instances of each status, sub status and source combination. I do have UIDs I can count for "number of instances".
I have written the following:
SELECT date_part('week', date) as week, date_part('year', date) as year, active_date, status, sub_status, source, id
FROM public.users
WHERE status < 2
GROUP BY created_at, active_date, status, sub_status, source, id
ORDER BY created_at DESC
Which accomplished the following:
How do I get these to roll up?
Thanks!
It turns out that the terms 'week' and 'year' are reserved words. by replacing them with 'theweek' and 'theyear', as well as adding in a count function, I was able to roll these up.
SELECT date_part('week', created_at) as theweek, date_part('year', created_at) as theyear, status, sub_status, source, Count(*)
FROM public.users
WHERE status < 2
GROUP BY theweek, theyear, status, sub_status, source
ORDER BY theweek DESC
Results looked like the following:
Thanks everyone!

Find rows which have different attribute value in ONE day for same product. (Postgresql)

can someone help me to write a query?
I have for example columns:
Date
product_key
category_code
In one day I expect to have same category_code for one product, but I want to check this with SQL.
Thank you.
If you want to find the day, the product_key and the category_code that doubles, You should use query like this:
SELECT
date,
product_key,
category_code,
count(1)
FROM your_table
GROUP BY date, product_key, category_code
HAVING count(1) > 1;
You can group your results by date and product, and use count and distinct to find if there is more than one category code for a product. You can then filter rows having more than 1 distinct category in the group.
SELECT
Date, product_key, count(distinct category_code) AS categories
FROM
my_table
GROUP BY
Date, product_key
HAVING
count(distinct category_code) > 1

Query Oracle SqlDeveloper

I want to write a SELECT statement to create a list of jobs containing the job_title and the mean value of the two fields MIN_SALARY and MAX_SALARY sorted from the highest mean value to the lowest.
I wrote this:
select job_title, MAX_SALARY, MIN_SALARY, count(*)
from HR.EMPLOYEES, HR.JOBS
order by count(*) desc
group by j.job_title;
but it gives error:
ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended"
I think that you should have this query
select job_title, MAX_SALARY, MIN_SALARY, count(*)
from HR.EMPLOYEES, HR.JOBS
group by job_title
order by count(*) desc
I have reordered the clauses and removed the spurious j
See - select