How to union multiple times same SQL - sql

I have query like this
select 1,2,3 from dual
union all
select 1,2,3 from dual
When I need to add new row, i put another union all, and that's ok. But problem appear when I need several union, for example 20. It is really annoying and not efficient to make another 17 unions. Is there a way (some procedure, function whatever) to make it faster and more elegant?

No problem, easy-peasy.
SQL> select 1, 2, 3
2 from dual
3 connect by level <= 10;
1 2 3
---------- ---------- ----------
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
10 rows selected.
SQL>

Sometimes it's easier to use json_table in such cases:
select *
from json_table(
'{data:[
[1,2,3,"abc"],
[2,3,4,"def"],
[3,4,5,"xyz"],
]
}'
,'$.data[*]'
columns
a number path '$[0]',
b number path '$[1]',
c number path '$[2]',
d varchar2(30) path '$[3]'
);
Results:
A B C D
---------- ---------- ---------- ------------------------------
1 2 3 abc
2 3 4 def
3 4 5 xyz

A variation on Littlefoot's answer:
select 1, 2, 3
from xmltable('1 to 20');

Related

Generate a serial number based on quantity column in sql

Hi Experts I have a table like this
T1
Order_no
Qty
1
3
2
5
3
1
4
3
I need to generate a column 'serial no' having values based on 'qty'
Output needed
OrderNo
Qty
SerailNo
1
3
1
1
3
2
1
3
3
2
5
1
2
5
2
2
5
3
2
5
4
2
5
5
3
1
1
4
3
1
4
3
2
4
3
3
Any suggestions?
Thanks in advance!!
You don't mention the specific database so I'll assume you are using PostgreSQL, aren't you?
You can use a Recursive CTE to expand the rows. For example:
with recursive
n as (
select order_no, qty, 1 as serial_no from t1
union all
select order_no, qty, serial_no + 1
from n
where serial_no < qty
)
select * from n order by order_no, serial_no
Result:
order_no qty serial_no
--------- ---- ---------
1 3 1
1 3 2
1 3 3
2 5 1
2 5 2
2 5 3
2 5 4
2 5 5
3 1 1
4 3 1
4 3 2
4 3 3
See running example at DB Fiddle.
EDIT FOR ORACLE
If you are using Oracle the query changes a bit to:
with
n (order_no, qty, serial_no) as (
select order_no, qty, 1 from t1
union all
select order_no, qty, serial_no + 1
from n
where serial_no < qty
)
select * from n order by order_no, serial_no
Result:
ORDER_NO QTY SERIAL_NO
--------- ---- ---------
1 3 1
1 3 2
1 3 3
2 5 1
2 5 2
2 5 3
2 5 4
2 5 5
3 1 1
4 3 1
4 3 2
4 3 3
See running example at db<>fiddle.
You should first provide the database you're using. Whether it's oracle, Sql Server, PostGreSQL will determine which procedural language to use. It's very likely that you'll need to do this in two steps:
1st: Duplicate the number of rows based on the column Qty using a decreasing loop
2nd: You'll need to create a sequential partionned column based on the Qty column

Hierarchical Data In Oracle SQL

I have tbl_parent like this in Oracle SQL, sample data is given below:
Id(primary key) parentid childid
1 1 2
2 1 3
3 2 1
4 3 1 -- This row is wrong
In above table, some rows are incorrectly inserted, for example, if parent_id 1 has child_id 3 then parent_id 3 should not have child_id 1 as 3 is already child of 1 so can not be parent, I have 5000+ rows and want to find these incorrect rows, any help please?
greatest and least functions might be used as
select least(parentid,childid) as least_par_chi_id,
greatest(parentid,childid) as greatest_par_chi_id
from tab
group by greatest(parentid,childid), least(parentid,childid)
having count(*)>1;
Basically you are looking for cycles in your table.
The Oracle functionality to indentify cycles in hierarchical query is
CONNECT BY NOCYCLE and CONNECT_BY_ISCYCLE
This query show all nodes that lead to cycle - column is_Cycle = 1
select tbl.* ,
CONNECT_BY_ISCYCLE is_Cycle,
SYS_CONNECT_BY_PATH(childid, '/') path
from tbl
CONNECT BY NOCYCLE PRIOR childid = parentid
For your data the result is
PARENTID CHILDID IS_CYCLE PATH
---------- ---------- ---------- ----------
1 2 0 /2
2 1 1 /2/1
1 3 1 /2/1/3
1 3 0 /3
3 1 1 /3/1
1 2 1 /3/1/2
2 1 0 /1
1 2 1 /1/2
1 3 1 /1/3
3 1 0 /1
1 2 1 /1/2
1 3 1 /1/3
Note taht each cycle is recognised on several places, so you get some redundant data.
The advantage of this apprach is, that it works for longer cycles too (where the simple GROUP BY approach fails).
Example for cycle of the length 3:
create table tbl as
select 1 parentid, 2 childid from dual union all
select 2 parentid, 3 childid from dual union all
select 3 parentid, 1 childid from dual;
PARENTID CHILDID IS_CYCLE PATH
---------- ---------- ---------- ----------
1 2 0 /2
2 3 0 /2/3
3 1 1 /2/3/1
2 3 0 /3
3 1 0 /3/1
1 2 1 /3/1/2
3 1 0 /1
1 2 0 /1/2
2 3 1 /1/2/3

Oracle Forms Secuences Or Autoincrement or an other option

I need to put the number 2 in the company that is CODCIA, the sequence is 1,2,3,4 etc. and when putting the number 4 the sequence is reset to 1.
I only have 1 table with a codcia field.
I have it this way:
CODCIA NRODOCTO
------ --------
2 1
2 2
2 3
4 4
4 5
4 6
WITH THIS CODE:
IF (:E.NRODOCTO in (2)) then
BEGIN
SELECT NVL(MAX(NRODOCTO),1200)+1
INTO :E.NRODOCTO
FROM PS91;
EXCEPTION WHEN OTHERS THEN :E.NRODOCTO := 1200;
END;
END IF;
and I need this:
CODCIA NRODOCTO
------ --------
2 1
2 2
2 3
4 1
4 2
4 3
It seems that quite a lot of information is missing in your question; not that you should post absolutely all information, but oversimplifying it also doesn't help.
Anyway: I'd say that you might be using the POST-INSERT trigger and update NRODOCTO column value, because - doing that in a form would require looping through the whole block, paying attention to CODCIA item value and incrementing the appropriate NRODOCTO value, and doing it for all distinct CODCIA values. That's somewhat dull as nobody prevents user from entering mixed CODCIA values.
Here's a SQL*Plus-based example (as I'm not going to create a form for it). The PS91 table most probably doesn't look as simple as that (re-read my first sentence), but that's what you told us.
SQL> create table ps91 (codcia) as
2 (select 2 from dual union all
3 select 2 from dual union all
4 select 2 from dual union all
5 --
6 select 4 from dual union all
7 select 4 from dual
8 );
Table created.
SQL> alter table ps91 add nrodocto number;
Table altered.
SQL> -- POST-INSERT form trigger
SQL> merge into ps91 p
2 using (select rowid rid, row_number() over (partition by codcia order by null) rn
3 from ps91
4 ) x
5 on (p.rowid = x.rid)
6 when matched then update set p.nrodocto = x.rn;
5 rows merged.
SQL> select * from ps91
2 order by codcia, nrodocto;
CODCIA NRODOCTO
---------- ----------
2 1
2 2
2 3
4 1
4 2
SQL>

Select Query on Sql Server

The question may be very simple but i don't know how to fix it,
I have this table structure
sno left Right
1 2 1
2 2 2
3 1 2
4 3 1
5 2 4
6 7 1
7 2 8
How do I get a result set like the one below
sno left Right Result
1 2 1 1
2 2 2 2
3 1 2 1
4 3 1 1
5 2 4 2
6 7 1 1
7 2 8 2
I wanna select the Data what mimimum value is matched between two columns,
Eg:3 and 1
1 is minimum value between these two and 1 is matched with 3, so the matched value is 1.
eg: 2 and 4
2 is minimum value between these two and 2 is is mathed with 4, so the matched value is 2.
Edited:
If choose 8 and 2 for example
8 contains(1,2,3,4,5,6,7,8)
2 contains(1,2)
So the Result is 2
Because 2 values are matched here.
I hope i explained it well, thanks
The following SQL will return the positive value of a subtraction operation between the left and right values - in a column with Result as the header. It will calculate the difference between left and right values - ABS will make the result positive.
SELECT
sno,
left,
Right,
ABS(left - right) AS Result
FROM tablename
One of the possible solutions:
DECLARE #t TABLE ( sno INT, l INT, r INT )
INSERT INTO #t
VALUES ( 1, 2, 1 ),
( 2, 2, 2 ),
( 3, 1, 2 ),
( 4, 3, 1 ),
( 5, 2, 4 ),
( 6, 7, 1 ),
( 7, 2, 8 )
SELECT *,
(SELECT MIN(v) FROM (VALUES(l),(r)) m(v)) AS m
FROM #t
Output:
sno l r m
1 2 1 1
2 2 2 2
3 1 2 1
4 3 1 1
5 2 4 2
6 7 1 1
7 2 8 2
case
when left < right then left
else right
end

Adding non existing data to SQL query

My SQL query returns the following result (screenshot):
x y count
----------- ----------- -----------
1 1 10
1 2 2
2 4 3
2 5 5
4 1 5
5 1 8
what i want is x, y should always contain 1 to 5 values, even if the query doesn't return them, in the above scenario x is missing 3. How to add the missing values here that are between 1 & 5.
Thanks in Advance
First you need to generate the desired data. You can use a table of numbers for this. Use CROSS JOIN to generate all possible combinations of two tables. Finally, OUTER JOIN the generated data with your table.
In the following query I have used union to build a list of numbers instead of fetching them from a table. But the idea remains same:
SELECT XList.x, YList.y, #temp.count
FROM (
SELECT 1 AS x UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
) AS XList
CROSS JOIN (
SELECT 1 AS y UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
) AS YList
LEFT JOIN #temp ON XList.x = #temp.x AND YList.y = #temp.y
Result:
x y count
----------- ----------- -----------
1 1 10
2 1 NULL
3 1 NULL
4 1 5
5 1 8
1 2 2
2 2 NULL
3 2 NULL
4 2 NULL
5 2 NULL
1 3 NULL
2 3 NULL
3 3 NULL
4 3 NULL
5 3 NULL
1 4 NULL
2 4 3
3 4 NULL
4 4 NULL
5 4 NULL
1 5 NULL
2 5 5
3 5 NULL
4 5 NULL
5 5 NULL
You can do it this way:
select t1.x, t2.y, s.count from
(values(1),(2),(3),(4),(5)) t1(x) cross join
(values(1),(2),(3),(4),(5)) t2(y)
left join #temp s on t1.x = s.x and t2.y = s.y