Joining the rows of one table to the columns of another in SQL - sql

I have the following two tables in SQLite:
transactions:
ID
Department
1
IT
2
Customer Service
3
Cleaning
standards:
IT
Customer Service
Cleaning
9.12
17.8
24.86
I want to join these two tables so the numbers in the second table become rows in my first table based on the matching value in the second table. The resulting table would look like this:
ID
Department
Standard
1
IT
9.12
2
Customer Service
17.8
3
Cleaning
24.86
How can I join these, as I'm using the rows of one table and the columns of another?

You need a CROSS join of the tables and a CASE expression to pick the appropriate standard:
SELECT t.*,
CASE t.Department
WHEN 'IT' THEN s.IT
WHEN 'Customer Service' THEN s.`Customer Service`
WHEN 'Cleaning' THEN s.Cleaning
END AS Standard
FROM transactions t JOIN standards s;
See the demo.

Related

How to combine two tables so the new one to have only new records from the first and all the rest from the second?

I have table A with 1 million records and a table B with 10 million records. Table A can have new records not present in table B. How I can combine the two tables so the table C to have all the records from A and all the records from B but with updated records from A. Both tables A and B have a column ID by which both can be joined.
You have to run a query and follow these instructions.
I would construct the query using the Join clause. For example:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Find a detailed explanation for the different types of Joins in BigQuery here.

SQL Query to fetch information based on one or more condition. Getting combinations instead of exact number

I have two tables. Table 1 has about 750,000 rows and table 2 has 4 million rows. Table two has an extra ID field in which I am interested, so I want to write a query that will check if the 750,000 table 1 records exist in table 2. For all those rows in table 1 that exist in table 2, I want the respective ID based on same SSN. I tried the following query:
SELECT distinct b.UID, a.*
FROM [Analysis].[dbo].[Table1] A, [Proteus_8_2].dbo.Table2 B
where a.ssn = b.ssn
Instead of getting 750,000 rows in the output, I am getting 5.4 million records. Where am i going wrong?
Please help?
You're requesting all the rows in your select if b.UID is a unique field in column two.
Also if SSN is not unique in table one you can get the higher row count than the total row count for table 2.
You need to consider what you want from table 2 again.
EDIT
You can try this to return distinct combinations of ssn and uid when ssn is found in table 2 provided that ssn and uid have a cardinality of 1:1, i.e., every unique ssn has a single unique uid.
select distinct
a.ssn,b.[UID]
from [Analysis].[dbo].[Table1] a
cross apply
( select top 1 [uid] from [Proteus_8_2].[dbo].[Table2] where ssn = a.ssn ) b
where b.[UID] is not null
Try with LEFT JOIN
SELECT distinct b.UID, a.*
FROM [Analysis].[dbo].[Table1] A LEFT JOIN [Proteus_8_2].dbo.Table2 B
on a.ssn = b.ssn
Since the order detail table is in a one-many relationship to the order table, that is the expected result of any join. If you want something different, you need to define for us the business rule that will tell us how to select only one record from the Order detail table. You cannot effectively write SQL code without understanding the business rules that of what you are trying to achieve. You should never just willy nilly select one record out of the many, you need to understand which one you want.

SQL queries with different results

I have two tables that I try to join over one field and it gives me different results in two queries that should give same results. Queries are:
SELECT * FROM tblCustomer tca
WHERE tca.PhoneNumber IN(
SELECT ts.SubscriptionNumber FROM sub.tblSubscription ts
WHERE ts.ServiceTypeID=4
AND ts.SourceID=-1
)
and
SELECT tca.*
FROM sub.tblSubscription ts
inner JOIN tblCustomer tca
ON ts.SubscriptionNumber = tca.PhoneNumber
WHERE ts.ServiceTypeID = 4
AND ts.SourceID = -1
How is this possible?
I'm assuming a customer can have multiple subscriptions, right? Let's assume you have 5 customers, each with 2 subscriptions...
When doing a SELECT ... FROM Customer WHERE IN (Subscription), you will receive 5 customer records, because each of those 5 customers are in fact in the subscription table, even though the subscription table will have 10 records. You are inherently asking the database for the data from one table, where the value of one of it's fields exists in another table. So it will only return the distinct records in the FROM table, irrespective of the amount of data in the WHERE IN table.
On the other hand, INNER JOINing the Customer table with the subscription table will return 5 customers x 2 subscriptions each = 10 records. By JOINing the tables you are asking the database for ALL the data in each table, where the data is matched up against specific fields.
So yes, the 2 queries will definitely give you different results.

How to reconcile common rows in two tables columnwise in oracle?

I have two tables in a database. The tables are called bi_employee and hr_employee. These two tables have few similar columns and then other extra columns specific to each table.
I need to reconcile the data between these two tables based on some user defined columns.
Lets say the common columns are id(pk), emp_code, region, country, title, division etc.
Now when I reconcile these two tables, I would want to see the rows which are there in both the tables but only differ in some columns.
e.g. emp_code 1000 is prsent in both the tables but in hr_employee his title is jr. developer but in bi_employee his title is sr.developer.
I do not want the records which are in one table but not in another table.
I only need to reconcile the rows which are present in both the tables but on columnwise which will be selected by the user.
User may chose to reconcile based on title or region or country or all of them.
Please help.
EDIT 1:
This is what I have done so far, with the following query I could get all the records which are there in both the tables. Now I just need to compare their columns to see if there are any mismatches.
SELECT emp_code FROM bi_employee INTERSECT SELECT emp_code FROM hr_employee
From what I understand, there is only one column that relates the recodes in each table; emp_code. Then you want to show records where the emp_code is the same in each talbe, but other field(s) are different.
You can do that with a simple join and filter in a WHERE clause...
SELECT
*
FROM
bi_employee
INNER JOIN
hr_employee
ON bi_employee.emp_code = hr_employee.emp_code
WHERE
(bi_employee.title <> hr_employee.title)
OR (bi_employee.region <> hr_employee.region)
etc, etc
(If any fields are nullable, you'll need to account for that with something like ISNULL(bi.x, '') <> ISNULL(hr.x, '')).
You might try this.
select hr.<list of columns to reconcile from hr table>
from bi_employee bi join hr_employee hr on hr.emp_code = bi.empcode
minus
select bi.<list of columns to reconcile from bi table>
from bi_employee bi join hr_employee hr on hr.emp_code = bi.empcode

SQLite JOIN two tables with duplicated keys

I need to join two tables on two different fields. I have table 1 like this:
key productid customer
1 100 jhon
2 109 paul
3 100 john
And table 2 has same fields but aditional data I must relate to first table
key productid customer status date ...
1 109 phil ok 04/01
2 109 paul nok 04/03
3 100 jhon nok 04/06
4 100 jhon ok 04/06
Both "key" fields are autoincrement. Problem is that my relationship fields are repeated several times across result and I need to generate a one-to-one relationship, in such manner that one row from table 2 must be related ONLY ONCE with a row on table 1.
I did a left join on (customer=customer and productid=productid) but relationship came out duplicated, a row from tablet 2 was related many times to rows of table one.
To clarify things...
I have to cross check both tables, table 1 is loaded from an XLS report, table 2 is data from a database that reflects customer transactions with many status data. I have to check if a row from XLS exists in database and then load additional status data. I must produce a report when rows from XLS has no correspondent data on database.
How can accomplish this JOIN, is this possible with only SQL?
You can accomplish this in MS SQL using the sql below. Not sure if SQLite supports this.
select a.*, c.*
from table2 a, ( select min(key) key, productid, customer
from table1
group by productid, customer
) b,
table1 c
where a.productid = b.productid
and a.customer = b.customer
and b.key = c.key
One way to understand this would be to figure out what each table represents exactly. Both tables seem to represent the same thing, with a row representing what you might call a purchase. Why are there two separate tables, then? Perhaps the second table goes into more depth about each purchase? Like jhon bought product 100, and it was 'nok' first and then 'ok'? Is so, then the key (what makes the table unique) for the second table would be all three fields.
You still join on only the two fields that match, but you can't expect uniqueness if there are two rows with the same unique keys.
It helps sometimes to create additional indexes on a table, to see what is truly unique.