How to write a date range condition - date

How would I write the date range condition correctly for the following query: list all instruments from table "asset", where the "maturity_dt" > 1 year:...
in English it sounds like :" AND asset.maturity_dt >= Today + 365.."

If you are using MySQL you will be needing a query like,
Select * from asset where DATEDIFF(maturity_dt, now())>365;

Related

Why a simple query works in BIgquery but not PostgreSQL?

I wrote the query below to calculate the death percentage in Postgresql and the result is zero,but same query in Bigquery gets the correct result. Can anyone have idea? Thanks!
SELECT
location,
MAX(total_cases) AS Cases_total,
MAX(total_deaths) AS Death_total, (MAX(total_deaths)/MAX(total_cases))*100 AS DeathPercentange
FROM covid_deaths
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY DeathPercentange DESC;
I am not allowed to insert my screenshot so I have the link:
Same query in Bigquery
Query in PostgreSQL
The database looks like this:
The preview of the database
The result is ok, your operation is between big integers. The result is just an integer.
112/9766 = 0 * 100 = 0
If you want a numeric as result you have to cast your columns as numeric
SELECT
location,
MAX(total_cases) AS Cases_total,
MAX(total_deaths) AS Death_total, (MAX(total_deaths)::numeric/MAX(total_cases)::numeric)*100 AS DeathPercentange
FROM covid_deaths
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY DeathPercentange DESC;
You do integer division. The result will always be 0 whenever total_deaths < total_cases (which is most likely your case). What you should do is cast to float or decimal at least one operand, e.g.
(MAX(total_deaths)::decimal / MAX(total_cases))*100 AS DeathPercentange

PostgreSQL transforming the entire column value in a table

I have been trying to change all the values in a column. When I do it turns out to take my table and drop all the rows that match my criteria in the where clause.
Example
|_Month_|_Cost_|_Title_|
|___2___|_15.99|_hello_|
|___2___|_32.87|_John__|
|___2___|_32.87|_Neat__|
|___3___|_32.87|_Kelps_|
|___4___|_32.87|_Gulp__|
|___5___|_32.87|_Tried_|
I run this Query
UPDATE tableName SET Month = 'FEB' WHERE Month = 2;
The Change:
|_Month_|_Cost_|_Title_|
|__FEB__|_32.87|_Neat__|
|___3___|_32.87|_Kelps_|
|___4___|_32.87|_Gulp__|
|___5___|_32.87|_Tried_|
What I need:
|_Month_|_Cost_|_Title_|
|__FEB__|_15.99|_hello_|
|__FEB__|_32.87|_John__|
|__FEB__|_32.87|_Neat__|
|___3___|_32.87|_Kelps_|
|___4___|_32.87|_Gulp__|
|___5___|_32.87|_Tried_|
Where am I going wrong?
I have also tried to get the date changed from a timestamp with
SELECT TO_DATE(payment_timestamp, 'Mon") payment_month
from tablename;
but PostgreSQL doesn't recognize the TO_DATE()?? Can someone tell me why?

Querying date/time on 2 columns

I have a column date and column time on my PostgreSQL table. I wish to make a query, to filter rows that are not expired based on date and time. I tried this, but it does not works and returns an error Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or nea:
from q in Line, where: fragment("date ? + time ? > NOW()", q.date, q.time)
I think this problem can be solved by not using time and date prefixes:
from q in Line, where: fragment("? + ? > NOW()", q.date, q.time)
or even
from q in Line, where: q.date + q.time < fragment("NOW()")
Provided, your columns have the correct data type
not sure if you need to run a standard query or if you are filtering via some GUI, but time and date types can be combined together via simple addition. https://www.postgresql.org/docs/current/functions-datetime.html
The following code:
WITH q AS (
SELECT* FROM (VALUES
('11:29:10'::time,'03-18-2019'::date),
('11:29:10'::time,'03-18-2021'::date)
) t ("time","date")
)
SELECT * FROM q WHERE q.time+q.date > NOW()
Should only print the date in the future, which is what you are trying to achieve.
Hope this helps!

How to get hits.second in BigQuery?

I'm newb on BigQuery. Is-it possible to get hit.second in a way in BigQuery?
My idea would be after to concat hits_time, hits_hour, hits_minute with date to display in a column the date with time (YYYY-MM-DD HH:MM:SS).
Thank for your help
try below
#standardSQL
SELECT
visitId,
hit.hitNumber,
TIMESTAMP_SECONDS(visitStartTime) AS visitStart,
TIMESTAMP_MILLIS(1000 * visitStartTime + hit.time) AS hitStart
FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910`, UNNEST(hits) AS hit
ORDER BY 1, 2
LIMIT 100
From BigQuery Export Schema:
hits.time INTEGER The number of milliseconds after the visitStartTime when this
hit was registered. The first hit has a hits.time of 0

idh_hist query is very slow while searching with date

I am trying to write a query to search thru MFG/PRO invoice table 'idh_hist' for specific date range. It is running very slow when added the date condition. But when I put off the date condition, it is very fast. Can you please suggest ways to write a query on idh_hist that runs reasonably faster with conditions.
Following is my query:
for each idh_hist no-lock where idh_domain = "d0002"
and idh_due_date = TODAY:
/* display code here... */
end.
Thanks in advance!
Database Index:
Flags Index Name Cnt Field Name
----- --------------------- ---- ---------------------
idh_fsm_type 4 + idh_domain
+ idh_fsm_type
+ idh_nbr
+ idh_line
pu idh_invln 4 + idh_domain
+ idh_inv_nbr
+ idh_nbr
+ idh_line
idh_part 4 + idh_domain
+ idh_part
+ idh_inv_nbr
+ idh_line
u oid_idh_hist 1 + oid_idh_hist
You do not appear to have an index that uses idh_due_date. You will need to add such an index.
The 4gl uses rules to select indexes based on the WHERE clause. The most important rule is that leading components of the index which have equality matches will be used.
The query you have shown only has one such match on idh_domain. So then tie breaker rules are applied. This will result in the idh_invln index being chosen.
As it is, to satisfy your query all records that match the "idh_domain" field need to be searched. (If you only have one domain that means that you are doing a table scan.)
You probably want to add an index on idh_domain and idh_due_date. That would be a perfect match for your query.