I have this SQL:
SELECT (Min(Time_In) & " to " & Max(Time_Out)) AS [Week Of],
Round((Sum(DATEDIFF("n", Time_In, Time_Out))/Count(DATEDIFF("n", Time_In, Time_Out))),2) AS [Avg Min of Jog]
FROM SomeTable
WHERE len(Time_In) > 0 AND len(Time_Out) > 0
AND #01/01/2012# <= Time_In AND #12/31/2012# >= Time_In
GROUP BY DatePart('ww',Time_In);
Which picks out average times of a jog per week. I would also like to include a count how many times jogged per week, which I'm trying to do by counting the jog_id where SomeTable could have 5 entries for one jog_id.
I have tried:
SELECT (Min(Time_In) & " to " & Max(Time_Out)) AS [Week Of],
Round((Sum(DATEDIFF("n", Time_In, Time_Out))/Count(DATEDIFF("n", Time_In, Time_Out))),2) AS [Avg Min of Jog],
Count(distinct jog_id) AS [Num Jogs]
FROM SomeTable
WHERE len(Time_In) > 0 AND len(Time_Out) > 0
AND #01/01/2012# <= Time_In AND #12/31/2012# >= Time_In
GROUP BY DatePart('ww',Time_In);
But this is giving me Syntax error (missing operator) in query expression 'Count(distinct jog_id)'.
What am I missing?
You can't do this in Access without using a sub-query.
EX:
SELECT Count(*)
FROM
(SELECT DISTINCT Name FROM table1);
Here is a detailed article on the topic:
Access refuses to do good in nearly all circumstances, one of which is the support of the Count(distinct x).
Related
Thanks for the help on this matter, I'm new with SQL. I'm trying to get a sub-count of Jedi who had more than 2 padawans last month. I tried putting the condition in WHERE but I get an error saying I can't include aggregates in it. I also tried using a CASE but kept getting a syntax error there too. Any help on this would be incredible. Thank you so much!
SELECT COUNT(DISTINCT old_republic.jedi_id), old_republic.region_id
FROM jedi_archives.old_repulicdata old_republic
WHERE old_republic.republic_date >= '2022-06-01' AND old_republic.republic_date <= '2022-06-30' AND COUNT(old_republic.padawan)>2
GROUP BY old_republic.region_id
ORDER BY old_republic.region_id
SELECT old_republic.jedi_id CASE (
WHEN Count(old_republic.padawan)>2
THEN 1
ELSE 0 End), old_republic.region_id
FROM jedi_archives.old_repulicdata old_republic
WHERE old_republic.republic_date >= '2022-06-01' AND old_republic.republic_date <= '2022-06-30'
GROUP BY old_republic.region_id
ORDER BY old_republic.region_id
I can't comment to ask for a fiddle, but from what you've written, you're probably looking for the HAVING clause.
Assuming that padawan denotes the number of Padawans:
SELECT region_id, jedi_id, sum(padawan)
FROM jedi_archives.old_republicdata
WHERE republic_date >= '2022-06-01'
AND republic_date <= '2022-06-30'
GROUP BY region_id, jedi_id
HAVING sum(padawan) > 2;
This query will return the sum of Padawans for each Jedi per region who had more than two Padawans last month in one region (if you don't want to take the region into account, remove it from the SELECT and GROUP BY clause). Other Jedis won't appear in the result.
You can use the CASE expression, too, in order to indicate whether a Jedi had more than two padawans:
SELECT region_id, jedi_id,
CASE WHEN sum(padawan) > 2 THEN 1 ELSE 0 END AS more_than_2_padawans
FROM jedi_archives.old_republicdata
WHERE republic_date >= '2022-06-01'
AND republic_date <= '2022-06-30'
GROUP BY region_id, jedi_id;
I'm not entirely sure without sample data. But I think using the HAVING clause could solve your question.
SELECT COUNT(jedi_id) as jedi_id, region_id FROM tableA
WHERE republic_date between '2022-05-20' and '2022-05-25'
GROUP BY region_id
having padawan > 2
db fiddle
I am trying to select the max date in a table that has a Booking Date and a Written Premium value for that date. I want the newest date that has Written Premium (not equal to Zero).
In the above table I want, or expect the 4th Row in my query (7/28/2021, 330000), but I get the first row
(8/20/21, 0)
This is the query I run:
SELECT
MAX(booking_date) AS [Max Booking Date]
FROM
DW.dbo.Table1
GROUP BY
booking_year
HAVING
SUM(written_premium) <> 0
AND booking_year = '2021'
I think this is summing all the written premium, not over the rows so I am just getting the max booking date. Maybe I need a PARTITION BY function or something? I am stuck on this and would appreciate any help on how to best do this.
Thanks,
Brian
I think there are multiple options, but one could be:
SELECT TOP 1 booking_date, written_premium
FROM DW.dbo.Table1
WHERE written_premium <> 0
ORDER BY booking_date DESC
If all you want is the date then there is no need of group by and a HAVING clause.
Set your conditions in the WHERE clause:
SELECT MAX(booking_date) AS [Max Booking Date]
FROM DW.dbo.Table1
WHERE booking_year = '2021' AND written_premium <> 0;
If you want both columns:
SELECT TOP 1 booking_date AS [Max Booking Date], written_premium
FROM DW.dbo.Table1
WHERE booking_year = '2021' AND written_premium <> 0
ORDER BY booking_date DESC;
I'd like to create a SortID column to sort the results in different case categories. Each case category represents a revenue range. I grouped by the results on the case statements to get how many orders for each case. Now I am stuck with this SortID creating issue. Below please find my current query. Please suggest me where i can put the index creation statement in my query.Many thanks in advance!
select SalesAmountCategory, count(*) as Orders
from
(Select case
when ((SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
when ((SalesAmount-TaxAmt-Freight)>=50000) then '$50000-$100000'
when ((SalesAmount-TaxAmt-Freight)>=10000) then '$10000-$50000'
when ((SalesAmount-TaxAmt-Freight)>=5000) then '$5000-$10000'
when ((SalesAmount-TaxAmt-Freight)>=2500) then '$2500-$5000'
when ((SalesAmount-TaxAmt-Freight)>=1000) then '$1000-$2500'
when ((SalesAmount-TaxAmt-Freight)>=500) then '$500-$1000'
when ((SalesAmount-TaxAmt-Freight)>=100) then '$100-$500'
when ((SalesAmount-TaxAmt-Freight)<100) then '$0-$100'
end as SalesAmountCategory
From dbo.FactResellerSales
where OrderDate BETWEEN '2010-01-01 00:00:00.000' AND '2010-12-31 23:59:59.999'
) as t
group by SalesAmountCategory
order by SalesAmountCategory;
Below please find the image as a expected result example
I would use apply instead of sub-query, you can use row_number to create sortid :
select row_number() over (order by min(SalesAmount - TaxAmt - Freight)) as sortid,
SalesAmountCategory, count(*) as Orders
from dbo.FactResellerSales frs cross apply
( values (case when (SalesAmount - TaxAmt - Freight) >= 100000 then '>$100000'
when (SalesAmount - TaxAmt - Freight) >= 50000) then '$50000-$100000'
. . .
when (SalesAmount - TaxAmt - Freight) < 100 then '$0-$100'
end)
) frss(SalesAmountCategory)
where OrderDate BETWEEN '2010-01-01 00:00:00.000' AND '2010-12-31 23:59:59.999'
group by SalesAmountCategory;
Instead of ordering by the category, order by the minimum amount in each category:
select row_number() over (order by min(SalesAmount-TaxAmt-Freight)) as sortid,
. . .
order by min(SalesAmount-TaxAmt-Freight)
Of course, you will need to select these columns as well in the subquery (or do the calculation in the subquery).
I'm trying to find out the bounce rate of the top 10 UTM source. There is no column for bounces in the table so I have to query it. I've created a query to find the TOP 10 UTM source and another query to find bounce rate. I just can't seem to figure out how to combine both of this queries together.
The database table contains:
1) cuuid -cookie ID
2) session - session
3) duration
4) Each row represents a page view
SELECT
TOP 10 regexp_replace(regexp_substr(url, 'utm_source\\=[^\\&]*'), 'utm_source='),
COUNT(DISTINCT(cuuid)) as "Total Unique Visitors",
COUNT(DISTINCT(session)) as "Total Unique Sessions",
COUNT(*) as "Total Page Views",
CAST(COUNT(DISTINCT(session)) AS FLOAT)/CAST(COUNT(DISTINCT(cuuid)) AS FLOAT) AS "Average Sessions per Visitor",
CAST(COUNT(*) AS FLOAT)/CAST(COUNT(DISTINCT(session)) AS FLOAT) AS "Average Pageview per Session",
ROUND(SUM(CASE WHEN duration < 0 THEN 0 ELSE duration END)::FLOAT/COUNT(DISTINCT(session))) AS "Average Duration per Session"
FROM table1
WHERE url ILIKE '%%utm_source%%'
AND ts>='2018-05-01'
AND ts < '2018-06-01'
GROUP BY 1
ORDER BY 2 DESC;
--add bounce rate query into first--
SELECT
CAST((CAST((SUM(bounces)*100) AS FLOAT)/CAST(COUNT(*) AS FLOAT)) AS VARCHAR(5)) + '%' as "Bounce rate"
FROM (
SELECT
MIN(ts) AS "time_first_viewed",
cuuid,
session,
COUNT(*) as "number_of_events",
CASE WHEN count(*) = 1 THEN 1 ELSE 0 END AS bounces
FROM table1
WHERE ts>='2018-05-01'
AND ts < '2018-06-01'
GROUP BY cuuid, session)
For the final result, I need it to be in the same table. And the columns are:
1)UTM Source
2)Unique Visitor
3)Unique Sessions
4)Page View
5)Session/Visitor
6)Pageview/session
7)Avg Duration
8)Bounce Rate
you just do like the below with comma :
hope this works
Select * from
(SELECT
TOP 10 regexp_replace(regexp_substr(url, 'utm_source\\=[^\\&]*'), 'utm_source='),
COUNT(DISTINCT(cuuid)) as "Total Unique Visitors",
COUNT(DISTINCT(session)) as "Total Unique Sessions",
COUNT(*) as "Total Page Views",
CAST(COUNT(DISTINCT(session)) AS FLOAT)/CAST(COUNT(DISTINCT(cuuid)) AS FLOAT) AS "Average Sessions per Visitor",
CAST(COUNT(*) AS FLOAT)/CAST(COUNT(DISTINCT(session)) AS FLOAT) AS "Average Pageview per Session",
ROUND(SUM(CASE WHEN duration < 0 THEN 0 ELSE duration END)::FLOAT/COUNT(DISTINCT(session))) AS "Average Duration per Session"
FROM table1
WHERE url ILIKE '%%utm_source%%'
AND ts>='2018-05-01'
AND ts < '2018-06-01'
GROUP BY 1
ORDER BY 2 DESC)table1
,
(SELECT
CAST((CAST((SUM(bounces)*100) AS FLOAT)/CAST(COUNT(*) AS FLOAT)) AS VARCHAR(5)) + '%' as "Bounce rate"
FROM (
SELECT
MIN(ts) AS "time_first_viewed",
cuuid,
session,
COUNT(*) as "number_of_events",
CASE WHEN count(*) = 1 THEN 1 ELSE 0 END AS bounces
FROM table1
WHERE ts>='2018-05-01'
AND ts < '2018-06-01'
GROUP BY cuuid, session))table2
you defines alias names to your columns and its done.
In the below query, I get the count of customers who were active between "2017-09-01 00:00:00" and "2017-11-31 23:59:59" as cust_90 and would like to add another column to find the count of customers who were active between "2017-11-01 00:00:00" and "2017-11-31 23:59:59" (a subset of the whole period).
select custid, count(distinct concat(visit1, visit2)) as cust_90
from test1
where date_time between "2017-09-01 00:00:00" and "2017-11-31 23:59:59"
and custid = '234214124'
group by custid;
Sample output:
CustomerName cust_90 cust_30
David 38 15
Wondering whether I could have a subquery in the above query to find the customers active in a month. Any suggestions would be great.
This is called conditional aggregation which can be done using a case expression.
select custid,
count(distinct concat(visit1, visit2) end) as cust_90,
count(distinct case when to_date(date_time)>='2017-11-01' then concat(visit1, visit2) end) as cust_30
from test1
where date_time >= '2017-09-01' and date_time < '2017-12-01'
and custid = '234214124'
group by custid;