SQL filter by two date columns in the same month - sql

In my dataset I have column A and column B, they have different dates, sometimes one column has a date and the other don't, sometimes both have values but different months. I want to filter by the date when they are in the January.
I want both columns to have date of January '2023-01-01'
I started with
SELECT *
FROM table
WHERE DATE_TRUNC(SAFE_CAST(column_a AS DATE), MONTH) = '2023-01-01'
OR DATE_TRUNC(column_b, MONTH) = '2023-01-01'
The problem is that in some cases the column_a has a date prior January due to the or. And both dates need to be in the some month.
How can I solve that?

Related

Generate date array with all dates in between week range

I am trying to generate a date array for joining purposes, where I have all dates relative to a week range in the second column.
i.e.
SELECT
generate_date_array(date(2021,1,1),date(2021,11,26),INTERVAL 6 DAY)
Produces
What I need is a second column, that shows all the dates between each date row.
i.e.
Since I am ultimately joining this to another table, to see what week a record creation date is in, for this interval, I'm wanting to make this a table with two columns, and a row for each entry.
There are many way of doing this - below is just one and to re-use the "code" you already started with
select week, week + step as date
from (
select
generate_date_array(date(2021,1,1),date(2021,11,26), interval 6 DAY) weeks
), unnest (weeks) week, unnest([0, 1, 2, 3, 4, 5]) step
with output

Oracle concatenate two int columns, make them date and select a date from them

I have column REF_YEAR and column REF_MONTH. Both are int columns.
I want to select a range of dates between these two ranges.
for example
select *
from table
where date between '05/2020' and '12/2021'
Can i do something like that but with similar results for int columns?
If I understood you correctly, you'd
select *
from your_table
where to_date(ref_year || lpad(ref_month, 2, '0'), 'yyyymm')
between date '2020-05-01' and date '2021-12-01'
i.e. you'd have to "convert" (to_date) ref_year concatenated with ref_month (left-padded with zero up to 2 characters in length because May 2021 isn't 20215 but 202105), using appropriate format mask (yyyymm) and compare it to valid date values, e.g. date literals I used.
You should be using a single proper date column here, rather than separate columns for the month and year. That being said, integer month and year columns is still workable. Using your example:
SELECT *
FROM yourTable
WHERE REF_YEAR = 2020 AND REF_MONTH >= 5 OR REF_YEAR = 2021;
Note that the above logic would include the entire 2021 calendar year, including the entire month of December. This is what I imagine you want; if you instead want to exclude December, we would have to change the logic slightly. That being said, it would be much better to maintain a single date column REF_DATE:
SELECT *
FROM yourTable
WHERE REF_DATE >= date '2020-05-01' AND REF_DATE < date '2022-01-01''

Access query to pull previous month's data

I have a table in Access 2013 (Table1) that contains the following columns:
ID (pk), ReportDate, Amount
The most current data is 30-50 days old. For example, today (6/22/16) the most recent data would be the 5/1/16 row, as the 6/1/16 data won't be entered until mid-July. (All dates in the ReportDate column are the 1st of the month, i.e.: 4/1/16, 5/1/16, etc.)
I need to write a query that will do a 6-month lookback, but exclude the most current month's data.
So, for example, if I ran the query today (6/22/16), I would only get the rows that correspond to the following months:
12/1/2015
1/1/2016
2/1/2016
3/1/2016
4/1/2016
The data for 5/1/16 should be excluded, as it's the most recent month.
I can pull the previous 6 months worth of data with setting the criteria (in QBE) for ReportDate to>=DateAdd("m",-6,Date()), but I can't seem to figure out how to exclude the most recent month.
This should give you the start date of the most recent month in your table:
SELECT Max(ReportDate) AS MaxOfReportDate
FROM Table1;
If that is the month you want to exclude, use that query as a subquery which you cross join back to the table. Then you can use a WHERE clause with a BETWEEN condition whose end points are determined by DateAdd() expressions based on MaxOfReportDate:
SELECT t.ID, t.ReportDate, t.Amount
FROM
Table1 AS t,
(
SELECT Max(ReportDate) AS MaxOfReportDate
FROM Table1
) AS sub
WHERE
t.ReportDate BETWEEN DateAdd('m', -6, sub.MaxOfReportDate)
AND DateAdd('m', -1, sub.MaxOfReportDate);

SSRS SQL I need to display data for dates used in the parameter and the previous month

I have an SSRS report with parameters for Created On Start and Created On End. Users run this manually and choose the date range to display records for. I need to display in two different columnns the records for the month the user entered in the parameters and the previous month for the dates used in the parameters.
For example the user uses the the following dates in the parameters:
Start Date: 03/01/2016 EndDate: 03/31/2016
The Report should display in one column the records for march 2016 and next to it the records for february 2016
You could write one query which queries both months.
Add a field that will act as the column label eg format the date as the first of the month.
Then create a pivot table to show the two months as the columns with the usual rows .
EDIT - new details
So:
dateStart = '2016-03-01'
dateEnd = '2016-03-31'
These could be less than the whole month, but should be in the same month. prevStart = DATEADD(month, DATEDIFF(month, '2000-01-01', dateStart)-1, '2000-01-01')
the first day of the previous month.
Use similar for the prevEnd to calculate the last day of previous month.
OK. Now build your select:
SELECT xxxx, yyyy, zzzz
, DATEADD(month, DATEDIFF(month, '2000-01-01', createdOnDate), '2000-01-01') as MonthCol
FROM tables
WHERE (createdOnDate>= prevStart and createdOnDate<=prevEnd)
OR (createdOnDate>= dateStart and createdOnDate<=dateEnd)
Build a pivot table style grid with monthCol as the heading of the columns and your usual data as the rows. That way you can get your "previous Month" columns as well as the date range that you selected

SQLITE strftime() function issue

SELECT strftime('%W', 'Week'), sum(income) FROM tableOne GROUP BY Week;
Format for date is a simple date: YYYY-MM-DD
PROBLEM: When run no value for the Week column is provided. Any suggestions?
There is data in the table and when the query is run the income is summarized by the date in the week column. Thing is, this column contains a date that may be any day of the week and often multiple different days of the same week. I need to summarize the income by week.
In SQL, 'Week' is a string containing four characters.
To reference the value in a column named "Week", remove the quotes: Week.