How can I sort DB2 results based on month and year parameter from separate column - sql

I am using DB2.
I have a query that returns a specific date in the following format: yyyy-mm-dd
SELECT DATE FROM ABC.DTS
Then I have devised another query that is supposed to return results that match the date returned above
SELECT COUNT(*)
FROM ABC.ATY
WHERE ID between 1 and 1000000000
AND MONTH(PRS) = MONTH(from DTS)
AND YEAR(PRS) = YEAR(from DTS)
AND CKPYE = ' '
;
I am getting an error when I run the second query that says: ILLEGAL SYMBOL "MONTH". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: XMLELEMENTXMLPI. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.19.56.
How can I return the results that match the date that is returned from the first query?
Is there another route/strategy I need to instead of the one I am currently trying?
Many thanks in advance.

See if this works for you:
SELECT COUNT(*)
FROM ABC.ATY
JOIN ABC.DTS ON MONTH(PRS) = MONTH(Date) AND YEAR(PRS) = YEAR(DATE)
WHERE ID between 1 and 1000000000
AND CKPYE = ' '
;

Related

How to write a DB2 select query to include the string before the '-' delimiter

I have a column "Department" in my Table "College". Department is having data like
Commerce1-683877
Science2-678900
I need to write a select query which returns only Commerce1 and Science2.
I am completely new to DB2, please help me solve this.
select substring(department,0,CHARINDEX('-',department) as DEPT from College
Edit -1 : Thanks #Charles
I tried your solution :
select substring(department , 1, LOCATE('-',department) - 1) AS DEPT from College
but it's throwing me an error :
SQL Error [42815]: THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 3 OF SUBSTRING IS INVALID. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.9.78
Edit 2: You're right #Charles, all my rows doesn't contain '-'
I tried using the below query as well but getting the same error :
select substring(department,1, LOCATE('-',department || '-') - 1) as DEPT from College
What platform and version of Db2?
Did you look for an appropriate Db2 SQL Reference manual?
Note Db2 SQL string & arrays start at 1, not 0.
select substring(department,1, LOCATE('-',department) - 1) as DEPT from College
One method uses regexp_substr():
regexp_substr(department, '^[^-]+')

How to convert use of max in Oracle code to BigQuery

How to convert the Oracle code below to BigQuery?
max(to_date(BIC_GM_AGCPOAODS00_BO_VW.BOUND_DATE,'yyyymmdd'))
When I try this code:
SELECT A._BIC_GCISBN,
max(cast(A.BOUND_DATE as date),'yyyymmdd')
FROM `BIC_GM_AGCPOAODS00_BO_VW` A
WHERE A._BIC_ZC2GRIRIN = 'G' AND A._BIC_ZCLOEKZ = ' '
GROUP BY A._BIC_GCISBN
I am get the error:
No matching signature for aggregate function MAX for argument types: DATE, STRING. Supported signature: MAX(ANY) at [15:2]
In BigQuery, you want parse_date():
SELECT A._BIC_GCISBN,
MAX(PARSE_DATE('%Y%m%d', A.BOUND_DATE))
FROM `BIC_GM_AGCPOAODS00_BO_VW`A
WHERE A._BIC_ZC2GRIRIN = 'G' AND A._BIC_ZCLOEKZ = ' '
GROUP BY A._BIC_GCISBN;
You should be storing the value using date, but sometimes we don't have control over how data is stored.
EDIT:
Given your data format, you can also write this as:
SELECT A._BIC_GCISBN,
PARSE_DATE('%Y%m%d', MAX(A.BOUND_DATE))
FROM `BIC_GM_AGCPOAODS00_BO_VW`A
WHERE A._BIC_ZC2GRIRIN = 'G' AND A._BIC_ZCLOEKZ = ' '
GROUP BY A._BIC_GCISBN;
I prefer the first version because it generalizes readily to any data format. This might be more efficient.

how to use case with count function in oracle plsql

here is the query, i want to use case statement with count function in oracle.
Select case when count(*) > 0 then 'doSomething' else 'doSomething'
from student where student_name='faizan ahmed' and student_father='ahmed' and UPPER(student_dob)=UPPER('01-FEB-19');
please help me out, using plsql code.
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
For this purpose, use exists instead:
Select (case when exists (select 1
from student
where student_name = 'faizan ahmed' and
student_father = 'ahmed' and
upper(student_dob) = upper('01-FEB-19');
then 'doSomething'
else 'doSomethingElse'
end)
from dual;
EXISTS is usually more efficient than a count, because it can stop at the first matching row instead of aggregating the whole table.
You're missing an END for CASE:
SELECT CASE WHEN COUNT (*) > 0 THEN
'doSomething'
ELSE 'doSomething'
END --> This
FROM student
WHERE student_name = 'faizan ahmed'
AND student_father = 'ahmed'
AND UPPER (student_dob) = date '2019-02-01' -- No! UPPER ('01-FEB-19');
It is easier to spot if you format code you write.
Apart from that, STUDENT_DOB seems to be a date. If so, then don't compare it to a string (because '01-feb-19' IS a string) but to a date (date '2019-02-01' - it is a date literal, consists of the date keyword and yyyy-mm-dd value).
Also, it is strange that you used UPPER with that "date" string, but all your names are lowercase. Hm?

How to use INTO and GROUP BY clause together

SELECT cast ( SUBSTRING ( CAST ("ProcessingDate" AS text), 5, 2 ) as integer),
COUNT(*) INTO resultValue1,resultValue2
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y' AND "ProcessingDate" >= 20160110
GROUP BY 1
ORDER BY 1;
In my database, the ProcessingDate is stored as YYYYMMDD. So, I am extracting its month from it.
This query is working fine if we remove the INTO clause but, I want to store the result to use it further.
So what should be the datatype of the variable resultValue1 and resultValue2 how to store the data(because data will be multiple).
As I am new to PostgreSQL I don't know how to do this can anybody help me out.
Here resultValue1 & resultValue2 will be tables not variables. you can group by using the column names.
Give some column alias names for both columns and group by using them.
You probably want this.
SELECT cast ( SUBSTRING ( cast ("ProcessingDate" as text),5 , 2 ) as
integer) AS resultValue1, COUNT(*) AS resultValue2
INTO <NewTable> --NewTable will be created with those two columns
FROM "DemoLogs"."Project"
-- conditions
Group By 1
-- other contitions/clauses
;
Kindly refer this INTO Documentation.
Hope this helps.
Try this:
SELECT cast ( SUBSTRING ( cast ("ProcessingDate" as text),5 , 2 ) as integer)resultValue1,
COUNT(*)resultValue2
INTO <Table Name>
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y'
AND "ProcessingDate" >= '20160110'
Group By 1
Order By 1;
Store the above query in the variable and remove that INTO clause from it.
So, query will be now :
query = SELECT cast ( SUBSTRING ( CAST ("ProcessingDate" AS text), 5, 2 ) as
integer),
COUNT(*)
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y' AND "ProcessingDate" >= 20160110
GROUP BY 1
ORDER BY 1;
Now declare result_data of type record and use in the following manner:
We are looping over result_data because it gives number of rows as output
and I have declared resultset of type text to store the result (as I needed further)
FOR result_data IN EXECUTE query
LOOP
RAISE NOTICE 'result : %',to_json(result_data);
resultset = resultset || to_json(result_data);
END LOOP;

sql - conversion error when changing 'and' to 'or'

I have a database table in sql server 2008. My query is breaking for a reason that is unknown to me:
select release_id, url_id,tt_projectid, release_title, tech_area, current_state, dateadd(ss,last_built,'12/31/1969 20:00:00') as recent_Date,
autobuild_count, manualbuild_count, bcm_build_count, config_count, force_count, transition_only_count,
auto_integ_count,last_auto_integ,dateadd(ss,integ_complete_date,'12/31/1969 20:00:00') as integ_date
from tablename
where (auto_integ_count > 0
and MONTH(last_auto_integ) = '1'
and YEAR(last_auto_integ) = '2013')
and (release_id > 36000)
order by release_id desc
The above query works fine, but when I change the last line of the where close from 'and' to 'or' I get this conversion error:
Conversion failed when converting date and/or time from character string.
I'm puzzled as to why changing
'and (release_id > 36000)'
to
'or (release_id > 36000)'
would cause such an error
The reason is because last_auto_integ is being stored as a string rather than as a date. These lines in the where clause are not being executed with the and -- by happenstance -- because none occur when release_id > 360000.
From what I can see, there are no other places in the query where you might be converting a string to a date format.
You can identify these values using:
select last_auto_integ
from tablename
where isdate(last_auto_integ) = 0 and last_auto_integ is not null
You can fix the problem in the query by using case:
where month(case when isdate(last_auto_integ) = 1 then last_auto_integ end) = 1 and
year(case when isdate(last_auto_integ) = 1 then last_auto_integ end) = 2013
Or, you can just use substring() to extract the month and year from whatever date format you are using.
Because when you change AND to OR you are getting a lot more rows returned, and one of these other expressions is failing:
dateadd(ss,integ_complete_date,'12/31/1969 20:00:00')
MONTH(last_auto_integ)
YEAR(last_auto_integ)
With only AND, it doesn't need to evaluate the other expressions for rows whose release_id is <= 36000, so it's not encountering the strings that are causing the problem on date conversion.