A column named "date" in SQL - sql

I have a query like this on sql
SELECT DISTINCT panel_pn_side, serial_nb, date, panel_stts
FROM table
and problem is, it doesn't get that date is a column name, maybe because to it, date is a keyword and not a column. I get the following error :
ERROR: column "date" does not exist
Do you guys have a solution for me ?
Thanks

Actually, even though it was shown as "date" in the database, after thorough research it appears that there were two columns, "date_timestampInUTC" and "date_originalTimezone" and that's why SQL didn't recognize the column named date. I have no idea what happened. Thank you all for the answers

Related

Date in where clause

I'm having to reverse engineer an Oracle query. I'm familiar with SQL but not Oracle and I'm using SQL OPENQUERY to the linked Oracle server. I've gone through the larger portions of the query and figured most of the syntax and getting results but when I get to the following in the where clause I get "missing expression" error. (simplified for clarity)
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= {1}
I can query the table and see that the column USER_DATE is indeed a date field so I don't understand the meaning of >= {1}. This query came to me as "this is how the other dept uses this query, make it work for us" and I don't have access to this other dept. Can someone explain how this supposedly works?
The {1} is just a placeholder for an actual value. Once you've populated this with a proper date value it will run just fine like this:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= '01/01/2019'
You can also use the to_date function for a specific format:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= to_date('01/01/2019', 'mm/dd/yyyy')
Most likely it is a matter of default format of date in your remote Oracle database is different from the "other" database. Most databases allow you to select date into string and also.conpare date to string. The comparison implicitly converts string to date.
Most likely that implicit conversion is failing here. Correct way would be to see if the parameter is a really a string and in what format is date presented there. Then use to_date on right side around {1}.
To make it not error temporarily you can put to_char on left side. Ofcourse your output will be incorrect but since SQL would run, you may be able to troubleshoot

Redshift - Issue displaying time difference in table stored in table

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.

Ambiguous column name for order by the selected column

I never notice below before:
SELECT A,*
FROM Table
ORDER BY A
It gives me the Ambiguous column name error. The * of course contains A, but will sql server take the same column A in above query as two different columns? Is this the reason behind that?
it is because the output has the same name if you did
SELECT A AS A_STANDS_ALONE,*
FROM Table
ORDER BY A
It will work
If you are looking for yes/no answer, then 'YES', you are right.
SQL Selects A column twice, one by explicit and one by the implicit * call.
SQL doesn't understand which one you want to sort by.
The first comment to your post shows that you can use an alias for the column.

Postgresql confuses property as table

I'm trying to use a property that has a .(d0t) in the the propery name, but it seems like postgresql is confused and thinks that i'm trying to access a table property.
Here is the sql query i'm trying to run
select count(metadata.username), metadata.username from appointment join appointment_wrapper a on a.id=appointment_wrapper_id where property='state' and string_value='Kano' and timestamp > '2014-08-01' and timestamp < '2014-11-01' group by metadata.username;
When i run that query it says
ERROR: missing FROM-clause entry for table "metadata"
When i add quotes it says
ERROR: non-integer constant in GROUP BY
Any help on how to solve this?
Thank you
What you're looking for is a "quoted identifier" that you obtain by enclosing your column name in doublequotes to prevent it from being interpreted as a keyword:
SELECT count("metadata.username") from app group by "metadata.username";
Just a reminder that you really, really want to avoid the confusion that having reserved characters in your column or table names bring.

Cast and Sum functions

I am writing a macro which pulls data from Oracle and displays in Excel. In Oracle DB we have a custom table with a column Named "Calculated_Quantity". The datatype of this column is BINARY_DOUBLE. However when I write a query in Excel macro to retreive this column, I get the error as "Data Type is not Supported". So I had to use "Cast" function to bypass this error.
Now I need to sum this column. If I write the statement as
Select Id, SUM(CAST(CALCULATED_QUANTITY AS NUMBER(10))) Qty
from DW.SAMPLE
it works fine, but the calculation is wrong.
If I write
Select Id, CAST(SUM(CALCULATED_QUANTITY AS NUMBER(10))) Qty
from DW.SAMPLE
I get an error as missing right parenthesis. The parenthesis seem to be correct. Help please! –
Select Id, CAST(SUM(CALCULATED_QUANTITY) AS NUMBER(10)) Qty
from DW.SAMPLE