how to calculate date difference - date

my data
column1 column2
1-Sep-11 31-Aug-12
1-May-12 30-Apr-14
1-Mar-09 28-Feb-14
1-Apr-13 31-Mar-14
1-Apr-10 31-Mar-13
i want how many years difference between column1 and column2
out put like
column1 column2
1-Sep-11 31-Aug-12 1
1-May-12 30-Apr-14 2
1-Mar-09 28-Feb-14 5
1-Apr-13 31-Mar-14 1
1-Apr-10 31-Mar-13 3
please let me know

Please try:
=YEAR(B1)-YEAR(A1)

select to_date(Column1,'DD-MON-YYYY')- to_date(Column2,'DD-MON-YYYY') from Table;

Excel solution:
If you only want the difference between the years, i.e. coming from 31-Dec-13 to 1-Jan-14 should result in 1 year, use pnut's formula =YEAR(B1)-YEAR(A1).
If you're interested in the real underlying duration, i.e. 1-Apr-13 to 31-Mar-14 would be 0 years, use this formula: =INT((B1-A1)/365)!

Related

How get latest date from other 4 columns for each record?

I have this select:
SELECT
hre.workday_given_id AS employee_id
,pp.id AS job_id
,pp.id AS job_pk
,pp.time_modified AS job_modify_date
,pps.time_modified AS store_job_modify_date
,tss.time_modified AS store_job_assignment_modify_date
,'?' AS time_modify_date
,cas.time_modified AS store_modify_date
FROM ...
I need time_modify_date -- this is latest date from other 4 date columns.
Please help me to create the query. Thank you.enter image description here
You can use the greatest() function:
greatest(pp.time_modified, pps.time_modified, tss.time_modified, cas.time_modified) as time_modify_date

Extracting data from a table in kdb

Kdb question:
They're multiple rows in a table and I want to check if all the rows if the column meets a condition.
So the column StartDay = ***
How can I check each single row for that column?
Select from t where StartDay = '$"***"
Just gives me type errors.
Any help would be appreciated !
Assuming the column StartDay is of date type like in the following example
q)show t:([]StartDay:.z.d+til 3;number:til 3;sym:`abc`def`ghi)
StartDay number sym
---------------------
2021.02.19 0 abc
2021.02.20 1 def
2021.02.21 2 ghi
Then the following query will work
q)select from t where StartDay=2021.02.19
StartDay number sym
---------------------
2021.02.19 0 abc
The example you have given seems like you are trying to query a column of symbol type. Here are two examples of that
q)select from t where sym=`$"ghi"
StartDay number sym
---------------------
2021.02.21 2 ghi
q)select from t where sym=`ghi
StartDay number sym
---------------------
2021.02.21 2 ghi
Perhaps the following guide on where in q-sql will help.

Postgres query for report

I'm trying to solve this problem:
I have a query/view that will join ~10 tables to extract some fields for a report (if any). The query doesn't use any grouping function, only joins and cut off some unuseful data.
I have to take this one big view, get the group for the first index, take the max of a date in the second column and take all the information from other fields referring the record of the max value.
I cannot be able to to this in postgres.
As a pseudo code I can give this:
select 1
, max(2)
, 3 referred to the record from max(2)
, 4 referred to the record from max(2)
, ...
, 20 referred to the record from max(2)
from (ViewWithAllJoins) a
group by 1
For privacy and business problem I had to obfuscate some informations, 1/2/3/4... are the name of the column from the view "ViewWithAllJoins", I hope that the problem is still understandable and resolvable!
I've tryied with WINDOW command as reported in Convert keep dense_rank from Oracle query into postgres but I cannot be able to use the group by that I need. Other tryes that I've done was about the dense_rank like shown in Dense_rank first Oracle to Postgresql convert but I can't do any assumption on the order of the data in any of the other fields in exception of 1 and 2, so I can't use any of the aggregate function on them.
Any ideas? Possibly without adding too much subqueryes.
Thank you!
EDIT:
As suggested I'll add some synthetic data to better understand the problem and what I want.
Start:
ID DATE COLUMN1 COLUMN2 COLUMN3
=====================================================================
88888888;"2016-04-02 09:00:00";"aaaaaaaaaaa";"TEXT89" ; 999999999
88888888;"2018-08-21 09:00:00";"a" ;"TEXT1" ; 988888888
88888888;"2017-11-09 09:00:00";"zzzz" ;"TEXT80000" ; 850580582
75858585;"2017-01-31 09:00:00";"~~~~~~~~~~~";"TEXT10" ; 101010101
75858585;"2018-04-02 09:00:00";"eeeeeeeeeee";"TEXT1000" ; 111111111
99999999;"2016-04-02 09:00:00";"8d2ecafd866";"TEXT808911"; 777777777
What I want:
ID DATE COLUMN1 COLUMN2 COLUMN3
===================================================================
88888888;"2018-08-21 09:00:00";"a" ;"TEXT1" ; 988888888
75858585;"2018-04-02 09:00:00";"eeeeeeeeeee";"TEXT1000" ; 111111111
99999999;"2016-04-02 09:00:00";"8d2ecafd866";"TEXT808911"; 777777777
So the group by id, the max of the date and the other fields related to the row of the max date.
-- So you have duplicate records per ID, and for every ID you want to select the record with the most recent date ?
Use NOT EXISTS:
SELECT id,zdate,column1,column2,column3 -- , ...
FROM queryview t
WHERE NOT EXISTS (
SELECT *
FROM queryview x
WHERE x.id=t.id
AND x.zdate > t.zdate
);
Or, use row_number() over a window, and pick only the row with the final date:
SELECT id,zdate,column1,column2,column3 -- , ...
FROM ( SELECT *
, row_number() OVER(PARTITION BY id, ORDER BY zdate DESC) AS rn
FROM queryview
) q
WHERE q.rn = 1
;

PostgreSQL Simple Crosstable (Rows to Columns and Columns to Rows) in a big data set

I have a table looking like this:
CELL day1 day2 day3 day4 ...... day365
1 3,7167 0 0 0,1487 ...... 0,3256
2 0 0 0,2331 0,1461 ...... 1,8765
3 1,431 0,4121 0 1,4321 ...... 0
...
...
...
64800
I would like to transponse my table, so that I have my rows as columns and my columns as rows. A result looking like this:
DAY 1 2 3 ...... 64800
day1 3,7167 0 1,431 ...... ......
day2 0 0 0,4121 ...... ......
day3 0 0,2331 0 ...... ......
day4 0,1487 0,1461 1,4321 ...... ......
...
...
...
day365
My biggest problem is the table size (365 columns and 64800 rows).
How do I write a query where I dont have to define the columns I want in the output. Is there a way to create a table without defining each column and could you show my how my query would have to look like?
My second problem is that I dont even get the crosstab to work in a datasubset .
The following query:
SELECT * FROM crosstab(
'SELECT * FROM 1997_subset ORDER BY 1,2')
AS test("cell" int, "day1" double precision, "day2" double precision, "day3" double precision, "day4" double precision, "day5" double precision)
Gives me this mistake:
ERROR: invalid source data SQL statement
DETAIL: The provided SQL must return 3 columns: rowid, category, and values.
Help is very much appreciated, thank you very much!
You can't do this within PostgreSQL, because it's limited to around 1600 columns in output. Otherwise the crosstab function from the tablefunc module would help you.
You'll need to extract the data to CSV using COPY then use an external tool that can pivot the data. Take a look at ETL tools, or at worst, you might have to script it.
I just checked and Talend at least can do a pivot (row/column transpose, crosstab). Pentaho, CloverETL, etc probably can too.

Adding leading zero if length is not equal to 10 digit using sql

I am trying to join 2 tables but my problem is that one of the table has 10 digit number and the other one may have 10 or less digit number. For this reason, i am loosing some data so i would like to do is check the length first if the length is less than 10 digit then i want to add leading zeros so i can make it 10 digit number. I want to do this when i am joining this so i am not sure if this is possible. Here is an example if i i have 251458 in the TABLE_WITHOUT_LEADING_ZERO then i want to change it like this: 0000251458. Here is what i have so far:
select ACCT_NUM, H.CODE
FROM TABLE_WITH_LEEDING_ZERO D, TABLE_WITHOUT_LEADING_ZERO H
WHERE substring(D.ACCT_NUM from position('.' in D.ACCT_NUM) + 2) = cast (H.CODE as varchar (10))
thanks
Another alternative:
SELECT TO_CHAR(12345,'fm0000000000');
to_char
------------
0000012345
In Netezza you can use LPAD:
select lpad(s.sample,10,0) as result
from (select 12345 as sample) s
result
-------
0000012345
However it would be more efficient to remove the zeros like in the example below:
select cast(trim(Leading '0' from s.sample) as integer) as result
from (select '0000012345' as sample) s
result
-------
12345