Why do I get an invalid identifier error in this query?
with
select
notif_set_code as notification_code
, (
select tab_to_string(
cast(collect(
case
when area_type is not null then
count(*) || ' ' || area_type
else 'default'
end
) as t_varchar2_tab), ' ,')
from my_table_2
where notif_set_code = nsd.notification_code
) areas
from my_table nsd
...
It sayd nsd.notification_code is not a valid identifier.
Using nsd.notif_set_code works if it's not in a with clause!
Presumably, there is no column called notification_code in nsd. You seem to intend:
where notif_set_code = nsd.notif_set_code
Renaming the column in the select doesn't change anything (although rather confusingly MySQL seems to allow column aliases in subqueries but that is an anomaly).
However, you should really qualify all column references:
where my_table_2.notif_set_code = nsd.notif_set_code
Related
I am using LISTAGG() function in my select statement in a procedure such as
SELECT DISTINCT REPLACE(LISTAGG(a.student1||':'||a.score||'+')
WITHIN GROUP ( ORDER BY a.roll_no) OVER (PARTITION BY a.class)
If my select statement has not null values then i get
for eg: ABC:100
But if the select is empty then i get
for eg: :
I wanted it to be null if there are no rows selected.
The problem is that the expression you're aggregating returns a non-NULL value whether or not the values you're concatenating are NULL. My guess is you want something like
listagg(case when a.student1 is not null
then a.student1||':'||a.score||'+'
else null
end)
If you can have a null value for student1 but a non-NULL value for score, you'd need to adjust the case statement to specify what you want to happen in that case (do you want the ":" and the "+"? Just the "+"?)
Well, your REPLACE is incomplete. Also, is '+' really supposed to be concatenated? What string separates values, then?
REPLACE (
LISTAGG (a.student1 || ':' || a.score, '+') --> removed || for the + sign
WITHIN GROUP (ORDER BY a.roll_no)
OVER (PARTITION BY a.class),
':', '') --> missing arguments for REPLACE
If that's still not what you asked, please, provide sample test data and desired output.
So I got this table FOO which has a column of the type VARCHAR and is named COMMENT (which happens to be a reserved keyword).
When I am trying to use it in a CONCAT function in my select the result is NULL.
How can I fix this?
SELECT
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), COMMENT),'"}')
FROM
SOMESCHEMA.FOO
I also tried to use " or ' around COMMENT, but then it is interpreted as a VARCHAR...
2nd I used ` but that happens to print me the following error.
[Code: -104, SQL State: 42601] ILLEGAL SYMBOL "`". SOME SYMBOLS THAT MIGHT BE LEGAL ARE:.
I also tried to add the SCHEMA and the TABLE name in front of the column like:
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), SOMESCHEMA.FOO.COMMENT),'"}')
But no luck.
Did you try this?
SELECT CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME
), '","COMMENT":"'
), "COMMENT"
),
'"}')
FROM SOMESCHEMA.FOO
That is, double quotes only around the column name.
I would find this simpler to read using the infix operator:
SELECT '{"NAME":"' CONCAT NAME CONCAT '","COMMENT":"' CONCAT "COMMENT" CONCAT '"}'
FROM SOMESCHEMA.FOO
or:
SELECT '{"NAME":"' || NAME || '","COMMENT":"' || "COMMENT" || '"}'
FROM SOMESCHEMA.FOO
It seems like DB2 also accept the ANSI/ISO SQL || concatenation:
SELECT
'{"NAME":"' || NAME || '","COMMENT":"' || COMMENT || '"}'
FROM
SOMESCHEMA.FOO
Using Teradata SQL, I'm running a query against an 'Order History' table I loaded, which involves finding the most recent order where a particular change occured. I ran into some trouble as the Order_Date was loaded as VARCHAR with varying lengths, however I've gotten around this by using CASE WHEN to align the character lengths of the dates and casting the column as a timestamp.
I now want to save this query as a VIEW, however the CREATE VIEW statement fails as the date "does not match a Defined Type name". I'm not sure why this error is occurring as the actual statement runs fine?
Could someone help point out what I am missing?
Create VIEW DB.ViewName as (
select Serv_No, Serv_Attrib, Order_Activ_Date
from (
select Serv_No, Serv_Attrib,
cast
(
CASE WHEN char_length(Order_Act_Date) = 21 AND index(order_act_Date,' ') = 10
THEN '0' || '' || Order_Act_Date
WHEN char_length(Order_Act_Date) = 20 AND index(order_act_Date,' ') = 10
THEN '0' || '' || substr(Order_Act_Date,1,10) || '0' || substr(Order_Act_Date,length(order_act_Date) - 10 + 1,10)
WHEN char_length(Order_Act_Date) = 21 AND index(order_act_Date,' ') = 11
THEN substr(Order_Act_Date,1,10) || ' 0' || substr(Order_Act_Date,length(order_act_Date) - 10 + 1,10)
WHEN Order_Act_Date IS NULL
THEN '01/01/1900 11:00:00 AM'
WHEN char_length(Order_Act_Date) = 22
THEN Order_Act_Date
END as timestamp format 'DD/MM/YYYYBHH:MI:SSBT'
) as Order_Activ_Date
from DB.Table
Qualify
Coalesce( max(Serv_Attrib) OVER (Partition By Serv_No ORDER BY ORDER_ACTIV_DATE DESC ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING
) ,Serv_Attrib || 'x') <> Serv_Attrib
) as bb
Qualify rank() over (partition by Serv_No ORDER BY Order_Activ_Date DESC) = 1
)
You probably run this in an ODBC connection, then it's due to the LENGTH function.
LENGTH is an ODBC-function which is replaced by the ODBC-driver with correct Teradata SQL, but only for SELECTs and not within DDL.
So simply change the remaining LENGTH to CHAR_LENGTH, too.
Btw, if you're on TD14, you can utilize Oracle's TO_TIMESTAMP, which is more flexible regarding single digit day/hour/minute/second. Following will cast all cases without the need to add leading zeroes:
TO_TIMESTAMP(Order_Act_Date, 'DD/MM/YYYY HH:MI:SS AM')
SQLite 3.7.11
Given I have these two records in a SQLITE table, with the field called name:
"preview"
"view"
If I run the query:
SELECT * FROM table WHERE name LIKE "%" + $keyword + "%" ORDER BY name
with $keyword set to "vi"
I would get the results in this order:
1) "preview"
2) "view"
Is there a way to order so that the names whose first letter is the same as the first letter of the keyword (in this example "v") would come first?
You can sort by the position of your keyword in the search string
SELECT * FROM your_table
WHERE name LIKE concat('%', '$keyword', '%')
ORDER BY POSITION('$keyword' IN name)
name
Not sure if it can be done less verbose, but this should work. I used ? as a placeholder for your variable. The query as you posted it doesn't seem to be correct SQL.
SELECT * FROM table1
WHERE name LIKE '%' || ? || '%'
ORDER BY (CASE WHEN SUBSTR(Name,1,1) = SUBSTR(?,1,1) THEN 0 ELSE 1 END),
name
I am using PostgreSQL with PostGis. I am executing a query like this:
select st_geomfromtext('point(22 232)',432)
It works fine. But now I want to take a value through a query. for example:
select st_geomfromtext('point((select x from data_name where id=1) 232)' , 432)
Here data_name is some table I am using and x stores some values. Now query inside is treated as a string and no value is returned.
Please help.
ERROR: syntax error at or near "select"
Try this:
select st_geomfromtext('point(' || x || ' 232)', 432) from data_name where id=1
Postgis has a function ST_MakePoint that is faster than ST_GeomFromText.
select ST_SetSRID(ST_MakePoint(x),432) from data_name where id=1;
While #muratgu answer is generally the way to go, one minor note:
A subquery gets you a different result when no row is found for id = 1. Then you get nothing back (no row), instead of:
select st_geomfromtext(NULL, 432)
If you need a drop-in replacement:
select st_geomfromtext('point('
|| (select x from data_name where id=1)
|| ' 232)' , 432)