How get a row multiple times repeated at SQL query result? - sql

I want to get some rows repeated at my SQL result. I have tested IN operator but it would return each row one time only:
SELECT ID,text from myTable WHERE id IN (2,2,3,4,4,5)
It's can be done by sending multiple SELECT queries,
SELECT ID,text from myTable WHERE id=1
UNION ALL
SELECT ID,text from myTable WHERE id=4
UNION ALL
SELECT ID,text from myTable WHERE id=4
UNION ALL
SELECT ID,text from myTable WHERE id=4
UNION ALL
SELECT ID,text from myTable WHERE id=4
UNION ALL
SELECT ID,text from myTable WHERE id=4
UNION ALL
SELECT ID,text from myTable WHERE id=5
But it become very slow when I have big request.

The IN operator just checks whether the value on the left matches some value on the right.
To return multiple rows, you have to either use multiple SELECTs, or create a temporary table where the value 4 appears in multiple rows:
WITH IDs_to_search(ID) AS (
VALUES (2), (3), (4), (4), (5)
)
SELECT ID, text
FROM MyTable
JOIN IDs_to_search USING (ID);

Related

SQL Using TOP n with UNION, but only want results of second query if first does not have enough records

I am trying to write a sql statement that works something like a store. I have two queries and I want n records from the first query, but if there is less than n, I want the rest from the second.
I tried using TOP n and UNION
SELECT TOP 20 FROM (
(SELECT * FROM t1)
UNION
(SELECT * FROM t2))
but the results are from both tables regardless of how many are in t1. Basically, I want the first query to have precedence. If 5 records exist there and I want them and I want the rest from t2.
Add a column that identifies the query, so the first one have precedence.
SELECT TOP 20 *
FROM ((SELECT 1 as query, * FROM t1)
UNION
(SELECT 2 as query, * FROM t2))
ORDER BY query
you can use a CTE to place a sort order on the two tables and then use that in an order by clause
declare #foo1 table(
bar INT
)
insert into #foo1
values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
--,(11),(12),(13),(14),(15),(16),(17),(18),(19),(20)
declare #foo2 table(
bar INT
)
insert into #foo2
values (101),(102),(103),(104),(105),(106),(107),(108),(109),(110),(111),(112),(113),(114),(115),(116),(117),(118),(119),(120)
;with base_data as (
select
0 as sort,
f1.bar
FROM #foo1 f1
UNION
SELECT
1 as sort,
f2.bar
FROM #foo2 f2
)
select top 20 bar
from base_data
order by sort, bar

How to create table with multiple rows and columns using only SELECT clause (i.e. using SELECT without FROM clause)

I know that in SQL Server, one can use SELECT clause without FROM clause, and create a table with one row and one column
SELECT 1 AS n;
But I was just wondering, is it possible to use SELECT clause without FROM clause, to create
a table with one column and multiple rows
a table with multiple columns and one row
a table with multiple columns and multiple rows
I have tried many combinations such as
SELECT VALUES(1, 2) AS tableName(n, m);
to no success.
You can do it with CTE and using union(Use union all if you want to display duplicates)
Rextester Sample for all 3 scenarios
One Column and multiple rows
with tbl1(id) as
(select 1 union all
select 2)
select * from tbl1;
One row and multiple columns
with tbl2(id,name) as
(select 1,'A')
select * from tbl2;
Multiple columns and multiple rows
with tbl3(id,name) as
(select 1,'A' union all
select 2,'B')
select * from tbl3;
-- One column, multiple rows.
select 1 as ColumnName union all select 2; -- Without FROM;
select * from ( values ( 1 ), ( 2 ) ) as Placeholder( ColumnName ); -- With FROM.
-- Multiple columns, one row.
select 1 as TheQuestion, 42 as TheAnswer; -- Without FROM.
select * from ( values ( 1, 42 ) ) as Placeholder( TheQuestion, TheAnswer ); -- With FROM.
-- Multiple columns and multiple rows.
select 1 as TheQuestion, 42 as TheAnswer union all select 1492, 12; -- Without FROM.
select * from ( values ( 1, 2 ), ( 2, 4 ) ) as Placeholder( Column1, Column2 ); -- With FROM.
You can do all that by using UNION keyword
create table tablename as select 1 as n,3 as m union select 2 as n,3 as m
In Oracle it will be dual:
create table tablename as select 1 as n,3 as m from dual union select 2 as n,3 as m from dual
You can use UNION operator:
CREATE TABLE AS SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values you can use UNION ALL.
The column names in the result-set are usually equal to the column names in the first SELECT statement in the UNION.
try this:
--1) a table with one column and multiple rows
select * into tmptable0 from(
select 'row1col1' as v1
union all
select 'row2col1' as v1
) tmp
--2) a table with multiple columns and one row
select 'row1col1' as v1, 'row1col2' as v2 into tmptable1
--3) a table with multiple columns and multiple rows
select * into tmptable2 from(
select 'row1col1' as v1, 'row1col2' as v2
union all
select 'row2col1' as v2, 'row2col2' as v2
) tmp
One can create a view and later can query it whenever required
-- table with one column and multiple rows
create view vw1 as
(
select 'anyvalue' as col1
union all
select 'anyvalue' as col1
)
select * from vw1
-- table with multiple columns and multiple rows
create view vw2 as
(
select 'anyvalue1' as col1, 'anyvalue1' as col2
union all
select 'anyvalue2' as col1, 'anyvalue2' as col2
)
select * from vw2

Getting distinct list of ints from 2 distinct int lists

I have 2 separate queries that are just basic selects, both returning a single distinct column of ints. I need to then combined these 2 lists of ints together to produce a final single distinct list of ints.
Is there any faster way to do this than the following?
SELECT DISTINCT ID
FROM dbo.Test
UNION
SELECT DISTINCT ID
FROM dbo.Test2
If you don't have duplicates within each table, then the following is probably faster:
select id
from dbo.test
union all
select id
from dbo.test1 t1
where not exists (select 1 from dbo.test t where t.id = t1.id);
For this, you want an index on test(id).
Even with duplicates, the following is likely to be faster:
select distinct id
from dbo.test
union all
select distinct id
from dbo.test1 t1
where not exists (select 1 from dbo.test t where t.id = t1.id);
This requires indexes for both test(id) and test1(id). The idea is that the indexes are scanned to return the id.
I think that the fastest approach in your case is to remove the two DISTINCT since UNION will remove all duplicates overall anyway:
SELECT ID
FROM dbo.Test
UNION
SELECT ID
FROM dbo.Test2
Note that the two DISTINCTs don't ensure uniqueness across both sequences anyway, that's what the UNION does. If you don't need/want unique elements use UNION ALL.

How to execute UNION without sorting? (SQL)

UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates.
UNION also sort the final output.
What I want is the UNION ALL without duplicates and without the sort. Is that possible?
The reason for this is that I want the result of the first query to be on top of the final result, and the second query at the bottom (and each sorted as if they where run individually).
I notice this question gets quite a lot of views so I'll first address a question you didn't ask!
Regarding the title. To achieve a "Sql Union All with “distinct”" then simply replace UNION ALL with UNION. This has the effect of removing duplicates.
For your specific question, given the clarification "The first query should have "priority", so duplicates should be removed from bottom" you can use
SELECT col1,
col2,
MIN(grp) AS source_group
FROM (SELECT 1 AS grp,
col1,
col2
FROM t1
UNION ALL
SELECT 2 AS grp,
col1,
col2
FROM t2) AS t
GROUP BY col1,
col2
ORDER BY MIN(grp),
col1
"UNION also sort the final output" - only as an implementation artifact. It is by no means guaranteed to perform the sort, and if you need a particular sort order, you should specify it with an ORDER BY clause. Otherwise, the output order is whatever is most convenient for the server to provide.
As such, your request for a function that performs a UNION ALL but that removes duplicates is easy - it's called UNION.
From your clarification, you also appear to believe that a UNION ALL will return all of the results from the first query before the results of the subsequent queries. This is also not guaranteed. Again, the only way to achieve a particular order is to specify it using an ORDER BY clause.
SELECT *, 1 AS sort_order
FROM table1
EXCEPT
SELECT *, 1 AS sort_order
FROM table2
UNION
SELECT *, 1 AS sort_order
FROM table1
INTERSECT
SELECT *, 1 AS sort_order
FROM table2
UNION
SELECT *, 2 AS sort_order
FROM table2
EXCEPT
SELECT *, 2 AS sort_order
FROM table1
ORDER BY sort_order;
But the real answer is: other than the ORDER BY clause, the sort order will by arbitrary and not guaranteed.
Consider these tables (Standard SQL code, runs on SQL Server 2008):
WITH A
AS
(
SELECT *
FROM (
VALUES (1),
(2),
(3),
(4),
(5),
(6)
) AS T (col)
),
B
AS
(
SELECT *
FROM (
VALUES (9),
(8),
(7),
(6),
(5),
(4)
) AS T (col)
), ...
The desired effect is this to sort table A by col ascending, sort table B by col descending then unioning the two, removing duplicates, retaining order before the union and leaving table A results on the "top" with table B on the "bottom" e.g. (pesudo code)
(
SELECT *
FROM A
ORDER
BY col
)
UNION
(
SELECT *
FROM B
ORDER
BY col DESC
);
Of course, this won't work in SQL because there can only be one ORDER BY clause and it can only be applied to the top level table expression (or whatever the output of a SELECT query is known as; I call it the "resultset").
The first thing to address is the intersection between the two tables, in this case the values 4, 5 and 6. How the intersection should be sorted needs to be specified in SQL code, therefore it is desirable that the designer specifies this too! (i.e. the person asking the question, in this case).
The implication in this case would seem to be that the intersection ("duplicates") should be sorted within the results for table A. Therefore, the sorted resultset should look like this:
VALUES (1), -- A including intersection, ascending
(2), -- A including intersection, ascending
(3), -- A including intersection, ascending
(4), -- A including intersection, ascending
(5), -- A including intersection, ascending
(6), -- A including intersection, ascending
(9), -- B only, descending
(8), -- B only, descending
(7), -- B only, descending
Note in SQL "top" and "bottom" has no inferent meaning and a table (other than a resultset) has no inherent ordering. Also (to cut a long story short) consider that UNION removes duplicate rows by implication and must be applied before ORDER BY. The conclusion has to be that each table's sort order must be explicitly defined by exposing a sort order column(s) before being unioned. For this we can use the ROW_NUMBER() windowed function e.g.
...
A_ranked
AS
(
SELECT col,
ROW_NUMBER() OVER (ORDER BY col) AS sort_order_1
FROM A -- include the intersection
),
B_ranked
AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY col DESC) AS sort_order_1
FROM B
WHERE NOT EXISTS ( -- exclude the intersection
SELECT *
FROM A
WHERE A.col = B.col
)
)
SELECT *, 1 AS sort_order_0
FROM A_ranked
UNION
SELECT *, 2 AS sort_order_0
FROM B_ranked
ORDER BY sort_order_0, sort_order_1;
select T.Col1, T.Col2, T.Sort
from
(
select T.Col1,
T.Col2,
T.Sort,
rank() over(partition by T.Col1, T.Col2 order by T.Sort) as rn
from
(
select Col1, Col2, 1 as Sort
from Table1
union all
select Col1, Col2, 2
from Table2
) as T
) as T
where T.rn = 1
order by T.Sort
Try this:
SELECT DISTINCT * FROM (
SELECT column1, column2 FROM Table1
UNION ALL
SELECT column1, column2 FROM Table2
UNION ALL
SELECT column1, column2 FROM Table3
) X ORDER BY Column1
The sort is used to eliminate the duplicates, and is implicit for DISTINCT and UNION queries (but not UNION ALL) - you could still specify the columns you'd prefer to order by if you need them sorted by specific columns.
For example, if you wanted to sort by the result sets, you could introduce an additional column, and sort by that first:
SELECT foo, bar, 1 as ResultSet
FROM Foo
WHERE bar = 1
UNION
SELECT foo, bar, 2 as ResultSet
FROM Foo
WHERE bar = 3
UNION
SELECT foo, bar, 3 as ResultSet
FROM Foo
WHERE bar = 2
ORDER BY ResultSet
I assume your tables are table1 and table2 respectively,
and your solution is;
(select * from table1 MINUS select * from table2)
UNION ALL
(select * from table2 MINUS select * from table1)
1,1:
select 1 from dual
union all select 1 from dual
1:
select 1 from dual
union select 1 from dual
You can do something like this.
Select distinct name from (SELECT r.name FROM outsider_role_mapping orm1
union all
SELECT r.name FROM user_role_mapping orm2
) tmp;

How do I LIMIT the number of rows in a DELETE with DB2?

I want to add a security on a sensitive table when I delete lines with an SQL request on a DB2 table.
I want to mimic the way MySQL allows you to limit the numbers of rows deleted in an SQL request.
Basically I want to do this with DB2 :
DELETE FROM table WHERE info = '1' LIMIT 1
Is there a way to do that with DB2 ?
delete from table where id in (select id from table where info = '1' order by id fetch first 1 rows only)
It really depends on your platform.
If you're using DB2 on Linux/Unix/Windows, you can just create a select that gets the rows you want, and put that as a subquery for your delete, and DB2 will be able to delete the results of your select. Like so:
DELETE FROM (
SELECT 1
FROM table
WHERE info = '1'
ORDER BY your_key_columns
FETCH FIRST ROW ONLY
) AS A
;
If you're on DB2 for z/OS, that syntax doesn't work, unfortunately. But, you can use your primary keys to do basically the same thing (this one also works on LUW):
DELETE FROM table
WHERE (info, key2) IN (
SELECT info, key2
FROM table
WHERE info = 1
ORDER BY key2
FETCH FIRST ROW ONLY
);
Here is an example script that demonstrates how it's used:
DECLARE GLOBAL TEMPORARY TABLE SESSION.TEST(
ID INT
,RN INT
) ON COMMIT PRESERVE ROWS;
INSERT INTO SESSION.TEST
SELECT 1,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,3 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,4 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,5 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2,3 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3,3 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 5,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 6,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 7,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 8,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 9,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10,1 FROM SYSIBM.SYSDUMMY1
;
SELECT * FROM SESSION.TEST ORDER BY ID, RN;
-- LUW Version
DELETE FROM (
SELECT 1
FROM SESSION.TEST
WHERE ID = 1
ORDER BY RN
FETCH FIRST ROW ONLY
) AS A
;
--Mainframe version
DELETE FROM SESSION.TEST
WHERE (ID, RN) IN (
SELECT ID, RN
FROM SESSION.TEST
WHERE ID = 1
ORDER BY RN
FETCH FIRST ROW ONLY
);
SELECT * FROM SESSION.TEST ORDER BY ID, RN;
DROP TABLE SESSION.TEST;
On IBMi DB2:
DELETE FROM table WHERE RRN(table) in
(SELECT RRN(table) FROM table WHERE col1 = '1' AND col2 = '2' FETCH FIRST 5 ROWS ONLY)
If your primary key has multiple values, or you just need multiple values as the condition, this is the query that works:
DELETE FROM TABLE
WHERE (COLUMN1, COLUMN2) IN (
SELECT COLUMN1, COLUMN2 FROM TABLE
WHERE SOME_COLUMN='THIS'
AND SOME_OTHER_COLUMN LIKE 'THAT%'
FETCH FIRST 10 ROWS ONLY)
How is this query?
delete from table D where exists
( select * from ( select * from table M fetch first 10 rows only ) as M
where M.object_id = D.object_id )
MERGE INTO XYZ A<BR>
USING (<BR>
SELECT RID_BIT(B) CHAVE<BR>
FROM XYZ B<BR>
FETCH FIRST 100000 ROWS ONLY) B<BR>
ON RID_BIT(A) = B.CHAVE<BR>
WHEN MATCHED THEN DELETE;
Just select a statement, and put the statement inside the delete query:
delete from (
select from table WHERE info = '1' order by id fetch first 25000 rows only
)
DELETE
FROM Bibl/File
WHERE RRN(File) = (
SELECT min(RRN(File))
FROM Bibl/File
WHERE Fld1 = 'xx'
)
The RRN function is to AS400/iSeries/PowerSystem alone. In other environments there are other functions for the relative record number.
This makes it possible to erase a record of several identical even without UNIQUE key. It can also be used to update with minor changes.
works like the LIMIT but with DELETE and / or UPDATE.
It only works on SQL DB2 in other settings should be changed by RRN function to return the column number
DELETE FROM table
WHERE info = '1'
FETCH FIRST 1 ROWS ONLY