I am trying to query a Google Sheet from a separate tab. I want to total minutes, between a certain date range, with a particular userid. I want the dates to be entered by the user as well as the userid.
My query is currently:
select SUM(Col28) where Col1 is not null and Col1 <> 'Timestamp' and Col1 >= '"&$A4&"' AND Col1 <= '"&$B4&"' and Col3 = 'ROS756'
What I get in the cell that contains the query is simply the word sum. I am not getting an error and feel like I am so close!!
try:
"select sum(Col28)
where Col1 >= date '"&TEXT(A4; "yyyy-mm-dd")&"'
and Col1 <= date '"&TEXT(B4; "yyyy-mm-dd")&"'
and Col3 = 'ROS756'"
Related
I want to divide two Counts in a single query, but the DATE_TRUNC causes some issues
So far I have a query in postgre looking like this:
SELECT DATE_TRUNC('month', "Date") as date,
COUNT(*) as AllTransactions,
COUNT(*) filter (where "State"='ACCEPTED') as Accepted,
FROM "Acceptance_Report"
GROUP BY 1
ORDER BY 1
It returns this:
Date
AllTransactions
Accepted
2019-01-01
930
647
2019-02-01
840
589
Now I need to get the percentage, so it should be Accepted/AllTransactions*100
I understand that I could create another table and use INSERT , but I feel like there is another easy way to do it in a single query.
Do you have any ideas?
So if you want to divide them, repeat the expressions. But it's important to convert one of the counts to a numeric value, otherwise it would result in an integer division (where 1/5 yields 0)
SELECT DATE_TRUNC('month', "Date") as date,
COUNT(*) as AllTransactions,
COUNT(*) filter (where "State"='ACCEPTED') as Accepted,
COUNT(*)::numeric / COUNT(*) filter (where "State"='ACCEPTED') as pct
FROM "Acceptance_Report"
GROUP BY 1
ORDER BY 1
If you don't want to repeat the expressions, you can use a derived table:
select "date",
alltransactions,
accepted,
alltransactions::numeric / accepted as pct
FROM (
SELECT DATE_TRUNC('month', "Date") as date,
COUNT(*) as AllTransactions,
COUNT(*) filter (where "State"='ACCEPTED') as Accepted
FROM "Acceptance_Report"
GROUP BY 1
ORDER BY 1
) t
You can use AVG() aggregate function over the boolean expression "State" = 'ACCEPTED' converted to 1 for true and 0 for false:
SELECT DATE_TRUNC('month', "Date") AS date,
COUNT(*) AS AllTransactions,
COUNT(*) filter (where "State" = 'ACCEPTED') AS Accepted,
100 * AVG(("State" = 'ACCEPTED')::int) AS percentage
FROM "Acceptance_Report"
GROUP BY 1
ORDER BY 1;
See a simplified demo.
I have the following query and I would like to add all the views of a specific date and if there is no view, I would like to get date and sum as 0 as shown below.
SELECT SUM("videoView"."views"), "videoView"."startDate"::timestamp::date
FROM "videoView"
WHERE "videoView"."videoId" =23
AND "videoView"."views"
BETWEEN = '2021/11/25' AND '2021/11/28'
GROUP BY "videoView"."startDate"::timestamp::date
The result I want is:
sum date
3 2021/11/25
0 2021/11/26
0 2021/11/27
4 2021/11/28
The result i am getting is
sum date
3 2021/11/25
4 2021/11/28
You need a calendar table here which holds all the dates which you want to appear in the report. The need for this table is that your current data set may be entirely missing certain dates for which there simply isn't any data. Consider:
WITH dates AS (
SELECT '2021-11-25'::date AS dt UNION ALL
SELECT '2021-11-26'::date UNION ALL
SELECT '2021-11-27'::date UNION ALL
SELECT '2021-11-28'::date
)
SELECT d.dt, COALESCE(SUM(v.views), 0) AS numViews
FROM dates d
LEFT JOIN videoView v
ON v.startDate = d.dt AND
v.videoId = 23
WHERE
d.dt BETWEEN '2021-11-25'::date AND '2021-11-28'::date
GROUP BY d.dt
ORDER BY d.dt;
I am trying to pull unique active users before a date.
So specifically, I have a date range (let's say August - November) where I want to know the cumulative unique active users on or before a day within a month.
So, the pseudocode would look something like this:
SELECT COUNT(DISTINCT USERS) FROM USER_DB
WHERE
Month = [loop through months 8-11]
AND
DAY <= [day in loop of 1:31]
The output I desire is something Like this
step-by-step demo: db<>fiddle
SELECT
mydate,
SUM( -- 3
COUNT(DISTINCT username) -- 1, 2
) OVER (ORDER BY mydate) -- 3
FROM t
GROUP BY mydate -- 2
GROUP BY your date and count the users
Because you don't want to count ALL user accesses, but only one access per user and day, you need to add the DISTINCT
This is a window function. This one aggregates all counts which where previously done cumulatively.
If you want to get unique user over ALL days (count a user only on its first access) you can filter the users with a DISTINCT ON clause first:
demo: db<>fiddle
SELECT DISTINCT ON (username)
*
FROM t
ORDER BY username, mydate
This yields:
SELECT
mydate,
SUM(
COUNT(*)
) OVER (ORDER BY mydate)
FROM (
SELECT DISTINCT ON (username)
*
FROM t
ORDER BY username, mydate
) s
GROUP BY mydate
Hi I am joining a table with range of 1 month days, to get the per day count based of join table(base table).
For that I using left outer join to get count of per day.
where my base table is as shown below (table name REGISTRIERUNG]
And I have create range of one month using below query
SELECT TO_DATE ('01-10-2017', 'dd-mm-yyyy') + ROWNUM - 1 AS daterange
FROM all_objects
WHERE ROWNUM <=
TO_DATE ('30-10-2017', 'dd-mm-yyyy')
- TO_DATE ('01-10-2017', 'dd-mm-yyyy')
+ 1;
but I getting count 1 for date where there now record matching with range table
instead of 0 count.
I am using below query for final result.
SELECT TRUNC (a.daterange), COUNT (a.daterange)
FROM (SELECT TO_DATE ('01-10-2017', 'dd-mm-yyyy') + ROWNUM - 1
AS daterange
FROM all_objects
WHERE ROWNUM <=
TO_DATE ('30-10-2017', 'dd-mm-yyyy')
- TO_DATE ('01-10-2017', 'dd-mm-yyyy')
+ 1) a
LEFT OUTER JOIN
REGISTRIERUNG b
ON TRUNC (a.daterange) = TRUNC (b.MODIFIKATIONZEIT)
GROUP BY TRUNC (a.daterange)
ORDER BY TRUNC (a.daterange) ASC;
You should not count rows based on the column that is always populated (in your query a.daterange is always populated because this column from your inline view has all the dates in a month). Rather, you should count number of rows from the table that is outer-joined to the inline view with generated dates. Note that count function will not take into account rows that have null value in the column modifikationzeit.
For instance:
select a.daterange,
count(b.modifikationzeit)
from (select to_date('01-10-2017', 'dd-mm-yyyy') + level - 1 as daterange
from dual
connect by level <= to_date('31-10-2017', 'dd-mm-yyyy') -
to_date('01-10-2017', 'dd-mm-yyyy') + 1) a
left outer join registrierung b
on a.daterange = trunc(b.modifikationzeit)
group by a.daterange
order by a.daterange;
I have removed unnecessary trunc's, and converted query from all_objects to the one that uses connect by clause. I also fixed date generation for October - it has 31 days, not 30 as per your example.
I am working on PostgreSQL, below is my query which returns min and max dates from the table based on the condition.
select min(created_date),
max(created_date)
from myTable
where mode='auto'
and status='released'
group by mode;
Result:
min max
date date
2012-01-15 2016-11-24
created_date is of type date.I want the result to be displayed in single column as below.
created_date
date
2012-01-15
2016-11-24
You should use a UNION or UNION ALL:
select min(created_date),
from myTable
where mode='auto'
and status='released'
group by mode
UNION ALL
select max(created_date)
from myTable
where mode='auto'
and status='released'
group by mode;