I have 2 tables.
One is Admin Table with all valid combinations and other is a Transaction table with the transactions.
I am trying to write a query which will give me the result of invalid combinations from transaction table.
The query should return the invalid transactions.
As you can see in the example below ABCD - TUV and IJKL - EFG are not valid combinations.
Admin Table
Column A Column B
ABCD XYZ
ABCD EFG
EFGH XYZ
IJKL TUV
IJKL XYZ
Alternatively you can use NOT EXISTS e.g.
SELECT * FROM TRANSACTIONS T WHERE NOT EXISTS (SELECT 1
FROM ADMIN
WHERE COMBINATION = T.COMBINATION)
Wether to use EXISTS or NOT IN depends largely on the data you're querying.
you can find more info on the topic here
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:953229842074
There is no example in your question, but you should use NOT IN.
For example :
SELECT * FROM TRANSACTIONS WHERE combination NOT IN (SELECT combination FROM ADMIN)
Related
I have a table of customer contacts and their role. Simplified example below.
customer | role | userid
----------------------------
1 | Support | 123
1 | Support | 456
1 | Procurement | 567
...
desired output
customer | Support1 | Support2 | Support3 | Support4 | Procurement1 | Procurement2
-----------------------------------------------------------------------------------
1 | 123 | 456 | null | null | 567 | null
2 | 123 | 456 | 12333 | 45776 | 888 | 56723
So dynamically create number of required columns based on how many user are in that role. It's a small number of roles. Also I can assume max 5 user in that same role. Which means worst case I need to generate 5 columns for each role. The userids don't need to be in any particular order.
My current approach is getting 1 userid per role/customer. Then a second query pulls another id that wasn't part of first results set. And so on. But that way I have to statically create 5 queries. It works. But I was wondering whether there is a more efficient way? Dynamically creating needed columns.
Example of pulling one user per role:
SELECT customer,role,
(SELECT top 1 userid
FROM temp as tmp1
where tmp1.customer=tmp2.customer and tmp1.role=tmp2.role
) as userid
FROM temp as tmp2
group by customer,role
order by customer,role
SQL create with dummy data
create table temp
(
customer int,
role nvarchar(20),
userid int
)
insert into temp values (1,'Support',123)
insert into temp values (1,'Support',456)
insert into temp values (1,'Procurement',567)
insert into temp values (2,'Support',123)
insert into temp values (2,'Support',456)
insert into temp values (2,'Procurement',888)
insert into temp values (2,'Support',12333)
insert into temp values (2,'Support',45776)
insert into temp values (2,'Procurement',56723)
You may need to adapt your approach slightly if you want to avoid getting into the realm of programming user defined table functions (which is what you would need in order to generate columns dynamically). You don't mention which SQL database variant you are using (SQL Server, PostgreSQL, ?). I'm going to make the assumption that it supports some form of string aggregation feature (they pretty much all do), but the syntax for doing this will vary, so you will probably have to adjust the code to your circumstances. You mention that the number of roles is small (5-ish?). The proposed solution is to generate a comma-separated list of user ids, one for each role, using common table expressions (CTEs) and the LISTAGG (variously named STRING_AGG, GROUP_CONCAT, etc. in other databases) function.
WITH tsupport
AS (SELECT customer,
Listagg(userid, ',') AS "Support"
FROM temp
WHERE ROLE = 'Support'
GROUP BY customer),
tprocurement
AS (SELECT customer,
Listagg(userid, ',') AS "Procurement"
FROM temp
WHERE ROLE = 'Procurement'
GROUP BY customer)
--> tnextrole...
--> AS (SELECT ... for additional roles
--> Listagg...
SELECT a.customer,
"Support",
"Procurement"
--> "Next Role" etc.
FROM tsupport a
JOIN tprocurement b
ON a.customer = b.customer
--> JOIN tNextRole ...
Fiddle is here with a result that appears as below based on your dummy data:
I have two tables. Table1, Table2. I have to get names from Table2 based on some other id's which are common in Table1 and Table2. I have to update Table1 and field name is "Name"
Table1:
id | Name
-------------
a,b,c |
Table2:
id | Name
------------
a | dinesh
b | suresh
c | ganesh
Output in Table1:
Id | Name
-------------------------------
a,b,c | dinesh, suresh, ganesh
Since you show no attempt at solving this yourself, I will assume you just need to be pointed in the direction of a solution.
One way to approach this is to use a string-split function (there is one built in to SS 2017, but previous versions you had to create one--there are plenty available on this site if you google).
This function will return a table with one row for each of the comma separated values in Table1. Then you JOIN that to Table2 to get the name and use that to build a comma-separated string with a group-concat function (also built in to SS 2017, but with plenty of examples here about how to do this with earlier versions).
Once you have gotten those values SELECTed, it's a simple matter to generate an UPDATE from that SELECT and update Table1 with the comma-separated string.
Just wondering will it be possible to insert records into a table from 2 difference sources in SQL?
Example:
Table 1
Number
1
2
Table 2
Name
Alex
Amy
I want to insert records into table 3 from table 1 and table 2 and the result for table 3 should be:
Number Name
1 Alex
2 Alex
1 Amy
2 Amy
Any way that I can do it in SQL Server?
Try a CROSS JOIN and a SELECT ... INTO:
This join relates every-with-every row. The result will be filled into a new table on the fly:
SELECT Nrs.Nr
,Nms.Name
INTO dbo.TheNewTable
FROM dbo.NumberTable AS Nrs
CROSS JOIN dbo.NameTable AS Nms;
See the result:
SELECT * FROM dbo.TheNewTable;
connect for two connections .
use a script not only in SQL :javca for example.
use hashmap and hashet ...
isnert in temporary table(drop on commit for example).
copy in table 3.
-don't forget to close connections.
Here is my table
Table Suppliers in Database A
ID(AUTONUMBER) | SupplierCode(Unique) | SupplierName
001 supp001 TestA
002 supp002 TestB
003 supp003 TestC
Table Suppliers in Database B
ID(AUTONUMBER) | SupplierCode(Unique) | SupplierName
001 supp001 TestA
003 supp003 TestC
In this case, i want to insert supp002 to table Suppliers in Database B
And it will skip supp001 and supp003 because the SUPPLIERCODE exists
Can anyone help me with this condition
note: SQL Server query not MySQL
Might as well make it an answer
insert into databaseB..suppliers
Select * from databaseA..suppliers where id not in (select id from databaseB..suppliers)
The table references should be correct, but I can't verify. You might want to go databasea.dbo.suppliers if that's the correct full name
Assuming the two databases are on the same server and both tables are part of the "dbo" schema, inserting only those records in the A table that don't already exist in B into the B table can be handled like this:
INSERT INTO DatabaseB.dbo.Suppliers
SELECT ID,SupplierCode, SupplierName FROM DatabaseA.dbo.Suppliers
WHERE SupplierCode NOT IN
(SELECT SupplierCode FROM DatabaseB.dbo.Suppliers)
If the tables in A and B belong to a different schema, replace the "dbo" above with the appropriate schema name(s).
If the databases reside on different servers, this article that discusses creating linked servers may be useful but the syntax will be similar.
Create Linked Servers
I do not normally recommend using In or Not in statements i usually uses joins so try this
Insert into DatabaseB
(SupplierCode,
SupplierName)
Select SupplierCode,
SupplierName
From DatabaseA A
Left join DatabaseB B
On A.SupplierCode = B.SupplierCode
Where B.SupplierCode IS NULL
Have two tables say ABC and XYZ and contain one column which data will be unique across these table. Now I have id with me but don't know in which table this id belongs. Is it possible to fetch record in single query with that id from ABC or XYZ?
Thanks in advance.
Is it possible to fetch record in single query with that id from ABC
or XYZ?
Yes, you can use UNION(implicit distinct) or UNION ALL(with duplicate values) to get all id's from the two tables:
SELECT id FROM ABC
UNION ALL
SELECT id FROM XYZ