SQL filter using 2 dates + SAS [closed] - sql

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 8 years ago.
Improve this question
I have a dataset that looks like below
ctry | start | end
I have a second dataset b
that has
ctry | start | end
as well as other columns.
I would like to filter the second dataset based on ctry, start and end dates

Am presuming you are looking for some kind of inner join (keep all records in the second dataset, b, if the three columns match those in the first dataset). Try the following approach (one of many):
proc sql;
create table filtered as
select b.*
from first_ds as a /* you never said what your first dataset was called */
inner join b as b
on a.ctry=b.ctry
/* edits following OP comment */
and a.start < b.end
and a.end > b.start;

Related

SQL Query for the player and vsplayer [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 1 year ago.
Improve this question
Team Table
Player (column)
A
B
C
Expected Output: with Two columns
Player vsPlayer
A B
B C
C A
How to write a sql query to get the exact output as Expected output mentioned above.
Thanks in advance
The normal way to get combination (pairs) is to take all permutations, but where column 1 is less than column 2.
SELECT
l.player,
r.player
FROM
player l
INNER JOIN
player r
ON l.player < r.player
Demo : https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=9ce7af3d0afe89c6434cc2800dfbd2ef

Get intermediate time periods in SQL Server [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 4 years ago.
Improve this question
I want to generate a table in SQL of intermediate joined states. E.g. I have the following table
status_1 status_2 start_date_V1 end_date_v1 start_date_2 end_date_v2
--------------------------------------------------------------------------------
A B 01Jan2018 31Jul2018 31Dec2017 31Jan2018
A C 01Jan2018 31Jul2018 01Feb2018 30Dec2018
In this table there are start and end dates of the different states "status_1" and "status_2". I wan to have the information about the changes of the two joined states. The desired table would be:
status_1 status_2 start_date end_date
-----------------------------------------------
A B 01Jan2018 31Jan2018
A C 01Feb2018 31Jul2018
The following image might help to understand the problem:
Can anyone help?
Seems like you need the intersecting time period(?), that'd be solved with a simple 'CASE-WHEN-ELSE'-statement for each date in the query result.
SELECT
[status1],
[status2],
[start_date] = CASE WHEN [start_date_V1] < [start_date_2] THEN [start_date_V1] ELSE [start_date_2] END,
[end_date] = CASE WHEN [end_date_v1] < [end_date_v2] THEN [end_date_v1] ELSE [end_date_v2] END
FROM Table
If you've got many date columns (known amount), it'd be cleaner to type it as below. However, beware that sub queries like this can slow down your queries tremendously, if you don't know what you're doing.
SELECT
Status1,
Status2,
-- New Name Name of custom group of values Column1 Column2 Name of custom group of values
-- | | | | |
[start_date] = (SELECT MAX(StartDate) FROM (VALUES (start_date_1), (start_date_2)) AS value(StartDate)),
[end_date] = (SELECT MIN(EndDate) FROM (VALUES (end_date_1), (end_date_2)) AS value(EndDate))
FROM Table

MS Access SQL - Average of 3 Previous Rows [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 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

SQL: Generating a field based on a field [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 8 years ago.
Improve this question
I have a table (for the sake of the exercise let's call it Persons)
I am trying to create a query that will generate the CrDRN number.
The CrDRN column should contain the DRN number of the last record that has a VoucherType = 80
In this instance the CrDRN column would have the values 1 1 1 5 5
Any help would be greatly appreciated.
The CrDRN column should contain the DRN number of the last record that
has a VoucherType = 80
Another way of saying that would be to get the max value of DRN where DRN is less than or equal to the current DRN and where VoucherType is 80.
You can formulate just that in a subquery that returns CrDRN.
select T1.DRN,
T1.VoucherType,
(
select max(T2.DRN)
from T as T2
where T2.DRN <= T1.DRN and
T2.VoucherType = 80
) as CrDRN
from T as T1

update filed in a table recursively [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 8 years ago.
Improve this question
I have the following table test
id name formula
--------------------------------
1 A aa+bb+cc
2 aa e+f+g
3 e b
4 f t
5 g 5
How can I update the filed formula to get something like that
id name formula
--------------------------------
1 A b+t+5+bb+cc// update aa=b+t+5
2 aa b+t+5//at first update formula which has id =2
3 e b
4 f t
5 g 5
Your question is too complex. You need to solve several problems before you can actually update anything. You need to be able to:
decompose a formula so that you can get a list of all the operands (do you have to do that in SQL, though?);
distinguish between a reference operand and a constant value (or function?) operand;
determine which reference operands are valid references (exist in the table);
determine the order in which the references must be evaluated.
I suggest you seek help on these problems separately, because Stack Overflow is not a good fit for overly complex questions. (Do try solving them yourself first, though.)