Oracle Query fetching table name alongwith column name - sql

select trx_id,refernce number from
(select * from abcd_1_txt union
select * from abcd_2_txt union
select * from abcd_3_txt union
select * from abcd_4_txt)
where trx_id in (123,321,1234)
In the query all the tables are of same format, same column names and same number of columns.
After running this query, surely i will get some data.
My question --- is there any way to know from which of these tables, i am getting the output.

Try to add a column with number of query as below
select qrynum, trx_id,refernce number from
(select 1 as qrynum,* from abcd_1_txt union
select 2,* from abcd_2_txt union
select 3,* from abcd_3_txt union
select 4,* from abcd_4_txt)
where trx_id in (123,321,1234)
as Joe W said in the comment below you can also use name of the table instead of query number, short example:
select tabname, trx_id,refernce number from
(select 'abcd_1_txt' as tabname,* from abcd_1_txt union
...
where trx_id in (123,321,1234)
but both ways don't eliminate duplicates, so you can use union all instead of union. Other way to do that is to run quires separately with the condition
select * from abcd_1_txt where trx_id in (123,321,1234)
select * from abcd_2_txt where trx_id in (123,321,1234)
.
.
.

Related

Union All have mismatched column count

I am trying to union some table. But I am having some issues with the query as I have the following error:
Queries in UNION ALL have mismatched column count; query 1 has 10 columns, query 2 has 12 columns at [3:1]
The query used is:
SELECT * FROM `table_1`
UNION ALL
SELECT * ,null, FROM `table_2`
UNION ALL
SELECT * FROM `table_3`
UNION ALL
SELECT * FROM `table_4`
UNION ALL
SELECT * , null, FROM `table_5`
Please someone have any suggestions?
In a UNION ALL all queries need to be the same number of columns.
You need to post the schema of all tables. But according to the error message you posted, this could help for the 2 first tables:
SELECT *, null FROM `table_1` -- assuming table_1 has 10 columns, add 1 more empty to match the table_2
UNION ALL
SELECT * FROM `table_2` -- assuming table_2 has 11 columns
-- make the same to the rest of queries
-- UNION ALL
-- SELECT * FROM `table_3`
-- UNION ALL
-- SELECT * FROM `table_4`
-- UNION ALL
-- SELECT * , null, FROM `table_5`
Update:
Adding a example of union all declaring the columns:
SELECT
col1_str,
col2_str,
col3_int,
col4_int
FROM `table_1`
UNION ALL
SELECT
col2_str,
FORMAT_DATE("%Y-%m-%d", col1_date) as col1_str, -- transform a date column to string
col3_int,
null
FROM `table_2`

Access append query inconsistent

I have a table creation query (combine2) which takes a query (combine) and makes it a table (linegraph). This table must reflect changes in the associated query. Currently I have assigned a macro to run the append query to reflect changes.
The problem is that no matter how the append query is called it sometimes but not always copies all the data. Sometimes it wont import the first 100 or so rows. How can I get an updating table that always matches my query?
Append Query: Combine2
INSERT INTO linegraph SELECT * FROM Combine;
Query to turn into table: Combine
SELECT * FROM Month1calc UNION
SELECT * FROM Month2calc UNION
SELECT * FROM month3calc UNION
SELECT * FROM Month4calc UNION
SELECT * FROM Month5calc UNION
SELECT * FROM Month6calc UNION
SELECT * FROM Month7calc UNION
SELECT * FROM Month8calc UNION
SELECT * FROM Month9calc UNION
SELECT * FROM Month10calc UNION
SELECT * FROM Month11calc UNION
SELECT * FROM Month12calc UNION
SELECT * FROM Month13calc UNION
SELECT * FROM Month14calc UNION
SELECT * FROM Month15calc UNION
SELECT * FROM Month16calc UNION
SELECT * FROM Month17calc UNION
SELECT * FROM Month18calc;
`
Try changing UNION to UNION ALL.
When you use UNION, the query only returns unique rows (i.e.: duplicates are removed). When you use UNION ALL, duplicates are not removed.

input string to table

I am doing some debugging in SQL for oracle 10g. I have a big input string which is used in "IN Clause" i.e.
select * from table where col in ('str2','str3','str4','str5',...)
i want to convert the in clause to rows or table?
Is there a way to do this i.e.
select 'str2','str3','str4','str5', .. from dual
but this outputs multiple columns and i want multiple rows?
Edit:
Here is what i am trying to do. suppose i have an excel data in tmp_table1 (cant create in reality) and tmp_table1 is same as the IN clause, then the below statement will give the missing keys.
SELECT *
FROM tmp_table1
WHERE unique_id NOT IN (
SELECT unique_id
FROM table1
WHERE unique_id IN
('str1', 'str2', 'str3', 'str4'))
now #andriy-m solution works if the in string is less than 4000. but what if its greater?
You are probably looking for this solution.
You can UNION the values into multiple rows:
SELECT 'str2' AS col FROM dual
UNION
SELECT 'str3' FROM dual
UNION
SELECT 'str4' FROM dual
UNION
SELECT 'str5' FROM dual

SQL return all id's from 1 table and only ones that don't match from another

I have 2 tables: #tmptable1 and #tmptable2. These tables have an ID to uniquely identify each record.
I want to return all records from table 1, but I only want to return the records from table 2 that aren't in table 1
I want this done in 1 query.
Thanks!
SELECT * FROM '#tmptable1'
UNION ALL
SELECT * FROM '#tmptable2' WHERE
ID NOT IN (SELECT ID FROM #tmptable1 WHERE ID IS NOT NULL)
If all the other fields and data of the two tables is identical you could do this:
SELECT * FROM #tmptable1
UNION
SELECT * FROM #tmptable2
UNION without the ALL modifier removes duplicates.
I don't quite understand what is it that you want to get in the results, but UNION selects distinct values, so you won't have duplicate values (values from #tmptable2 that already exist in #tmptable1).
SELECT * FROM #tmptable1
UNION
SELECT * FROM #tmptable2
Anyways, these records are records from #tmptable2 that aren't in #tmptable1.
SELECT * FROM #tmptable2
EXCEPT
SELECT * FROM #tmptable1
SELECT ID FROM #tmptable1
UNION
SELECT ID FROM #tmptable2
The UNION operator will automatically remove duplicates.

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