query condition - Oracle SQL - sql

I wrote code only on transact sql . I want to write this query in oracle sql. But it doesn't work . the main problem that I can't write my select parameters in the where condition . How to avoid this problem . My query ,
select t.*,
count(*) over(partition by t.el1_code) cnt ,
count(*) over(partition by t.el1_code, t.el1_valid) cnt1 ,
concat(t.el1_name,t.el1_sname) str,
max(concat(t.el1_name,t.el1_sname)) over(partition by t.el1_code) str_check
from tasuser.coda_element_1 t
)tt
—where tt.cnt>1 and (tt.el1_valid=0 or (tt.el1_valid=1 and cnt=cnt1 and
str<>str_check) )
where not (tt.cnt>1 and (tt.el1_valid=0 or (tt.el1_valid=1 and
cnt=cnt1 and
str<>str_check)))

You need to use derived table/inline view:
SELECT *
FROM (
select t.*,
count(*) over(partition by t.el1_code) cnt ,
count(*) over(partition by t.el1_code, t.el1_valid) cnt1 ,
concat(t.el1_name,t.el1_sname) str,
max(concat(t.el1_name,t.el1_sname)) over(partition by t.el1_code) str_check
from tasuser.coda_element_1 t
) tt
WHERE tt.conditions....
or common table expression:
WITH cte AS (
select t.*,
count(*) over(partition by t.el1_code) cnt ,
count(*) over(partition by t.el1_code, t.el1_valid) cnt1 ,
concat(t.el1_name,t.el1_sname) str,
max(concat(t.el1_name,t.el1_sname)) over(partition by t.el1_code) str_check
from tasuser.coda_element_1 t
)
SELECT *
FROM cte
WHERE conditions ...

Related

error SQL query Invalid call to dataType on unresolved object, tree: '

This is a close example of a problem.
I have a similar problem where I have to do parent first and then child join but before I add ROW_NUMBER it returns an error, I know it for sure. But the dataType problem got me thinking
I can only do SELECT and I'm using pyspark. There is also a community-written library that runs this code
Invalid call to dataType on unresolved object, tree: '
WITH cte1 AS (
SELECT
ROW_NUMBER() OVER (ORDER BY UPPER(name)) AS id,
0 as parent_id ,
name,
"type1" as type
FROM sourcetable1
),
WITH cte2 AS (
SELECT
ROW_NUMBER() OVER (ORDER BY UPPER(name))+(SELECT max(id) FROM cte1) AS id,
cte1.id as parent_id
name,
"type1" as type
FROM sourcetable2
INNER JOIN cte1 ON cte1.type = sourcetable2.type and cte1.name = . sourcetable2.name
),
WITH cte3 AS (
SELECT
ROW_NUMBER() OVER (ORDER BY UPPER(name))+(SELECT max(id) FROM cte2) AS id,
0 as parent_id ,
name,
"type2" as type
FROM sourcetable3
),
WITH cte4 AS (
SELECT
ROW_NUMBER() OVER (ORDER BY UPPER(name))+(SELECT max(id) FROM cte3) AS id,
cte3.id as parent_id
name,
"type2" as type
FROM sourcetable4
INNER JOIN cte3 ON cte3.type = sourcetable4.type and cte3.name = . sourcetable4.name
)
SELECT * FROM cte1
UNION ALL
SELECT * FROM cte2
UNION ALL
SELECT * FROM cte3
UNION ALL
SELECT * FROM cte4
Please help me!
I'm sorry if I explained it wrong :(
I tried different joining methods but they didn't like them

SQL command for getting a specific pattern

I have a table sample with column 'observations':
Please help with the SQL command to get the following 'cumulative multiplication' output:
2
6
30
300
One method is a recursive CTE:
with tt as (
select t.*, row_number() over (order by obs) as seqnum
from t
),
cte as (
select obs as prod, seqnum
from tt
where seqnum = 1
union all
select cte.prod * tt.obs, tt.seqnum
from cte join
tt
on tt.seqnum = cte.seqnum + 1
)
select *
from cte;
Another uses arithmetic to implement a "product" window function:
select t.*,
exp(sum(log(obs)) over (order by obs))
from t;
Here is a db<>fiddle.

How to get MAX rownumber with rowNumber in same query

I have ROW_NUMBER() OVER (ORDER BY NULL) rnum in a sql statement to give me row numbers. is there a way to attach the max rnum to the same dataset going out?
what I want is the row_number() which I get, but I also want the MAXIMUM rownumber of the total return on each record.
SELECT
ROW_NUMBER() OVER (ORDER BY NULL) rnum,
C.ID, C.FIELD1, C."NAME", C.FIELD2, C.FIELD3
FROM SCHEMA.TABLE
WHERE (C.IS_CRNT = 1)
), MAX_NUM as (
SELECT DATA.ID, max(rnum) as maxrnum from DATA GROUP BY DATA.COMPONENT_ID
) select maxrnum, DATA.* from DATA JOIN MAX_NUM on DATA.COMPONENT_ID = MAX_NUM.COMPONENT_ID
DESIRED RESULT (ASSUMING 15 records):
1 15 DATA
2 15 DATA
3 15 DATA
etc...
I think you want count(*) as a window function:
SELECT ROW_NUMBER() OVER (ORDER BY NULL) as rnum,
COUNT(*) OVER () as cnt,
C.ID, C.FIELD1, C."NAME", C.FIELD2, C.FIELD3
FROM SCHEMA.TABLE
WHERE C.IS_CRNT = 1
Based on my assumptions in your dataset, this is the approach I would take:
WITH CTE AS (
select C.ID, C.FIELD1, C."NAME", C.FIELD2, C.FIELD3, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID)
FROM SCHEMA.TABLE WHERE (C.IS_CRNT = 1))
SELECT *, (select count(*) from cte) "count" from cte;

Trouble using ROW_NUMBER() OVER (PARTITION BY

I have this query :
SELECT
Reservation.*,
ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY (SELECT NULL)) AS RowNums
FROM
Reservation
WHERE
RowNums = 1
I get this error :
Msg 207, Level 16, State 1, Line 2
Invalid column name 'RowNums'.
I don't know where the problem is - any help?
You'd need to use a CTE:
WITH tempData AS
(
SELECT Reservation.*,
ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY (SELECT NULL)) AS RowNums
FROM Reservation
)
SELECT * FROM tempData WHERE RowNums = 1
However, a cleaner approach would be to use WITH TIES:
SELECT TOP 1 WITH TIES *
FROM Reservation
ORDER BY ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY (SELECT NULL))
You can't use RowNums directly,
Try like this,
SELECT *
FROM
(
SELECT Reservation.*,ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY (SELECT NULL)) AS RowNums
FROM Reservation
) S
where RowNums = 1
SELECT t.*
FROM
(SELECT Reservation.*,ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY NULL ) AS RowNums
FROM Reservation) t
where RowNums = 1
You cannot use this directly as this is not a column in the table. Either use CTE or derived table.
CTE
:WITH C AS (
SELECT Reservation.*,ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY (SELECT NULL)) AS RowNums
)
SELECT * FROM C WHERE RowNums = 1
Try this
;WITH CTE AS(
SELECT Reservation.*,
ROW_NUMBER() OVER (PARTITION BY code_bien ORDER BY (SELECT NULL)) AS RowNums
FROM Reservation)
SELECT * FROM CTE
WHERE RowNums = 1

How do I create a temp table using this snippet?

how do i produce this SQL snippet as a temp table so I can join some other stuff into it?
with MyCTE AS
(
select *, RANK() OVER (PARTITION BY workplace ORDER BY Total DESC) AS Rank
from [dbo].[OriginDestination]
)
select * from MyCTE where Rank <= 5
Like this:
with MyCTE AS
(
select *, RANK() OVER (PARTITION BY workplace ORDER BY Total DESC) AS Rank
from [dbo].[OriginDestination]
)
select *
into #yourTempTable
from MyCTE
where Rank <= 5