I have a table tk. It has a column effective_date with a data type of date. The table also has some other columns. What I want to do is query the table such that output contains all dates having same year but month should differ
I have tried with below query in SQL Server, but it's not returning the desired result:
select * tk_id
from tk
group by tk_id, YEAR(effective_date);
Are you looking for a where clause?
select *
from t
where effective_date >= '2019-01-01' and effective_date < '2020-01-01'
Related
I have a google bigquery table with orders with a DATE column and other columns related to the orders. The starting date of the dataset is from 2021-01-01 (yyyy-mm-dd).
My aim is to filter on the DATE column from last year and this year to the previous iso week. For this, I used the ISOWEEK to create a new column:
WITH
last_week_last_year AS (
SELECT
DATE,
EXTRACT(ISOWEEK FROM DATE) AS isoweek,
FROM
`orders`
WHERE
EXTRACT(ISOWEEK FROM DATE) = EXTRACT(ISOWEEK FROM CURRENT_DATE())-1
GROUP BY 1, 2
ORDER BY DATE
)
SELECT * FROM last_week_last_year
This query results as the following table:
The issue is that when I filter on the original orders table by the DATE from the last_week_last_year table I get all the orders back instead of just the filtered version.
My method to filter is WHERE DATE IN (SELECT DATE FROM last_week_last_year) as seen below.
SELECT
*
FROM
`orders`
WHERE
DATE IN (SELECT DATE FROM last_week_last_year)
ORDER BY DATE DESC;
A snapshot of resulting table. It contains all of the records from 2021-01-01 until the latest day.
How can I make sure that on the latter query the table is filtered based on the first query's dates in DATE column?
I have a table in which one of the columns is m_date with type timestamp with timezone.
I want to write a query to filter the rows where the year is 2020.
One way is to use a range query:
select *
from the_table
where m_date >= date '2020-01-01'
and m_date < date '2021-01-01';
This can use an index in m_date.
Alternatively (but slower!)
select *
from the_table
where extract(year from m_date) = 2020
I am querying in my Table for Distinct Product Details. Now I have to filter Data based upon Date Column present in Table. The column having Dates have Data Type Varchar2. But I am not getting any result whereas Data is present in that Daterange. WEEK_DATE is my Date Column.
select distinct PRODUCT
from Table1
where WEEK_DATE between '12/31/2012' and '06/19/2017'
Some Sample Dates
2014-03-31
2014-09-01
2014-12-15
2014-12-22
You can try the following query :
select distinct PRODUCT from Table1
where cast(WEEK_DATE as date) between '12/31/2012' and '06/19/2017'
I would start by switching to standard date formats:
select distinct product
from Table1
where week_date >= '2012-12-31' and
week_date < '2017-06-20';
This will probably fix your problem. You query would return no rows if the comparisons were made as strings rather than dates.
I have a have a hive table which is partitioned with the table_date.I want to get the count of individual partition for the particular day for the particular month and particular year.
When I run the following query I am getting a count for an entire month but I want it as individual day.
select count(*) from table where month(table_date)=1 and year(table _date)=2016
If it is partitioned on date, I would expect this to work:
select table_date, count(*)
from table
where table_date >= '2016-01-01' and table_date < '2016-02-01'
group by table_date;
select table_date, count(*)
from table
where table_date between '2016-01-01' and '2016-02-01'
group by table_date;
I have a column in my database called "dob" of type datetime. How do I select all the rows after a specific DoB in SQL Server 2005?
Simply put:
SELECT *
FROM TABLE_NAME
WHERE
dob > '1/21/2012'
Where 1/21/2012 is the date and you want all data, including that date.
SELECT *
FROM TABLE_NAME
WHERE
dob BETWEEN '1/21/2012' AND '2/22/2012'
Use a between if you're selecting time between two dates
Let's say you want to get all records from a table called Table_One with a datetime column called date_value that have happened in the past six months...
CREATE TABLE (
date_value DATETIME
)
SELCECT *
FROM Table_One
WHERE date_value > DATEADD(month, -6, getdate());
This gives a bit more dynamic of a solution.