MS Access SQL - Average of 3 Previous Rows [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a column of values where I need to take the average of the two previous values and the current value and display as a new column. I'm using MS Access 2013 and am more familier using SQL code rather than the SQL Query Wizard. So, if you could provide the code I'd appreciate it.
I've read on other threads that talked about a Lag function, but I believe Access does not allow that. Also, I've seen similar questions answered with subqueries, but I'm not familiar enough with those yet.
Below is what I'm looking for. Given column A (1,2,3,4,5) how do I make B (0,0,2,3,4)?
A | B
----------
1 | 0
2 | 0
3 | 2 = (1+2+3)/3
4 | 3 = (2+3+4)/3
5 | 4 = (3+4+5)/3

Utilize a self join to the same table for when the value of A is BETWEEN a - 2 and A.
SELECT
t.A
,SUM(pre.A) / 3 as B
FROM
TableName t
LEFT JOIN TableName pre
ON pre.A >= (t.A - 2)
AND pre.A <= t.A
GROUP BY
t.A
Note that this will actually give you 0,1,2,3,4. If you want 0,0,2,3,4 you will have to count the preceding rows to determine if there are three and if not make it 0 such as:
SELECT
t.A
,IIF(COUNT(pre.A) < 3, 0, SUM(pre.A) / 3) as B
FROM
TableName t
LEFT JOIN TableName pre
ON pre.A >= (t.A - 2)
AND pre.A <= t.A
GROUP BY
t.A

Related

Nested SELECT vs JOIN performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I have the following two tables in PostgreSQL database (simplified for the sake of example):
article
id
summary
1
Article 1
2
Article 2
3
Article 3
...
...
event
id
article_id
eventtype_id
comment
108
1
4
Comment 1
109
2
8
Comment 2
110
3
4
Comment 3
...
...
I would like to select only 1 event with eventtype_id=4 for each article. The result should look like this:
article_id
article_summary
event_comment
1
Article 1
Comment 1
2
Article 2
3
Article 3
Comment 3
...
Which of these 2 queries (Query 1 or Query 2) runs faster? Do they return the same result?
Query1:
SELECT
a.id AS article_id,
a.summary AS article_summary,
evnt.comment AS event_comment
FROM
article a
LEFT JOIN
event evnt ON evnt.article_id = a.id AND evnt.eventtype_id = 4;
Query2:
SELECT
a.id AS article_id,
a.summary AS article_summary,
(
SELECT
evnt.comment
FROM
event evnt
WHERE
evnt.article_id = a.id AND
evnt.eventtype_id = 4
LIMIT 1
) AS event_comment
FROM
article a;
Apart from the fact that queries are not the same, as you said in the comments, generally speaking, correlated queries are not considered suitable from a performance point of view. That's because these queries are applied row by row. They are usually helpful in some particular situations: read this. However, even in those situations, it is a good practice to use them in an exists clause if possible, So that whenever it finds a row for the query it returns true.

Related and non-related SQL queries putted together [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two questions, one is how I can put these two queries together (add just the right column). Example:
Query 1
Phase menCount
A 40
B 50
C 60
D 20
Query 2
Phase womenCount
A 60
B 50
C 40
Wanted query result
Phase menCount womenCount
A 40 60
B 50 50
C 60 40
D 20 NULL
The second question is, how I can put together columns from non-related queries. Is this possible at all? I have at least 10 different srcripts and if I can put them into one call, it would be easier. Example:
Query 1
Phase phaseCount
A 20
B 40
C 60
Query 2
Stage stageCount
W 90
X 120
Y 150
Z 190
Wanted query result
Phase phaseCount Stage stageCount
A 20 W 90
B 40 X 120
C 60 Y 150
Z 190
Thanks for answers.
To put two queries together you can treate them as "table expressions". For example
select
coalesce(a.phase, b.phase) as phase,
a.mencount,
b.womencount
from (
-- query #1 here
) a
full join (
-- query #2 here
) b on a.phase = b.phase
To put unrelated queries together you need to produce a joining condition somewhere. For example, you can modify each query to produce an artificial row number using the ROW_NUMBER() function; then you can use this value to join against each other, as in:
select
a.*, b.*
from (
-- modified query #1 now
select
...,
row_number() over(order by ...) as rn
from ...
) a
full join (
-- modified query #2 now
select
...,
row_number() over(order by ...) as rn
from ...
) b on a.rn = b.rn
order by coalesce(a.rn, b.rn)

Count by ID if value is lower than and set the value 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a problem.
My table is tableXYZ
I have a row a where i set the year.
I have a row b where i need to set value 1 if the year from row a is lower than 2019 and 0 if is > 2019.
After all i need to make a count from row b only the 1 values as total.
How can i do that, because i tried a lot of examples but doesn't work.
This might help:
SELECT COUNT(1) AS [b_count]
FROM (
-- Use case to evaluate column a and set value for column b
SELECT a,
CASE WHEN a < 2019 then '1' else '0' end as [b]
FROM tableXYZ
) AS X -- Alias of subquery
WHERE X.b = '1' -- Select only rows where b = '1'

SQL QUERY SQL SSRS [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hi can anyone help me with sum up first two rows in table and then rest would be same. example is
ID SUM
12 60
0 20
1 30
2 50
3 60
I am expecting
ID SUM
0 80
1 30
2 50
3 60
I am doing this from memory - so if this doesnt work let me know and we can do it another way looking at the row number;
Assuming you have a unique ID to sort it by as you suggested, you could do something like this;
you may want to change the order to be desc if that's how you classify your 'top 2'
SELECT TOP 2 ID,
SUM(VALUE)
FROM [Table]
GROUP BY ID
ORDER BY ID
UNION
SELECT ID,
VALUE
FROM [Table]
WHERE ID NOT IN (SELECT TOP 2 ID
FROM [Table] ORDER BY ID)

Select simplest element SQL Server recursion changed complexity of table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have T1 table with Id and name columns.
Table T2 with Aggregate_ID and Element_ID.
They are crossed
x1:
ID и Name
1 Car
2 Hood
3 Engine
4 Cylinder
5 Wheel
6 tyre
7 rim (car)
8 Rim fixation (Car)
x2:
Aggregate_ID Element_ID
1 2
1 3
1 4
1 5
3 4
5 6
5 7
7 8
I need to select simplest element like 2, 4, 8
Complexity and number of elements can be varied.
How can I do it with recursion?
There is another task:
I need to output all the simplest elements of which consists Wheel.
Recursive solution in SQL can be very complex. In your case I see no need to use it, since it will only make your code more complex.
You can use CTE if you still insist:
Recursive query in SQL Server
Non- recursive solution:,
You want only the elements that appear in T2 in the Element_ID but not in the Aggregate_ID:
SELECT Element_ID
FROM T2
EXCEPT
SELECT Aggregate_ID
FROM T2
Or if you want to display all of the information for the elements:
SELECT *
FROM T1
WHERE T1.ID NOT IN (SELECT Aggregate_ID
FROM T2)