I am trying to find the time difference between two time stamps and store it in a column. When I check the output in the table, I see the value to be a huge number and not the difference in day/hours. I am using amazon redshift as the database.
Data_type of time_duration column : varchar
Given below is the sample:
order_no,order_date,complain_date,time_duration
1001,2018-03-10 04:00:00,2018-03-11 07:00:00,97200000000
But I am expecting time_duration column to show 1 day,3 hours
This issue happens when I store the time_duration in a table and then query to view the output.
Could anyone assist.
Thanks.
Do it following way, it will give hours difference
select datediff(hour,order_date,complain_date) as diff_in_hour from your_table ;
If you want to do it day, do it following way
select datediff(day,order_date,complain_date) as diff_in_day from your_table;
You could use datediff function to update your table column time_duration.
Refer Redshift documentation for more details.
Related
I have 1 partition on column _installed_at_ (timestamp), see
here. But when I run
SELECT * FROM `data-analytics-experiment.data_3rd_party.raw_adjust` WHERE DATE(_installed_at_) = "2022-05-31" LIMIT 1000
This query processed all the tables, the partition is not running. This query returned no results.
See here
Help please T.T
Below screenshot says that your table is partitioned but most values in _installed_at_ which is a partition column are not valid.
You might want to check if _installed_at_ is properly generated or parsed from string-formatted timestamp.
I have a table in my SQL Server database. I am showing my table with some data in image called input.
And from this input I want to add one derived column in that column the data should be below format. I am showing my expected output in image called output.
How can I achieve my expected output with a SQL query?
We have lots of records in the table, but the maximum length of CODE column is 4. Means the last value in that column is 9999 only.
Please suggest how I can get my expected output with a simple SQL query.
Best Regards,
Phani Kumar.
SELECT *,
'C' + RIGHT('000'+CONVERT(VARCHAR(4),Code),4) [YourColumn]
FROM dbo.YourTable;
I'm pulling my hair out this morning, as I'm trying to select a simple average from a single field from a table in an Oracle database. My table has 31 rows, the column in question is called AGE and I just want an average. The column is of type "number" and there are no nulls in it.
SELECT AVG(AGE)
FROM COLLECTIONS.CUSTOMERS
This query always returns null. I have also tried:
SELECT SUM(AGE)/COUNT(AGE)
FROM COLLECTIONS.CUSTOMERS
with the same result. Any help is greatly appreciated!
I have tried creating a sample table with single column (age). I kept data type int and number both and getting expected result:
INTEGER : Demo
NUMBER : Demo
NUMBER (with same datatype as OP): Demo
I just need to make a simple query of a table on a MapR cluster in that I want to know what the date is of the most recent record in the table. Dates are in a 'report_date' column in string format. I tried the following query without success:
select max(report_date) from partition.table_name
I know the second part of the statement works. Is there something wrong with the first part?
Thanks,
A
Your date column datatype is string hence the max function doesnt produce the output as desired.
for example : string column with values 1,2,3,4 and when you run max(column) you wont get the output as 4 , since max doesnt work on string datatype.
Try changing your datatype to DATE or TIMESTAMP , Which should work.
OR
if changing datatype is not possible then try,
If there is an auto incrementing ID column in the table or any column like so , then
select report_date from table_name order by ID desc.
This should provide you the max date sting.
I run a daily report that has to query another table which is updated separately. Due to the high volume of records in the source table (8M+ per day) each day is stored in it's own partition. The partition has a standard format as P ... 4 digit year ... 2 digit month ... 2 digit date, so yesterday's partition is P20140907.
At the moment I use this expression, but have to manually change the name of the partition each day:
select * from <source_table> partition (P20140907) where ....
By using sysdate, toChar and Concat I have created another table called P_NAME2 that will automatically generate and update a string value as the name of the partition that I need to read. Now I need to update my main query so it does this:
select * from <source_table> partition (<string from P_NAME2>) where ....
You are working too hard. Oracle already does all these things for you. If you query the table using the correct date range oracle will perform the operation only on the relevant partitions - this is called pruning .
I suggest reading the docs on that.
If you'r still skeptic, Query all_tab_partitions.HIGH_VALUE to get each partitions high value (the table you created ... ).
I thought I'd pop back to share how I solved this in the end. The source database has a habit of leaking dates across partitions which is why queries for one day were going outside a single partition. I can't affect this, just work around it ...
begin
execute immediate
'create table LL_TEST as
select *
from SCHEMA.TABLE Partition(P'||TO_CHAR(sysdate,'YYYYMMDD')||')
where COLUMN_A=''Something''
and COLUMN_B=''Something Else''
';
end
;
Using the PL/SQL script I create the partition name with TO_CHAR(sysdate,'YYYYMMDD') and concatenate the rest of the query around it.
Note that the values you are searching for in the where clause require double apostrophes so to send 'Something' to the query you need ''Something'' in the script.
It may not be pretty, but it works on the database that I have to use.