Categorizing date by week with dynamic alias name - sql

We have one column in our table which stores a number of records in "date" type, we want to separate them by each week and find the number of repetitions for each week that we have in our column.
the database is in oracle.
I think for creating a dynamic alias name we need cursor.
this image below may help:

you can use mysql function week()
here the info: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
an example
select concat('Week ',week(date)), count(date)
from table
group by week(date)

Try this syntax;
SELECT to_char(date,'WW') week, COUNT(date) number
FROM your_table
GROUP BY to_char(date,'WW')
Have a look at TO_CHAR function.

Related

SQLite - TZ format to Date time only using SQL queries

I have a SQLite database with a simple table in the format of:
ID,DateText,Name
1,2020-09-01T18:57:17Z,John
2,2022-12-01T04:00:09Z,Laurel
...
The DateText column is declared as TEXT on the "create table" statement. Using only SQL, I need to:
Create a new column with the DateText data.
Obtain the "oldest" date
Obtain the "newest" date
Note that I need to resolve this with a SQL query. I cannot read into a programming language, parse, and update table--I need to do everything on SQL. For example, SQL Server DateTime with timezone is the opposite, but they are using node.js, something I cannot do.
You can get the oldest and newest using min() and max():
SELECT ID, min(DateTime), Name FROM YourTable; -- Oldest
SELECT ID, max(DateTime), Name FROM YourTable; -- Newest
The nice thing about ISO-8601 date and time format strings is that they sort lexicographically without having to do anything special with them.
These queries would give an error on most SQL database engines because of the mix of non-grouped columns with an aggregate function, but SQLite explicitly will return the row that goes with the minimum or maximum column value. And of course if you don't want the other columns, just leave them out of the SELECT.

How to select from variable table name?

I have a scheduled query set up, but I want to select FROM my_project.my_database.my_table_{todays_date} each day.
I found how to create variables in BigQuery like:
DECLARE todays_date STRING DEFAULT REPLACE(CAST(CURRENT_DATE AS STRING), '-', '').
(Date Format: YYYYMMDD (no underscore or hyphen))
But how could I query from this table each day?
`my_project.my_database.my_table_{todays_date}`
You can achieve this using queries with wildcards in the table name. The documentation here explains it very well:
https://cloud.google.com/bigquery/docs/querying-wildcard-tables
Additionally in your case if you wanted to filter against a subset of the tables you could do something like this where the _TABLE_SUFFIX psuedo column is used to filter to select tables based on the #run_date variable if being done through a scheduled query:
SELECT *
FROM my_project.my_dataset.my_table_*
WHERE _TABLE_SUFFIX = CAST(#run_date AS STRING)

SQL timestamp Query

Table has Created_time column with values like 25-MAY-2012.10.12.320000 PM.
I need to write a query to display the all the fields from table XYZ which were created between the month of March and April, agnostic to the year they are in.
I would like to display the records in ascending order based on their id.
Can someone help me?
Check out month and year function.
if created_time is of type datetime:
select *
from TABLE
where month(Created_time) in (3, 4)
If you need to check whether the Created_time was in March of any year, then the idea is to do something like this:
select *
from XYZ
where Created_time like '%-MARCH-%'
order by id;
Or, if this is not supported for your column's type, then convert it to varchar. Also, there are some RDBMS-specific functions that could help you, but for a more thorough answer I would need to know the RDBMS you are using and the type of Created_time.
EDIT
As pointed out by Jarhl, EXTRACT should be usable in this case to check the month. Source, proving that he is right says:
Name
EXTRACT
The ANSI SQL scalar function for extracting parts from a date is
EXTRACT. ANSI SQL Standard Syntax
The ANSI SQL EXTRACT function takes a date_part and an expression that
evaluates to a datetime value. MySQL, Oracle, and PostgreSQL support
the ANSI SQL standard syntax:
EXTRACT( date_part FROM expression )

How to compare dates or filter data based on Date in sybase?

I want to fetch data for a particular date, datatype for this column is DateTime.
Tried below query:-
SELECT * from table_name where transaction_date=convert(DATETIME,'02/21/2017',101)
But above query is not working and returning no result, please could someone point out what I am doing wrong.
If you need compare dates with day precision you can use datediff function as below.
SELECT *
from table_name
where datediff(dd,transaction_date,convert(DATETIME,'02/21/2017',101))=0
More information about DateDiff

how to find fifference between 2 dates in DB2

In my database i have a TIMESTAMP field.Now I want to find the difference between the value stored in this field and the current TIMESTAMP .I want the result in days.
How can i get it?
Note:I am using DB2 database
days(TIMESTAMP)_-days(your_col)
You don't say what version of DB2 you're using.
In older versions, you have to SELECT against a table, even though you're not retrieving any columns.
SELECT days(TIMESTAMP)-days(your_col)
FROM SYSIBM.SYSTABLES
Substitute a creator and table that you have SELECT authority for.
I shall reiterate what Gilbert answered. He was correct, but I don't think the original author understood the idea. As an example:
create table test ( mycolumn timestamp )
insert into test values (current timestamp)
select * from test
select days(current timestamp)-days(mycolumn) from test
The current timestamp is a special register that DB2 has available. Days is a default function that will return the number of days component of the given timestamp counting from January 1, year 1.
The CURRENT_DATE returns the current timestamp then this can be used
DAYS(CURRENT_DATE)-DAYS(TIMESTAMP)