Using SQLite, I have a suspected case of recursive data that has cycle - in other words, a child node is also its own grandparent. The symptom, of course, is an infinite loop
I know how Oracle and Postgresql handle cyclic data; but have not found any method to do so with SQLite. Below is an example of data with a cycle. If you remove the last "row" in the dataset table, it will work. As written, it goes into an infinite loop.
with DataSet as
(
select 'A' as node, null as parent union all
select 'B' as node, 'A' as parent union all
select 'C' as node, 'B' as parent union all
select 'D' as node, 'C' as parent union all
select 'A' as node, 'D' as parent
),
Hierarchy( node, parent, level, path )
as
(
select DataSet.node,
DataSet.parent,
1 as level,
'/' || DataSet.node as path
from DataSet
where DataSet.parent is null
union all
select DataSet.node,
DataSet.parent,
Hierarchy.level + 1 as level,
Hierarchy.path || '/' || DataSet.node as path
from Hierarchy
join DataSet
on DataSet.parent = Hierarchy.node
)
select *
from Hierarchy
order by path
;
Without the level, you could use UNION to ignore the duplicates.
Otherwise, there is no easy way to compare 'old' rows.
You could add a sufficiently large LIMIT to prevent the loop from becoming infinite, but then it is not always possible to remove duplicates.
To close the loop (pun intended) on this, I finally figured out a method that works. Plus I even added a column to show the rows where a cyclic condition exists. This solution includes: level (depth) number, parent, child, path (root to node/leaf), and a zero or one for detection of a cycle. You can paste the SQL below into the sqlite3 prompt and it will display:
level parent node path cyclic_flag
------ ------- ------- ------------ ------------
0 A B /A/B 0
1 B C /A/B/C 0
2 C D /A/B/C/D 0
3 D A /A/B/C/D/A 1
sqlite>
Finally, here is the SQL that shows the method. The only tricky part is the use of counting substrings in the path (the next node being the substring counted).
with dataset as
(
select 'A' as parent, 'B' as node union all
select 'B' as parent, 'C' as node union all
select 'C' as parent, 'D' as node
union all select 'D' as parent, 'A' as node
),
hierarchy( level, parent, node, path, cyclic_flag )
as
(
select 0 as level,
dataset.parent,
dataset.node,
'/' || dataset.parent || '/' || dataset.node as path,
0 as cyclic_flag
from dataset
where dataset.parent = 'A'
union all
select
hierarchy.level + 1 as level,
dataset.parent,
dataset.node,
hierarchy.path || '/' || dataset.node as path,
case
when
(length(path||dataset.node) - length(replace(path|| dataset.node,dataset.node,'')))/length(dataset.node) = 1
then 0
else 1
end as cyclic_flag
from hierarchy
inner join dataset
on dataset.parent = hierarchy.node
where (length(path) - length(replace(path,hierarchy.node,'')))/length(hierarchy.node) < 2
)
select *
from hierarchy
order by path
;
Related
I need to output running balance in the following query, Please help me with the query-code:
select
WEAVING_YARN_TR.TR_ID,
WEAVING_YARN_TR.GP_NO,
WEAVING_YARN_TR.YR_ID,
WEAVING_YARN_GATEPASS.TO_FROM as PARTY,
WEAVING_YARN_GATEPASS.GP_DATE,
WEAVING_YARN_TR.BAGS_IN,
WEAVING_YARN_TR.BAGS_OUT,
SUM(NVL(WEAVING_YARN_TR.BAGS_OUT,0)-NVL(WEAVING_YARN_TR.BAGS_IN,0)) over (order by GP_DATE) as BALANCE
from WEAVING_YARN_TR,WEAVING_YARN_GATEPASS
where WEAVING_YARN_TR.GP_NO=WEAVING_YARN_GATEPASS."GP_NO." and WEAVING_YARN_TR.YR_ID=:P216_YR_ID and WEAVING_YARN_GATEPASS.TO_FROM=:P216_YR_DESC
UNION ALL
select
null as TR_ID,
WEAVING_WARP_SET.SET_ID,
WEAVING_WARP_SET.YARN_ITEM,
WEAVING_WARP_SET.PARTY,
WEAVING_WARP_SET.RECEIVED_DATE,
WEAVING_WARP_SET.TOTAL_BAGS_CONSUMED,
null as BAGS_OUT,
NVL(null,0)-NVL(WEAVING_WARP_SET.TOTAL_BAGS_CONSUMED,0) as BALANCE
from WEAVING_WARP_SET
where WEAVING_WARP_SET.YARN_ITEM=:P216_YR_ID and WEAVING_WARP_SET.PARTY= :P216_YR_DESC
order by GP_DATE
So here is the thing; if you're going to use UNION or UNION ALL, each query block has to be the equal column and type.
For example; We have 2 queries and we want to bind them.
The first query block contains 3 columns and their type NUMBER, VARCHAR, NUMBER (in order). The second block has to be 3 column and types must be NUMBER, VARCHAR, VARCHAR (in order)
So your query does not seem like that. You want an output like the screenshot that you sent. Here is the query that edited:
select
weaving_yarn_gatepass.gp_date
,weaving_yarn_tr.gp_no
,weaving_yarn_gatepass.to_from as party
,weaving_yarn_tr.yr_id
,weaving_yarn_tr.bags_in
,weaving_yarn_tr.bags_out
,sum(nvl(weaving_yarn_tr.bags_out,0) - nvl(weaving_yarn_tr.bags_in,0)) over(order by gp_date) as balance
from weaving_yarn_tr
,weaving_yarn_gatepass
where weaving_yarn_tr.gp_no = WEAVING_YARN_GATEPASS."GP_NO."
and weaving_yarn_tr.yr_id = :p216_yr_id
and weaving_yarn_gatepass.to_from = :p216_yr_desc
union all
select
weaving_warp_set.received_date --I don't know if it means gp_date
,null as gp_no
,weaving_warp_set.party
,null yr_id -- If there is a column named YR_ID in WEAVING_WARP_SET put here
,weaving_warp_set.total_bags_consumed
,null as bags_out
,nvl(null,0) - nvl(weaving_warp_set.total_bags_consumed,0) as balance
from weaving_warp_set
where weaving_warp_set.yarn_item = :p216_yr_id
and weaving_warp_set.party = :p216_yr_desc
order by gp_date;
I'm performing an Sqlite3 query similar to
SELECT * FROM nodes WHERE name IN ('name1', 'name2', 'name3', ...) LIMIT 1
Am I guaranteed that it will search for name1 first, name2 second, etc? Such that by limiting my output to 1 I know that I found the first hit according to my ordering of items in the IN clause?
Update: with some testing it seems to always return the first hit in the index regardless of the IN order. It's using the order of the index on name. Is there some way to enforce the search order?
The order of the returned rows is not guaranteed to match the order of the items inside the parenthesis after IN.
What you can do is use ORDER BY in your statement with the use of the function INSTR():
SELECT * FROM nodes
WHERE name IN ('name1', 'name2', 'name3')
ORDER BY INSTR(',name1,name2,name3,', ',' || name || ',')
LIMIT 1
This code uses the same list from the IN clause as a string, where the items are in the same order, concatenated and separated by commas, assuming that the items do not contain commas.
This way the results are ordered by their position in the list and then LIMIT 1 will return the 1st of them which is closer to the start of the list.
Another way to achieve the same results is by using a CTE which returns the list along with an Id which serves as the desired ordering of the results, which will be joined to the table:
WITH list(id, item) AS (
SELECT 1, 'name1' UNION ALL
SELECT 2, 'name2' UNION ALL
SELECT 3, 'name3'
)
SELECT n.*
FROM nodes n INNER JOIN list l
ON l.item = n.name
ORDER BY l.id
LIMIT 1
Or:
WITH list(id, item) AS (
SELECT * FROM (VALUES
(1, 'name1'), (2, 'name2'), (3, 'name3')
)
)
SELECT n.*
FROM nodes n INNER JOIN list l
ON l.item = n.name
ORDER BY l.id
LIMIT 1
This way you don't have to repeat the list twice.
I want to sort semicolon separated values per row in a column. Eg.
Input:
abc;pqr;def;mno
xyz;pqr;abc
abc
xyz;jkl
Output:
abc;def;mno;pqr
abc;pqr;xyz
abc
jkl;xyz
Can anyone help?
Perhaps something like this. Breaking it down:
First we need to break up the strings into their component tokens, and then reassemble them, using LISTAGG(), while ordering them alphabetically.
There are many ways to break up a symbol-separated string. Here I demonstrate the use of a hierarchical query. It requires that the input strings be uniquely distinguished from each other. Since the exact same semicolon-separated string may appear more than once, and since there is no info from the OP about any other unique column in the table, I create a unique identifier (using ROW_NUMBER()) in the most deeply nested subquery. Then I run the hierarchical query to break up the inputs and then reassemble them in the outermost SELECT.
with
test_data as (
select 'abc;pqr;def;mno' as str from dual union all
select 'xyz;pqr;abc' from dual union all
select 'abc' from dual union all
select 'xyz;jkl' from dual
)
-- End of test data (not part of the solution!)
-- SQL query begins BELOW THIS LINE.
select str,
listagg(token, ';') within group (order by token) as sorted_str
from (
select rn, str,
regexp_substr(str, '([^;]*)(;|$)', 1, level, null, 1) as token
from (
select str, row_number() over (order by null) as rn
from test_data
)
connect by level <= length(str) - length(replace(str, ';')) + 1
and prior rn = rn
and prior sys_guid() is not null
)
group by rn, str
;
STR SORTED_STR
--------------- ---------------
abc;pqr;def;mno abc;def;mno;pqr
xyz;pqr;abc abc;pqr;xyz
abc abc
xyz;jkl jkl;xyz
4 rows selected.
I am new in oracle and I want to get the value from a column which is stored as "Ashu||123 ||Main Menu|ENG||1|1".
as you can see each value is separated by || symbol.in the above value Ashu is the customer name and 123 is the id, I want both value as customer-name and customer id.
In the query below, I include some test data "on the fly" (not part of the solution; use your actual table name instead of test_data in the main query, and your actual column name instead of str). I included several special cases for testing, to make sure the query works correctly in all cases. I assume the first value (before the first ||) is the customer name and the second the customer id, and the rest of the input string can be ignored. I looked in particular to see that the query handles null values correctly (assuming they may happen in your data).
I left the customer id as a string; if it must be a number, it may be better to wrap it all within to_number().
with
test_data ( str ) as (
select 'Ashu||123||Main Menu|ENG||1|1' from dual union all
select 'Misha||125' from dual union all
select 'Babu||||Main Menu|NZL||?' from dual union all
select 'Rim||' from dual union all
select 'Todd' from dual union all
select '||139||Other Stuff' from dual
)
-- end of test data (only for testing and illustration) - not part of solution
-- SQL query begins BELOW THIS LINE
select str,
regexp_substr(str, '([^|]*)(\|\||$)', 1, 1, null, 1) as cust_name,
regexp_substr(str, '([^|]*)(\|\||$)', 1, 2, null, 1) as cust_id
from test_data
;
STR CUST_NAME CUST_ID
----------------------------- --------- -------
Ashu||123||Main Menu|ENG||1|1 Ashu 123
Misha||125 Misha 125
Babu||||Main Menu|NZL||? Babu
Rim|| Rim
Todd Todd
||139||Other Stuff 139
6 rows selected.
I need to manage hierarchy data storing in my database. But I have a problem now. Please see my example
I have a table called COMMON.TASK_REL
My second table is called Common. task
I suppose need to sort the task_seq and return a result like below:
Task Name || Task_Seq
Item1 1
..Item1.2 1
...Item1.2.1 1
..Item1.1 2
Here is my query
--Common task SQL modify --
WITH ctLevel
AS
(
SELECT
C_TASK_ID AS Child
,P_Task_ID AS Parent
,common_task.TASK_SEQ AS taskOrder
,1 AS [Level]
,CAST(C_TASK_ID AS VARCHAR(MAX)) AS [Order]
,CAST (Replicate('.', 1) + common_task.TASK_NAME AS VARCHAR(25)) AS [Task_Name]
FROM
[COMMON.TASK_REL] as common_task_rel,
[COMMON.TASK] as common_task
WHERE common_task_rel.C_TASK_ID = common_task.TASK_ID
and common_task.[TASK_TYPE] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT'
AND common_task.[STATUS] <> 'D'
UNION ALL
SELECT
C_TASK_ID AS Child
,P_Task_ID AS Parent
,common_task.TASK_SEQ AS taskOrder
,[Level] + 1 AS [Level]
,[Order] + '.' + CAST(C_TASK_ID AS VARCHAR(MAX)) AS [Order]
,CAST (Replicate('.', [Level] + 1) + common_task.TASK_NAME AS VARCHAR(25)) AS [Task_Name]
FROM [COMMON.TASK_REL] as common_task_rel
INNER JOIN ctLevel
ON ( P_Task_ID = Child ) , [COMMON.TASK] as common_task
WHERE common_task_rel.C_TASK_ID = common_task.TASK_ID
and common_task.[TASK_TYPE] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT'
AND common_task.[STATUS] <> 'D'
)
-- Viewing Data
SELECT Child ,Parent ,taskOrder,Level,[Order],Task_Name
FROM ctLevel
GROUP BY Child ,Parent ,taskOrder,Level,[Order],Task_Name
order by [Order];
GO
But my result returns duplicated rows:
Anyone can help me correct my query? Thanks
I believe that your duplicates are coming from your root/anchor query. You should add the following to that query:
AND Task_Seq = 0
Basically, you only want the root to be set up as the beginning of the tree. 301|300 should not be picked up until the recursion section (the part after union all)
If that does not make sense, then I can repaste your query with the modification, but that seemed unnecessary for a small change.