Hello everyone, I have a problem to get the result use this formula
LISTAGG at Razor in DB2. Can you help me what the problem with this formula and how to solve it?
This is my Query:
SELECT
REFRV_REV_CODE
, LISTAGG(Cast(REFRV_DESCRIPTION as VARCHAR), ', ') AS Groups
FROM
tref_revenue_code
GROUP BY REFRV_REV_CODE
The error SQL0604N refers to an invalid type precision. I would assume that the VARCHAR is at fault. Have you tried to restrict the VARCHAR to a length, e.g., use VARCHAR(200) instead of VARCHAR alone?
LISTAGG is only supported on Db2 for LUW from version 10.1 onwards, your error message implies that you are using an earlier version.
Your code works on e.g. Db2 11.
WITH tref_revenue_code(REFRV_REV_CODE, REFRV_DESCRIPTION) AS (VALUES (1,'a'),(1,'b'))
SELECT
REFRV_REV_CODE
, LISTAGG(Cast(REFRV_DESCRIPTION as VARCHAR), ', ') AS Groups
FROM
tref_revenue_code
GROUP BY
REFRV_REV_CODE
returns
REFRV_REV_CODE GROUPS
-------------- ------
1 a, b
Related
I am trying to query against database table h_cmdb_assets to return a count of and entries for h_owned_by_name that do have a name against them but only if h_record_state is Active or Operational (0 or 1) but the query fails.
I am just learning SQL and am unsure as to whether you can use 2 WHERE commands or whether I just have the syntax wrong.
I am not using MYSQL to run this query but I am using it within the database direct query function of our ITSM tool - Hornbill Service Manager.
I am using:
SELECT count(*)
FROM h_cmdb_assets
WHERE h_record_state in ('0','1')
or h_operational_state in ('0','1')
AND
WHERE h_owned_by_name is null
or h_owned_by_name =' '
order
by count(*) desc
Any pointers gratefully received.
Regards,
Andrew
WHERE is a clause that is allowed at most one time per SELECT.
I am trying to query against database table h_cmdb_assets to return a count of and entries for h_owned_by_name that do have a name against them but only if h_record_state is Active or Operational (0 or 1)
What you are looking for is more complex boolean logic:
SELECT count(*)
FROM h_cmdb_assets
WHERE h_record_state in (0, 1) AND
(h_owned_by_name IS NULL OR h_owned_by_name = '');
Notes:
The question only references h_record_state, not h_operational_state, so I removed that. I assume the 0 and 1 refer to active/operational.
0 and 1 look like numbers. Compare numbers to numbers and strings to strings. Try not to mix types.
You use ' ' for a blank string. I think '' is more likely. If you want to eliminate spaces as well, then you can use trim() or replace(name, ' ', '') = '').
An aggregation query with no ORDER BY always returns exactly one row. The ORDER BY is superfluous.
Table Name: Worker,
Fields : worker_id | first_name | last_name | department
I have a table name worker and i wanted to write an SQL query that fetches the unique values of DEPARTMENT from Worker table and prints its length. So i tried running this : (Database- Mysql)
select length(distinct(department)) from worker;
But it is giving an error saying:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct(department)) from worker' at line 1
But when i ran below query, it works perfectly fine:
select distinct(length(department)) from worker;
Can somebody please explain me why interchanging distinct and length function makes query works?
Thanks in advance!
Try not to use distinct like function but clause otherwise it will give syntax error.
Below sql statement will execute as shown below:
select distinct (length('xyz')) ---- length('xyz') : 3
select distinct (3) ---- output : 3
Distinct is not properly a function but a clause
select distinct length(department) from worker;
Anyway in MySQL work also with function syntax
select distinct( length(department)) from worker;
The code with the exchanged token don't work because DISTINCT produce an aggregated result removing the duplicated values,this implies that the outer length() function work on not correct set of rows or better the db engine see that there an improper use of the DISTINCT clause and raise the syntax error
select length( distinct 'A' ) this raise an error
If you want use the outer length() function you should code this way
select length(my_col) from (
select distinct department my_col from worker
) ;
correct answer :
select distinct <column_dept> as department***,*** (len(<column_dept>) as length_column_dept from xyz_table
select distinct( length(department)),department from worker group by department;
select distinct (Department) as 'Unique department', len(Department) as 'length of name' from Worker;
I need to apply a Rank function in MYSQL 5.6.10. The issue is, I am getting a syntax error when i am using the standard syntax i found online
Eg:
INSERT INTO t(val)
VALUES(1),(2),(2),(3),(4),(4),(5);
SELECT
val,
RANK() OVER (
ORDER BY val
) my_rank
FROM
t;
I am getting a red line suggesting a syntax error near 'OVER ('
As far as i know window functions are introduced after version 8 and above.
source
Something like this may give you some insights.
SELECT
val,
(select 1+count(*) from t b where t.val>b.val)"rank"
FROM
t;
Demo
RANK is only available in MySQL-8.0+
I have a series of tables with the same prefix, and I need to select data from the latest version --whose postfix with the highest numeric number. Here is what I have:
SELECT
#latest_version_number :=
MAX(
CAST(SUBSTRING_INDEX(table_name,'_',-1) AS UNSIGNED)
)
FROM information_schema.tables
WHERE lower(table_name) like '{table_prefix}%';
SELECT
*
FROM CONCAT('`{table_prefix}', CAST(#latest_version_number AS CHAR), '`')
It behaved like what I expected when ran as 2 separate queries in the console. But I got "syntax error" trying to run it as a single query. What's the cleanest way to refactor this into a single query? Thanks
The only way I'm aware that you can use a variable in a table name in MySQL is using the prepare + execute statements.
Here is an example I found online that gives you exact instructions.
https://www.tutorialspoint.com/set-user-defined-variable-with-table-name-in-mysql-prepare-statement
The following query works in MySQL:
SELECT
f.created_date as time_sec
,sum(f.value) as value
, date_format( f.created_date , '%a') as metric
FROM ck_view_fills as f
GROUP BY date_format(f.created_date, '%a' )
I have migrated my database to PostgreSQL and I am now converting my queries to match. My naive conversion looks like this:
SELECT
f.created_date as time_sec
,sum(f.value) as value
, to_char( f.created_date , "D") as metric
FROM ck_view_fills as f
GROUP BY to_char( f.created_date , "D")
This query is not accepted and the error message produced by PostgreSQL is the following:
Error in query (7): ERROR: column "f.created_date" must appear in the
GROUP BY clause or be used in an aggregate function LINE 2:
f.created_date as time_sec
As far as I can tell f.created_date is indeed used in the group by clause. I have also seen examples using this very syntax. So what is the cause of this error and how do I get around it?
Postgres is correct. In fact, your query would fail with the most recent versions of MySQL as well -- using the default settings.
Just use an aggregation function:
SELECT MIN(f.created_date) as time_sec,
SUM(f.value) as value
TO_CHAR(f.created_date, 'D') as metric
FROM ck_view_fills f
GROUP BY to_char(f.created_date , 'D');
You should have used a similar construct in MySQL (regarding MIN() -- or MAX()).