Related
I have a query that was written in Presto SQL format (100 lines of insert a query result to a table that already exists) and takes within 10 minutes to get the result.
Now I am going to use Airflow and need to change the query to Hive SQL format to append previous month's data, there is no error, but it is taking 75+ minutes now and the query is still running and not returning any result.
Shall I 'stop' it or is there anything else to consider?
SET hive.limit.query.max.table.partition = 1000000;
INSERT INTO TABLE schema.temp_tbl partition(year_month_key)
Select
distinct
tbl.account_id,
tbl.theme_status,
streaming.streaming_hours,
tbl.year_month as year_month_key
From
(
Select
tbl_0.year_month,
tbl_0.account_id,
case when max(tbl_0.theme_status) = 1 then 'With Theme' else 'No Theme' end as theme_status
From
(Select
streaming.year_month,
streaming.account_id,
case when theme_events.account_id is not null then 1 else 0 end as theme_status
from
(
Select
substring(date_key, 1, 7) as year_month,
last_day(add_months(date_key, -1)) as year_month_ed,
date_key,
upper(account_id) as account_id,
play_seconds
from agg_device_streaming_metrics_daily
Where date_key between date_add(last_day(add_months(current_date, -2)),1) and last_day(add_months(current_date, -1))
and play_seconds > 0
) streaming
left join
(
Select
upper(theme.virtualuserid) as account_id,
min(theme.createddate) as min_createddate,
min(theme.date_key) as date_key
From
(
select * from theme_activate_event_history
where date_key between '2019-01-01' and '2020-01-01'
and activate = 'true' and themetype in ('ThemeBundle','ScreenSaver','Skin','Audio')
union
select * from theme_activate_event_history
where date_key between '2020-01-01' and '2021-01-01'
and activate = 'true' and themetype in ('ThemeBundle','ScreenSaver','Skin','Audio')
union
select * from theme_activate_event_history
where date_key between '2021-01-01' and '2022-01-01'
and activate = 'true' and themetype in ('ThemeBundle','ScreenSaver','Skin','Audio')
union
select * from theme_activate_event_history
where date_key between cast('2022-01-01' as date) and last_day(add_months(current_date, -1))
and activate = 'true' and themetype in ('ThemeBundle','ScreenSaver','Skin','Audio')
) theme
group by theme.virtualuserid
) theme_events
on streaming.account_id = theme_events.account_id
and date(theme_events.date_key) <= date(streaming.year_month_ed)
) tbl_0
group by tbl_0.year_month, tbl_0.account_id
) tbl
inner join
(Select
substring(date_key, 1, 7) as year_month,
upper(account_id) as account_id,
cast(sum(play_seconds) / 3600 as double) as streaming_hours
from agg_device_streaming_metrics_daily
Where date_key between date_add(last_day(add_months(current_date, -2)),1) and last_day(add_months(current_date, -1))
and play_seconds > 0
group by substring(date_key, 1, 7), upper(account_id)
) streaming
on tbl.account_id = streaming.account_id and tbl.year_month = streaming.year_month;
We are working on Oracle Database SQL Query.
DataBase Structure:
(etd_log_id, region,sla_status,escaltion_level,added_ts)
escalation_level field has:
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9
1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,
1,2,3,4,5,6,
1,2,3,4,5,
1,2,3,4,
1,2,3
1,2
1
Requirement: We need to send alert based on level as a input,
Lets say we get 6 as a input, we should get all these records :
1,2,3,4,5,6,
1,2,3,4,5,
1,2,3,4,
1,2,3
1,2
1
Java passes level as input in the where clause
`WHERE CAST(REGEXP_SUBSTR(ESCALATION_LEVEL, '[^,]*$') AS NUMBER) <= 10<--Input from Java`
Approach: We developed a SQL query initially wherein we had this
CAST(REGEXP_SUBSTR(ESCALATION_LEVEL, '[^,]*$') AS NUMBER) <= 6--<input from java>
But it threw an error as
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
Upon RCA we came to know that REGEXP is treating the value into ASCII values.
So we changed the condition by removing CAST as NUMBER from the CLAUSE.( WHERE (REGEXP_SUBSTR(ESCALATION_LEVEL, '[^,]*$') <= ‘10’)Now 10 is treated as a character and comparison is made on the basis of ascii values.
The code failed in testing .
I did RCA for the same and came to know that 1 and 10 have same ASCII value as ‘49’.
So here is the interesting part.
1 and 10 have ASCII value as 49.
2,3,4,5,6,7,8,9 have ASCII values as 50,51,52,53,…
When it reaches level 10, it shows output for escalation level 1 and 10 ( as we have WHERE (REGEXP_SUBSTR(ESCALATION_LEVEL, '[^,]*$') < =‘10’) because it satisfies the condition <=49(ascii value) for level 1 as well as 10.
Also, when it reaches 10, and then we provide any level between 2 and 9 we get level 10 also in the output.(obviously because now string has 10 in it with lower ASCII value to satisfy the condition)
What I want?
Is there any way we can convert regexp and the level to same format? ( I tried with TO_NUMBER and CAST as NUMBER)
Is there any other approach you would suggest to solve this issue?
Your help will be highly appreciated!
QUERY
SELECT CAST( REGEXP_SUBSTR(ESCALATION_LEVEL, '[^,]*$') AS VARCHAR2(200)) "LEVEL",
ETD_ALERT_LOG_ID ,REGION, REQUIRED_PERCENTAGE AS SLA_SET,
----------------- THIS PART FETCHES START TIME FOR EMAIL ALERT---------------------------------------------------
CAST(TO_CHAR(
(FROM_TZ((SELECT ADDED_TS FROM (SELECT ADDED_TS, DENSE_RANK() OVER (PARTITION BY REGION ORDER BY ADDED_TS DESC) RNK
FROM CHRT_SMTFOMGR.ETD_ALERT_LOG WHERE UPPER(REGION) = UPPER('Southern Ohio')
AND ESCALATION_LEVEL ='1' AND SLA_STATUS = 'Missed SLA'
AND ADDED_TS > (SELECT ADDED_TS FROM (SELECT ADDED_TS, DENSE_RANK() OVER (PARTITION BY REGION ORDER BY ADDED_TS DESC) RNK
FROM CHRT_SMTFOMGR.ETD_ALERT_LOG WHERE UPPER(REGION) = UPPER(EAL.REGION)
AND (SLA_STATUS = 'In SLA' OR IS_SEND_ALERT = 'N') )
WHERE RNK = 1 AND ROWNUM = 1)
AND TRUNC(FROM_TZ(ADDED_TS, (SELECT TZ_OFFSET(TO_CHAR(SYSTIMESTAMP,'TZR')) FROM DUAL ))
AT TIME ZONE (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION) = UPPER(EAL.REGION) AND ROWNUM=1))=
TRUNC(SYSTIMESTAMP AT TIME ZONE (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG
WHERE UPPER(REGION) = UPPER(EAL.REGION) AND ROWNUM=1)))
WHERE RNK = 1 AND ROWNUM = 1), (SELECT TZ_OFFSET(TO_CHAR(SYSTIMESTAMP,'TZR')) FROM DUAL ))
AT TIME ZONE (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION)= UPPER(EAL.REGION) AND ROWNUM=1))
- NUMTODSINTERVAL((SELECT VALUE FROM CHRT_SMTFOMGR.APP_PROPERTY
WHERE UPPER(NAME) = 'ETD_ALERT_ESCALATION_INTERVAL'),'MINUTE')
,'HH:MI AM') AS VARCHAR2(10)) ||' '|| (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION)= UPPER(EAL.REGION) AND ROWNUM=1) AS ALERT_START_TIME,
----------------------END OF START TIME **WORKS FINE**-------------------------
------------------FETCHES END TIME FOR EMAIL TEMPLATE-------------------------------------
CAST(TO_CHAR(
(FROM_TZ(ADDED_TS, (SELECT TZ_OFFSET(TO_CHAR(SYSTIMESTAMP,'TZR')) FROM DUAL ))
AT TIME ZONE (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION)= UPPER(EAL.REGION) AND ROWNUM=1))
,'HH:MI AM') AS VARCHAR2(10)) ||' '|| (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION)= UPPER(EAL.REGION) AND ROWNUM=1)
AS ALERT_END_TIME,
-------------------------END TIME **WORKS FINE----------------------------------------------------
TOTAL_ETD, (TOTAL_ETD - TOTAL_MET_SLA) AS UCM_SLA_MISSED_ETD, CURRENT_PERCENTAGE AS ACTUAL_SLA
FROM CHRT_SMTFOMGR.ETD_ALERT_LOG EAL
---------------------WHERE CLAUSE MENTIONED IN THE MAIL--------------------------------------------
WHERE CAST(REGEXP_SUBSTR(ESCALATION_LEVEL, '[^,]*$') AS NUMBER) <= 10
----------------------------------------------------------------------------------------------------
--------------PART TO FILTER REGION AND CHECK IF THE ALERT IS ON FOR SENDING EMAIL----
AND UPPER(EAL.REGION) = UPPER('Southern Ohio') AND IS_SEND_ALERT = 'Y'
-----------------PART TO CONVERT THE TIME ZONE INTO REGION BASED TIMEZONE SO THAT ALERT IS SENT ONLY DURING CONFIGURED START AND END TIME-------------------------------------------
AND TRUNC(FROM_TZ(ADDED_TS, (SELECT TZ_OFFSET(TO_CHAR(SYSTIMESTAMP,'TZR')) FROM DUAL ))
AT TIME ZONE (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION) = UPPER(EAL.REGION) AND ROWNUM=1))=
TRUNC(SYSTIMESTAMP AT TIME ZONE (SELECT TIMEZONE FROM CHRT_SMTFOMGR.ETD_ALERT_CONFIG EAC
WHERE UPPER(EAC.REGION) = UPPER(EAL.REGION) AND ROWNUM=1))
---PART TO CHECK IF THE TICKETS MEET SLA OR THE ALERT WAS DISABLED IN BETWEEN THE RECORDS THAT ARE TO BE FETCHED SHOULD BE AFTER THAT 'In SLA' RECORD-----------------------------------------------------------------------
AND ADDED_TS > (SELECT ADDED_TS FROM (SELECT ADDED_TS, DENSE_RANK() OVER (PARTITION BY REGION ORDER BY ADDED_TS DESC) RNK
FROM CHRT_SMTFOMGR.ETD_ALERT_LOG WHERE UPPER(REGION) = UPPER(EAL.REGION)
AND (SLA_STATUS = 'In SLA' OR IS_SEND_ALERT = 'N') )
WHERE RNK = 1 AND ROWNUM = 1)
ORDER BY ADDED_TS DESC;
You do not need (slow) regular expressions and can do it with simple string functions:
SELECT *
FROM table_name
WHERE TO_NUMBER(
RTRIM(
SUBSTR(escalation_level, INSTR(escalation_level, ',', -2) + 1),
','
)
) <= 6;
Which, for the sample data:
CREATE TABLE table_name ( escalation_level ) AS
SELECT '1,2,3,4,5,6,7,8,9,10 ' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,6,7,8,9,10' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,6,7,8,9,10' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,6,7,8,9' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,6,7,8,' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,6,7,' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,6,' FROM DUAL UNION ALL
SELECT '1,2,3,4,5,' FROM DUAL UNION ALL
SELECT '1,2,3,4,' FROM DUAL UNION ALL
SELECT '1,2,3' FROM DUAL UNION ALL
SELECT '1,2' FROM DUAL UNION ALL
SELECT '1' FROM DUAL
Outputs:
ESCALATION_LEVEL
1,2,3,4,5,6,
1,2,3,4,5,
1,2,3,4,
1,2,3
1,2
1
db<>fiddle here
Additionally:
(SELECT TZ_OFFSET(TO_CHAR(SYSTIMESTAMP,'TZR')) FROM DUAL)
Does not need the sub-query and you can just use:
TZ_OFFSET(TO_CHAR(SYSTIMESTAMP,'TZR'))
and
SELECT ADDED_TS FROM (
SELECT ADDED_TS,
DENSE_RANK() OVER (PARTITION BY REGION
ORDER BY ADDED_TS DESC) RNK
FROM CHRT_SMTFOMGR.ETD_ALERT_LOG
WHERE UPPER(REGION) = UPPER(EAL.REGION)
AND (SLA_STATUS = 'In SLA' OR IS_SEND_ALERT = 'N')
)
WHERE RNK = 1 AND ROWNUM = 1
When there are two REGION values that have identical characters but differing cases then the inner query will return multiple rows where rnk = 1 and it may be random which is matched by ROWNUM = 1 and it may not be the one with the latest date.
db<>fiddle here
You can write it much simpler using ROW_NUMBER and without the PARTITION BY clause (since that is already handled in the WHERE filter and I am assuming case does not matter):
SELECT ADDED_TS FROM (
SELECT ADDED_TS,
ROW_NUMBER() OVER (ORDER BY ADDED_TS DESC) RN
FROM CHRT_SMTFOMGR.ETD_ALERT_LOG
WHERE UPPER(REGION) = UPPER(EAL.REGION)
AND (SLA_STATUS = 'In SLA' OR IS_SEND_ALERT = 'N')
)
WHERE RN = 1
However, you can probably write it without all the nested sub-queries if you use conditional aggregation in a windowed analytic function but your query is huge, badly-formatted and difficult to understand and I'll leave that to you to solve if you want to.
I have a subquery which is used for an Oracle database, but I want to use an equivalent query for a SQL Server database.
I didn't figure out how to migrate the TO_TIMESTAMP(TO_CHAR(TO_DATE part and also didn't know how to handle the thing with rownums in T-SQL.
Is it even possible to migrate this query?
SELECT 0 run_id,
0 tran_id,
0 sort_id,
' ' tran_type,
10 prod_id,
72 type_id,
1 value,
TO_TIMESTAMP(TO_CHAR(TO_DATE('2016-03-18 00:00:00', 'YYYY.MM.DD HH24:MI:SS') + rownum -1, 'YYYY.MM.DD') || to_char(sw.end_time, 'HH24:MI:SS'), 'YYYY.MM.DD HH24:MI:SS') event_publication,
EXTRACT (YEAR
FROM (TO_DATE('2016-03-18 00:00:00', 'YYYY.MM.DD HH24:MI:SS') + rownum -1)) y,
EXTRACT (MONTH
FROM (TO_DATE('2016-03-18 00:00:00', 'YYYY.MM.DD HH24:MI:SS') + rownum -1)) mo,
EXTRACT (DAY
FROM (TO_DATE('2016-03-18 00:00:00', 'YYYY.MM.DD HH24:MI:SS') + rownum -1)) d,
to_number(to_char (sw.end_time, 'HH24')) h,
to_number(to_char (sw.end_time, 'MI')) mi,
to_number(to_char (sw.end_time, 'SS')) s,
0 ms
FROM all_objects ao,
settlement_win sw,
prod_def pd
WHERE pd.prod_id = 10
AND sw.country = pd.country
AND sw.commodity = pd.commodity
AND rownum <= TO_DATE('2016-03-18 23:59:00', 'YYYY.MM.DD HH24:MI:SS') -TO_DATE('2016-03-18 00:00:00', 'YYYY.MM.DD HH24:MI:SS')+1
The first thing to address is the use of rownum which has no direct equivalent in TSQL but we can mimic it, and for this particular query you need to recognize that the table ALL_OBJECTS is only being used to produce a number of rows. It has no other purpose to the query.
In TSQL we can generate rows using a CTE and there are many many variants of this, but for here I suggest:
;WITH
cteDigits AS (
SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
)
, cteTally AS (
SELECT
d1s.digit
+ d10s.digit * 10
+ d100s.digit * 100 /* add more like this as needed */
-- + d1000s.digit * 1000 /* add more like this as needed */
+ 1 AS rownum
FROM cteDigits d1s
CROSS JOIN cteDigits d10s
CROSS JOIN cteDigits d100s /* add more like this as needed */
--CROSS JOIN cteDigits d1000s /* add more like this as needed */
)
This will quickly spin-up 1000 rows as is and can be extended to produce many more rows by adding more cross joins. Note this returns a column called rownum which starts at 1 thus mimicking the Oracle rownum.
So next you can just add some of the remaining query, like this:
SELECT
0 run_id
, 0 tran_id
, 0 sort_id
, ' ' tran_type
, 10 prod_id
, 72 type_id
, 1 value
, convert(varchar, dateadd(day, rownum - 1,'20160318'),121) event_publication
-- several missing rows here
, 0 ms
FOM cteTally
INNER JOIN settlement_win sw
INNER JOIN prod_def pd ON sw.country = pd.country AND sw.commodity = pd.commodity
WHERE pd.prod_id = 10
AND rownum <= datediff(day,'20160318','20160318') + 1
Note that you really do not need a to_timestamp() equivalent you just need the ability to output date and time to the maximum precision of your data which appears to be to the level of seconds.
To progress further (I think) requires an understanding of the data held in the column sw.end_time. If this can be converted to the mssql datetime data type then it is just a matter of adding a number of days to that value to arrive at the event_publication and similarly if sw.end_time is converted to a datetime data type then use date_part() to get the hours, minutes and seconds from that column. e.g.
, DATEADD(day,rownum-1,CONVERT(datetime, sw.end_time)) AS event_publication
also, if such a calculation works then it would be possible to use an apply operator to simplify the overall query, something like this
;WITH
cteDigits AS (
SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
)
, cteTally AS (
SELECT
d1s.digit
+ d10s.digit * 10
+ d100s.digit * 100 /* add more like this as needed */
-- + d1000s.digit * 1000 /* add more like this as needed */
+ 1 AS rownum
FROM cteDigits d1s
CROSS JOIN cteDigits d10s
CROSS JOIN cteDigits d100s /* add more like this as needed */
--CROSS JOIN cteDigits d1000s /* add more like this as needed */
)
SELECT
0 run_id
, 0 tran_id
, 0 sort_id
, ' ' tran_type
, 10 prod_id
, 72 type_id
, 1 value
, convert(varchar(23), CA.Event_publication, 121) Event_publication
, datepart(day,CA.Event_publication) dd
, datepart(month,CA.Event_publication) mm
, datepart(year,CA.Event_publication) yyyy
, datepart(hour,CA.Event_publication) hh24
, datepart(minute,CA.Event_publication) mi
, datepart(second,CA.Event_publication) ss
, 0 ms
FOM cteTally
INNER JOIN settlement_win sw
INNER JOIN prod_def pd ON sw.country = pd.country AND sw.commodity = pd.commodity
CROSS APPLY (
SELECT DATEADD(day,rownum-1,CONVERT(datetime, sw.end_time)) AS event_publication ) CA
WHERE pd.prod_id = 10
AND rownum <= datediff(day,'20160318','20160318') + 1
NB: IT may be necessary to include this datediff(day,'19000101,'20160318') (which equals 42445) into the calculation of the event_date e.g.
SELECT DATEADD(day,42445 + (rownum-1),CONVERT(datetime, sw.end_time)) AS event_publication
One last point is that you could use datetime2 instead of datetime if you really do need a greater degree of time precision but there is no easily apparent requirement for that.
I need to select the last 12 months. As you can see on the picture, May occurs two times.
But I only want it to occur once. And it needs to be the newest one.
Plus, the table should stay in this structure, with the latest month on the bottom.
And this is the query:
SELECT Monat2,
Monat,
CASE WHEN NPLAY_IND = '4P'
THEN 'QuadruplePlay'
WHEN NPLAY_IND = '3P'
THEN 'TriplePlay'
WHEN NPLAY_IND = '2P'
THEN 'DoublePlay'
WHEN NPLAY_IND = '1P'
THEN 'SinglePlay'
END AS Series,
Anzahl as Cnt
FROM T_Play_n
where NPLAY_IND != '0P'
order by Series asc ,Monat
This is the new query
SELECT sub.Monat2,sub.Monat,
CASE WHEN NPLAY_IND = '4P'
THEN 'QuadruplePlay'
WHEN NPLAY_IND = '3P'
THEN 'TriplePlay'
WHEN NPLAY_IND = '2P'
THEN 'DoublePlay'
WHEN NPLAY_IND = '1P'
THEN 'SinglePlay'
END
AS Series, Anzahl as Cnt FROM (SELECT ROW_NUMBER () OVER (PARTITION BY Monat2 ORDER BY Monat DESC)rn,
Monat2,
Monat,
Anzahl,
NPLAY_IND
FROM T_Play_n)sub
where sub.rn = 1
It does only show the months once but it doesn't do that for every Series.
So with every Play it should have 12 months.
In Oracle and SQL-Server you can use ROW_NUMBER.
name = month name and num = month number:
SELECT sub.name, sub.num
FROM (SELECT ROW_NUMBER () OVER (PARTITION BY name ORDER BY num DESC) rn,
name,
num
FROM tab) sub
WHERE sub.rn = 1
ORDER BY num DESC;
WITH R(N) AS
(
SELECT 0
UNION ALL
SELECT N+1
FROM R
WHERE N < 12
)
SELECT LEFT(DATENAME(MONTH,DATEADD(MONTH,-N,GETDATE())),3) AS [month]
FROM R
The With R(N) is a Common Table Expression.The R is the name of the result set (or table) that you are generating. And the N is the month number.
In SQL Server you can do It in following:
SELECT DateMonth, DateWithMonth -- Specify columns to select
FROM Tbl -- Source table
WHERE CAST(CAST(DateWithMonth AS INT) * 100 + 1 AS VARCHAR(20)) >= DATEADD(MONTH, -12,GETDATE()) -- Condition to return data for last 12 months
GROUP BY DateMonth, DateWithMonth -- Uniqueness
ORDER BY DateWithMonth -- Sorting to get latest records on the bottom
So it sounds like you want to select rows that contain the last occurrence of months. Something like this should work:
select * from [table_name]
where id in (select max(id) from [table_name] group by [month_column])
The last select in the brackets will get a list of id's for the last occurrence of each month. If the year+month column you have shown is not in descending order already, you might want to max this column instead.
You can use something like this(the table dbo.Nums contains int values from 0 to 11)
SELECT DATEADD(MONTH, DATEDIFF(MONTH, '19991201', CURRENT_TIMESTAMP) + n - 12, '19991201'),
DATENAME(MONTH,DateAdd(Month, DATEDIFF(month, '19991201', CURRENT_TIMESTAMP) + n - 12, '19991201'))
FROM dbo.Nums
I suggest to use a group by for the month name, and a max function for the numeric component. If is not numeric, use to_number().
I'm trying to run this query in Oracle 8i but it's not working!
SELECT DECODE(seqnum, 1, t.ID1,cnt,'0') PI_VALUE1,
DECODE(seqnum, 1, t.STARTTIME,cnt,t.ENDTIME) timestamp,
'090.'
|| t2.APP
|| '.BATCH' tagname
FROM
(SELECT t.*,
row_number() over(partition BY t.ID1, t.PLANT_UNIT order by t.STARTTIME) AS seqnum,
COUNT(*) over(partition BY t.ID1, t.PLANT_UNIT) cnt
FROM tb_steps t
) t
INNER JOIN tb_equipments t2
ON t2.plant_unit = t.plant_unit
WHERE (seqnum = 1
OR SEQNUM = CNT)
AND (T.STARTTIME > '15-jul-2013'
AND t.ENDTIME < '15-aug-2013') ;
I've already made a lot of changes [like changing case when for decode] but it's still not OK...
Can someone help me write the query to be supported by Oracle 8i?
PS.: I know this version is not supported by Oracle for AGES but I'm only querying data for my .NET application so I can't upgrade/touch the DB.
Version is 8.1.7 and the specific error:
ORA-00933: SQL command not properly ended.
Many thanks,
ANSI joins were not introduced until Oracle Database 9iR1 (9.0.1). They are not supported in 8.1.7.
Try re-writing the query without an ANSI style join.
Something like this may work:
SELECT DECODE(seqnum, 1, t.ID1,cnt,'0') PI_VALUE1,
DECODE(seqnum, 1, t.STARTTIME,cnt,t.ENDTIME) timestamp,
'090.'
|| t2.APP
|| '.BATCH' tagname
FROM
(SELECT t.*,
row_number() over(partition BY t.ID1, t.PLANT_UNIT order by t.STARTTIME) AS seqnum,
COUNT(*) over(partition BY t.ID1, t.PLANT_UNIT) cnt
FROM tb_steps t
) t, tb_equipments t2
WHERE t2.plant_unit = t.plant_unit
AND (t.seqnum = 1
or t.seqnum = t.cnt)
AND (T.STARTTIME > '15-jul-2013'
AND t.ENDTIME < '15-aug-2013') ;
Totally untested....
Hope that helps.