Nesting Queries in Access SQL - sql

I have recently started using SQL and am stuck when applying it to Access. My previous experience (limited) has been with PostgreSQL, and I understand that SQL in Microsoft Access requires you to nest queries into sub-queries, which I am not familiar with.
I believe the code in SQL (not for access would look something like this...)
select weeks, sum(sweets_eaten), count(distinct (sweet))
from table
group by weeks;
This would then give me a table where I would have the unique weeks, the sum of sweets eaten per week and the number of sweets per week.
Ideally, what the query would then do is also tell me the average sweets eaten per week by dividing the total sweets eaten per week by the number of sweets.
Does anyone know how to write a query so this will work in Microsoft Access?
Thanks!
Edited code, this is what I am entering
select f15, sum(f16), count(*)
from (select f15, sum(f16) as sum_sweets_eaten
from table1
group by f15, f16
) as t
group by f15;
For the average, would it be possible to do this in addition to the sum.

The query you have written will not work in MS Access, because it does not support count(distinct).
You can pre-aggregate to get the result you want:
select weeks, sum(sum_sweets_eaten), count(*)
from (select weeks, sum(sweets_eaten) as sum_sweets_eaten
from table
group by weeks, sweet
) as t
group by weeks;
To get the average, use avg() rather than sum().

Related

What is the underlying purpose of a group by statement in SQL? [duplicate]

This question already has answers here:
Is SQL GROUP BY a design flaw? [closed]
(9 answers)
Is it really necessary to have GROUP BY in the SQL standard
(3 answers)
Closed 12 months ago.
Lately I have been dealing with extremely wide queries that perform a lot of transforms on data, and I am annoyed by having to maintain wide group by statements. This has me wondering,
why do they exist?
For example
select
company,
sum(owing) as owing
from
receivables
group by
company
Given this statement, it seems to me that the group by is implied.
There is an aggregate function
There only field not part of an aggregation is company.
Therefore, I would expect that a query engine could determine that company should be the thing grouped on.
select
company,
sum(owing) as owing
from
receivables
My general assumption is always that something like this exists for a reason, I just don't understand the reason, but ... I don't understand the reason.
What is the scenario that makes the existence of group by necessary?
Update
Based on comments, a point regarding mult-table queries making it less obvious to the engine. Also, a point regarding multi-nonaggregate fields.
select
c.name as company,
t.curr as currency,
sum(t.amt) as owing
from
company c
inner join transactions t on c.id = t.comp_id
having
sum(t.amt) < 0
This (more realistic) version of the original query uses two tables. It is still unclear to me, why the engine would not know to group on company and currency as they are the non-aggregated fields
An example from Oracle which supports nested aggregate functions
Assume that you have a cube rolling results.
The following query shows us the throws distribution.
select result
,count(*) as count
from cube_roll
group by result
RESULT
COUNT
1
11
2
23
3
12
4
23
5
15
6
16
The following query shows us the maximum count for the results.
Please note that result does not appear in the SELECT clause.
select max(count(*)) as max_count
from cube_roll
group by result
MAX_COUNT
23
Please note that result cannot be added to the SELECT clause.
select result -- invalid reference
,max(count(*)) as max_count
from cube_roll
group by result
ORA-00937: not a single-group group function
Fiddle

How to write an SQL query to get max number of counts for the most number of travelling of a user within a month

I have been given a task by my manager to write a SQL query to select the max number of counts (no of records) for a user who has travelled the most within a month provided that if the user travels multiple places on the same date, then it should be counted as one. For instance, if you look at the following table design; according to this scenario, my query must return me a count of 2. Although traveller_id "1" has traveled three times within a month, but he traveled to Thailand and USA on the same date, that is why its count is reduced to 2.
I have also developed my logic for this query but I am unable to write it due to lack of syntax knowledge. I split up this query into 3 parts:
Select All records from the table within a month using the MONTH function of SQL
Select All distinct DateTime records from the above result so that the same DateTime gets eliminated.
Select max number of counts for the traveller who visited most places.
Please help me in completing my query. You can also use a different approach from mine.
You can use the count aggregation in a cte then select top(1):
with u as
(select traveller_id,
count(distinct visit_date) as n
from travellers_log
where visit_date between '2022-03-01' and '2022-03-31'
group by traveller_id)
select top(1) traveller_id, name, n from u inner join table_travellers
on u.traveller_id = table_travellers.id
order by n desc;

Is there a way to handle immutability that's robust and scalable?

Since bigquery is append-only, I was thinking about stamping each record I upload to it with an 'effective date' similar to how peoplesoft works, if anybody is familiar with that pattern.
Then, I could issue a select statement and join on the max effective date
select UTC_USEC_TO_MONTH(timestamp) as month, sum(amt)/100 as sales
from foo.orders as all
join (select id, max(effdt) as max_effdt from foo.orders group by id) as latest
on all.effdt = latest.max_effdt and all.id = latest.id
group by month
order by month;
Unfortunately, I believe this won't scale because of the big query 'small joins' restriction, so I wanted to see if anyone else had thought around this use case.
Yes, adding a timestamp for each record (or in some cases, a flag that captures the state of a particular record) is the right approach. The small side of a BigQuery "Small Join" can actually return at least 8MB (this value is compressed on our end, so is usually 2 to 10 times larger), so for "lookup" table type subqueries, this can actually provide a lot of records.
In your case, it's not clear to me what the exact query you are trying to run is.. it looks like you are trying to return the most recent sales times of every individual item - and then JOIN this information with the SUM of sales amt per month of each item? Can you provide more info about the query?
It might be possible to do this all in one query. For example, in our wikipedia dataset, an example might look something like...
SELECT contributor_username, UTC_USEC_TO_MONTH(timestamp * 1000000) as month,
SUM(num_characters) as total_characters_used FROM
[publicdata:samples.wikipedia] WHERE (contributor_username != '' or
contributor_username IS NOT NULL) AND timestamp > 1133395200
AND timestamp < 1157068800 GROUP BY contributor_username, month
ORDER BY contributor_username DESC, month DESC;
...to provide wikipedia contributions per user per month (like sales per month per item). This result is actually really large, so you would have to limit by date range.
UPDATE (based on comments below) a similar query that finds "num_characters" for the latest wikipedia revisions by contributors after a particular time...
SELECT current.contributor_username, current.num_characters
FROM
(SELECT contributor_username, num_characters, timestamp as time FROM [publicdata:samples.wikipedia] WHERE contributor_username != '' AND contributor_username IS NOT NULL)
AS current
JOIN
(SELECT contributor_username, MAX(timestamp) as time FROM [publicdata:samples.wikipedia] WHERE contributor_username != '' AND contributor_username IS NOT NULL AND timestamp > 1265073722 GROUP BY contributor_username) AS latest
ON
current.contributor_username = latest.contributor_username
AND
current.time = latest.time;
If your query requires you to use first build a large aggregate (for example, you need to run essentially an accurate COUNT DISTINCT) another option is to break this query up into two queries. The first query could provide the max effective date by month along with a count and save this result as a new table. Then, could run a sum query on the resulting table.
You could also store monthly sales records in separate tables, and only query the particular table for the months you are interested in, simplifying your monthly sales summaries (this could also be a more economical use of BigQuery). When you need to find aggregates across all tables, you could run your queries with multiple tables listed after the FROM clause.

SQL query question

I'm trying to do something in a query that I've never done before. it probably requires variables, but i've never done that, and I'm not sure that it does.
What I want is to get a list of sales, grouped first by affiliate, then by it's month.
I can do that, but here's the twist... I don't want the month, but month 1, month 2, month 3...
And those aren't Jan, feb, march, but the number of months since the day of first sale.
Is this possible in a query at all, or do I need to do this in my code.
Oh, mysql 5.1.something...
Sure, just write an expression in SQL that generates the number of months since the first sale (Do you mean the first sale for that afiliate? If so, you'll need a subquery)
And since you say you want a list of sales, I assume you don't really want to "Group By" affilaite and monthcount, you just want to Sort, or Order By those values)
If you wanted the Average sales amount, or the Count of sales, or some other Aggregate function of sales data, then you would be doing a "Group By"...
And I don't think you need to worry about sorting by the number of months, you can simply sort by the difference between each sales date and the rearliest sale date for each affiliate. (If you wanted to apply a third sorting rule, after the sales date sort, then you would need to be more careful.)
Select * From Sales S
Order By Affiliate,
SalesDate - (Select Min(SalesDate)
From Sales
Where Affiliate = S.Affiliate)
Or, if you really need it to be by the difference in months
Select * From Sales S
Order By Affiliate,
Month(SalesDate) -
(Select Month(Min(SalesDate))
From Sales
Where Affiliate = S.Affiliate)
This is possible in standard SQL if you use what I like to call "SQL gymnastics". It can be done with subqueries.
But it looks incredibly ugly, is hard to maintain and it's really not worth it. You're far better off using one of the many programming languages that wrap SQL (such as PL/SQL) or even a general purpose language that can call SQL (such as Python).
The result will be in two languages but will be all the more understandable than the same thing written in just SQL.

Sql Server : Column wise total SQl Query

Is it possible to get column wise total using query?
in my grid there are 20 columns. i have to display each columns total value in its footer. now im using TemplateField field and javascript function to get the total value.if it is possible to get it from sql query i can reduce the code
Try something like:
SELECT *, SUM(SalesAmount) OVER() as TotalSales
FROM YourTable
But if you only need the sum and nothing else, just do:
SELECT SUM(SalesAmount) as TotalSales
FROM YourTable
And in future, please try to give more information in your question.
Rob
To sum columns, it's best to use whatever client you're dealing with (Reporting Services, Datagrid, whatever), and just tell that to display a totals row.
If you were to do it within the same query, then you'd end up with rows that meant something different, and displaying it becomes quite awkward.
You CAN do it in the query, but you probably shouldn't.
Rob
I think you are looking for SUM function
Eg:
SELECT SUM(salary) as "Total Salary"
FROM employees
select MAX([p-1]) p1,MAX([p-2]) p2 from #temp