It can't calculate count(*) in a query onto DB2 database - db2

I want to do a count(*) of the number of a rows fom a DB2 database.
The basic query is the following:
select
SUBSTR("Request_Detail",LOCATE('/',"Request_Detail")+1,LOCATE('/',"Request_Detail",LOCATE('/',"Request_Detail")+1)-LOCATE('/',"Request_Detail"))
from "Request_Analisys"
WHERE
"Sample_Date_and_Time">=1200323230000000 and "Sample_Date_and_Time"<1200332300000000
and "Request_Detail" <> '[Summary]'
and "Request_Detail" not like 'WS:%'
Now I'd like to do a count(*) of the resulting rows, but if I do a query like this:
select
count(*),
SUBSTR("Request_Detail",LOCATE('/',"Request_Detail")+1,LOCATE('/',"Request_Detail",LOCATE('/',"Request_Detail")+1)-LOCATE('/',"Request_Detail"))
from "Request_Analisys"
WHERE
"Sample_Date_and_Time">=1200323230000000 and "Sample_Date_and_Time"<1200332300000000
and "Request_Detail" <> '[Summary]'
and "Request_Detail" not like 'WS:%'
It gives the error:
18:51:58 FAILED [SELECT - 0 rows, 0.032 secs] 1) [Code: -119, SQL State: 42803] An expression starting with "Request_Detail" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified.. SQLCODE=-119, SQLSTATE=42803, DRIVER=4.22.29
2) [Code: -727, SQL State: 56098] An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-119", SQLSTATE "42803" and message tokens "Request_Detail".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.22.29
How could I do to get the count of the rows?

Which Request_Detail line's substr would you think it shows after the count?
If you count the lines, the result set will be a single line, and using any columns in it makes no sense.
If you want multiple lines, with a count for each found substr, you need to GROUP BY this substr.

This may work...
select
count(
SUBSTR("Request_Detail"
,LOCATE('/',"Request_Detail")+1
,LOCATE('/',"Request_Detail",LOCATE('/',"Request_Detail")+1)
-LOCATE('/',"Request_Detail")))
)
from "Request_Analisys"
WHERE
"Sample_Date_and_Time">=1200323230000000 and "Sample_Date_and_Time"<1200332300000000
and "Request_Detail" <> '[Summary]'
and "Request_Detail" not like 'WS:%'
But if not this should..
with cte as (
select
SUBSTR("Request_Detail"
,LOCATE('/',"Request_Detail")+1
,LOCATE('/',"Request_Detail",LOCATE('/',"Request_Detail")+1)
-LOCATE('/',"Request_Detail"))) as mydetail
from "Request_Analisys"
WHERE
"Sample_Date_and_Time">=1200323230000000 and "Sample_Date_and_Time"<1200332300000000
and "Request_Detail" <> '[Summary]'
and "Request_Detail" not like 'WS:%'
)
select count(*) from cte

I suggest you use REGEXP_EXTRACT to pick what you want out of your "Request_Detail" column. This is more flexable than using SUBSTR and LOCATE, and will avoid the statement was not executed because a numeric argument of a scalar function is out of range.. error
e.g
select
REGEXP_EXTRACT("Request_Detail",'.*/(.+/)',1,1,'',1)
, SUBSTR("Request_Detail",LOCATE('/',"Request_Detail")+1,LOCATE('/',"Request_Detail",LOCATE('/',"Request_Detail")+1)-LOCATE('/',"Request_Detail"))
FROM TABLE(VALUES('aaaa/bbbb/ccc')) AS T("Request_Detail")
returns
1 |2
------|-----
bbbb/ |bbbb/
so, you could then do this
SELECT
COUNT(*)
, REGEXP_EXTRACT("Request_Detail",'.*/(.+/)',1,1,'',1)
FROM
"Request_Analisys"
GROUP BY
REGEXP_EXTRACT("Request_Detail",'.*/(.+/)',1,1,'',1)
for example

Related

ERROR: Teradata prepare: Syntax error when usign date format 'ddmmyyy'd in SAS

I'm using this code to extract information from this database. However, it is showing me this error:
ERROR: Teradata prepare: Syntax error, expected something like ')' between a string or a Unicode character literal and the word
'd'. SQL statement was: WITH vmher102ult as ( select cod_cte, max(fec_consulta) as max_fec_consulta from
klarmxpw_her.vmher102 where cod_cte not in ('','0','00000000') and fec_consulta>='01MAR2021'd group by cod_cte) select t1.*
from klarmxpw_her.vmher101 as t1 inner join vmher102ult as t2 on t1.cod_cte=t2.cod_cte and
t1.fec_consulta=t2.max_fec_consulta.
The code I'm using for this pass through is the following:
proc sql;
connect to teradata as tera (user=&tuser. password=&tpass. server='TDMX03');
create table vmher101_m as
select * from connection to tera (
WITH vmher102ult as (
select cod_cte, max(fec_consulta) as max_fec_consulta
from klarmxpw_her.vmher102
where cod_cte not in ('','0','00000000')
and fec_consulta>='01MAR2021'd
group by cod_cte)
select t1.*
from klarmxpw_her.vmher101 as t1
inner join vmher102ult as t2
on t1.cod_cte=t2.cod_cte and t1.fec_consulta=t2.max_fec_consulta);
disconnect from ter;
Does anybody know what can I do?
You need to use TERADATA code inside the () after from connection to tera.
Try
and fec_consulta>= DATE '2021-03-01'
Teradata Documentation

LPAD function errors when used in WITH variable in Redshift

Can you tell me why this is throwing an error in Redshift?
WITH Testing_PADDING AS (SELECT '12345678' AS column1)
SELECT LPAD(column1, 9,'0') FROM Testing_PADDING;
Here is the error I receive:
"Invalid operation: failed to find conversion function from "unknown" to text;"
Redshift can't determine data type from the context, so you need to explicitly set it
WITH Testing_PADDING AS (SELECT '12345678'::text AS column1)
SELECT
LPAD(column1, 9, '0')
FROM Testing_PADDING;
I suspect that one of your strings isn't being seen as text - likely the column1 text. (Sorry don't have a cluster up not to test)
Try:
WITH Testing_PADDING AS (SELECT '12345678'::text AS column1)
SELECT LPAD(column1, 9,'0'::text) FROM Testing_PADDING;

Postgresql: HAVING statement that skips rows which are null or empty?

Is there any way to skip rows that are null or empty? I could use some help with sorting the output of a subtable. My having statement is returning an error I can’t fix.
It returns the error: ERROR: invalid input syntax for integer: " "
This seems to be because some rows in my table will have either null values or be empty and the having statement is getting hung up there.
Here is the full query
SELECT
count(job),
year,
zipcode
FROM
(
SELECT
substring (cast(dobjobs.prefilingdate AS varchar), '^\d\d\d\d') AS year,
dobjobs.job,
dobjobs.bbl,
pluto_17v1.zipcode
FROM
dobjobs
JOIN pluto_17v1 ON dobjobs.bbl = pluto_17v1.bbl
GROUP BY
dobjobs.prefilingdate,
dobjobs.bbl,
pluto_17v1.zipcode,
dobjobs.job
ORDER BY
year
) AS sub
GROUP BY
year, zipcode
HAVING
CAST( zipcode AS int ) IN (10039, 10039, 10026, 10030, 10037, 10027, 10032, 10033, 10040, 10034, 10031)
ORDER BY
year;

PostgreSql Group By and aggreate function error

My problem is, when I run the following query in MySQL, it looks like this
Query;
SELECT
CONCAT(b.tarih, '#', CONCAT(b.enlem, ',', b.boylam), '#', b.aldigi_yol) AS IlkMesaiEnlemBoylamImei,
CONCAT(tson.max_tarih, '#', CONCAT(tson.max_enlem, ',', tson.max_boylam), '#', tson.max_aldigi_yol) AS SonMesaiEnlemBoylamImei,
Max(CAST(b.hiz AS UNSIGNED)) As EnYuksekHiz,
TIME_FORMAT(Sec_TO_TIME(TIMESTAMPDIFF(SECOND, (b.tarih), (tson.max_tarih))), '%H:%i') AS DurmaSuresi
FROM
(Select id as max_id, tarih as max_tarih, enlem as max_enlem, boylam as max_boylam, aldigi_yol as max_aldigi_yol from _213gl2015016424 where id in(
SELECT MAX(id)
FROM _213gl2015016424 where (tarih between DATE('2016-11-30 05:45:00') AND Date('2017-01-13 14:19:06')) AND CAST(hiz AS UNSIGNED) > 0
GROUP BY DATE(tarih))
) tson
LEFT JOIN _213gl2015016424 a ON a.id = tson.max_id
LEFT JOIN _213gl2015016424 b ON DATE(b.tarih) = DATE(a.tarih)
WHERE b.tarih is not null And (b.tarih between DATE('2016-11-30 05:45:00') AND Date('2017-01-13 14:19:06')) AND b.hiz > 0
GROUP BY tson.max_tarih
Output is order by date;
Result query
When I try to run a query in PostgreSQL, I get group by mistake.
Query;
SELECT
CONCAT(b.tarih, '#', CONCAT(b.enlem, ',', b.boylam), '#', b.toplamyol) AS IlkMesaiEnlemBoylamImei,
CONCAT(tson.max_tarih, '#', CONCAT(tson.max_enlem, ',', tson.max_boylam), '#', tson.max_toplamyol) AS SonMesaiEnlemBoylamImei,
Max(CAST(b.hiz AS OID)) As EnYuksekHiz,
to_char(to_timestamp((extract(epoch from (tson.max_tarih)) - extract(epoch from (b.tarih)))) - interval '2 hour','HH24:MI') AS DurmaSuresi
FROM
(Select id as max_id, tarih as max_tarih, enlem as max_enlem, boylam as max_boylam, toplamyol as max_toplamyol from _213GL2016008691 where id in(
SELECT MAX(id)
FROM _213GL2016008691 where (tarih between DATE('2018-02-01 03:31:54') AND DATE('2018-03-01 03:31:54')) AND CAST(hiz AS OID) > 0
GROUP BY DATE(tarih))
) tson
LEFT JOIN _213GL2016008691 a ON a.id = tson.max_id
LEFT JOIN _213GL2016008691 b ON DATE(b.tarih) = DATE(a.tarih)
WHERE b.tarih is not null And (b.tarih between DATE('2018-02-12 03:31:54') AND DATE('2018-02-13 03:31:54')) AND b.hiz > 0
GROUP BY tson.max_tarih
Group by error is : To use the aggregate function, you must add the column "b.tarih" to the GROUP BY list.
When I add it I get the same error for another column.I'm waiting for your help.
You are using a feature of MySQL that is not standard SQL and you can also deactivate.
You are grouping by tson.max_tarih in your query. That means that for all rows that share the same value in that field, you will get only one row as a result of that group.
If you have several different values in the rest of the fields (enlem, boylam, etc...) which one are you trying to get in as the result of the query? That's the question that PostgreSQL is asking you.
MySQL is just returning any value for those fields among the rows in the group. PostgreSQL requires you to actually specify it.
Two typical solutions would be grouping by the rest of the fields (b.tarih, b.enlem) or specifying the value those fields to something like MAX(b.tarih), etc.

How to get the count of records returned by teradata sql griup by query

I have a condition where I need to check number of rows returned by select query in Teradata.
My query looks like below
select
PDate ,Risk ,BName ,BNumber ,
ONumber ,OnNumber ,ID_CD ,Entity ,
AU ,RType, count (*)
from Load_one.import_test
group by 1,2,3,4,5,6,7,8,9,10
having count (*) > 1;
So I would like to know the count of rows it returns. I tried something like below
Select Count(*)
From ( select
PDate ,Risk ,BName ,BNumber ,
ONumber ,OnNumber ,ID_CD ,Entity ,
AU ,RType, count (*)
from Load_one.import_test
group by 1,2,3,4,5,6,7,8,9,10
having count (*) > 1;
) as temp
It returning an error: select failed 3707 expected something like an 'EXCEPT' keyword or an 'UNION' keyword or a 'MINUS' keyword between an integer and ;
Please help me.