How to branch SQL query statement using conditions? - sql

Let's say we have table customer with column subject_type_fk. We need to find name of the customer which can be in table person or enterprise. subject_type_fk defines which of those 2 tables search in. subject_type_fk can be 0 or 1. subject_fk defines the primary key of record from person or enterprise. Both tables have name column we need to retrieve (actually enterprise.enterprise and person.last_name). Customer.customer is given as input parameter. How to write that kind of query for postgreSQL?
UPDATE

select p.last_name
from customer c join person p on c.subject_fk = p.person and c.subject_type_fk = 0
union
select e.full_name
from customer c join enterprise e on c.subject_fk = e.enterprise and c.subject_type_fk = 1
(edited after OP's edit)

Related

Complex SQL Query with Dead Ends and 7 Tables

I was tasked with creating a complex query that incudes all of the data from all of the tables minus the Keys. I am having an issue with the dead end tables and how to circle back around to include the data of the connecting table. I need to select columns DivisionName, ProgramName, ProgramChairFName, ProgramChairLName, CourseID, OutcomeDescription from the listed tables.
SQL Diagram
The 'dead-ends' aren't really dead-ends. When you join all the tables by the appropriate keys, you'll get an assembly of the information you want.
Consider a really simple example:
table person
id name
1 Alice
table pet
id person_id animal
1 1 cat
table hobby
id person_id activity
1 1 dancing
Here, the two tables pet and hobby link to the person table via the person_id key.
In your thinking, "pet" could be considered a "dead-end" because it doesn't link to hobby. But it doesn't need to. The query:
SELECT name, animal, activity
FROM person
JOIN pet ON person.id = pet.person_id
JOIN hobby ON person.id = hobby.person_id;
creates the correct joins back to the person table. It's not a linear path (person -> pet -> hobby). The nature of the joins are specified by the "ON" part of the query. You can see this simple example works here: http://sqlfiddle.com/#!9/02c94b/1
So, in your case, you can have a series of JOINs:
SELECT [all the columns you want]
FROM Division d JOIN Program p
ON d.DivisionKey = p.DivisionKey
JOIN ProgramChairMap pcm
ON p.ProgramKey = pcm.ProgramKey
JOIN ProgramChair pc
ON pcm.ProgramChairKey = pc.ProgramChairKey
JOIN Course c
ON p.ProgramKey = c.ProgramKey
JOIN CourseOutcome co
ON c.CourseKey = co.CourseKey
JOIN Outcome o
ON co.OutsomeKey = o.OutcomeKey

Overwrite values from one table to another using

I have two tables, customer and newCustomer. For specific columns, I want to overwrite the column values of customer table with the newCustomer table. For example :
customer.firstname = newCustomer.firstname
customer.lastname = newCustomer.lastname
** I do have matching Ids for the two tables.
I can think of how to do this in terms of coding, but am having a hard time when thinking of doing this in SQL.
I am using Microsoft SQL Server.
Would appreciate any examples or hints.
Presumably, you have some id that connects the two tables. If so, just use join and set:
update c
set firstname = nc.firstname,
lastname = nc.lastname
from customer c join
newcustomer nc
on c.customerid = nc.customerid;

Replacing a value in a table and then joining it to another table

brand new to the site and this is my first question! Sorry I am very limited in SQL skills. I mostly use Tableau but need to create a custom SQL join in Tableau.
I have 2 tables (C table and P table) that will need to be joined by the "Orders" field (found in both tables) and "Prefix" field (only found in C table but looking to change values in the "contract" field in P table to do the join). It is just two values that need to be changed. If contract="1234", it will be changed to "ABC. And it contract="5678" it will be changed to "XYZ".
Once those values are changed, Contract from P table can be joined to Prefix from C Table.
Sorry I couldnt explain this better but like I said I have very limited experience in SQL. Any help would be appreciated!
You can do it with a subquery.
select *
from C
inner join (
select *,
case contract when 1234 then ABC
when 5678 then XYZ
else contract
end as changed_contract -- or whatever name you like
from P
) P on P.changed_contract = C.prefix
and P.orders = C.orders
The table in the subquery will give you all the fields from P including a new field that takes into a account the modifications you made. Then you just join on that new field and change your select statement to grab the fields you want.
You could use a case to modify contract for certain values. Like:
select prefix + ' ' + case contract
when '1234' then 'ABC'
when '5678' then 'XYZ'
else contract
end as prefix_plus_contract
, *
from c
join p
on c.orders = p.orders

SQL fetch multiple values on join

Hi I have an SQL table which has two tables which make reference to the same foreign key in a separate table twice... something like
SALES table
idSales idClient1 idClient2
1 1 2
CLIENT table
idClient ClientName
1 Bob
2 Mick
I want to join the SALES table to the CLIENT table and return data as follows:
idSales idClientClientName1 idClientClientName2
1 Bob Mick
Can anyone help with the SQL for this? I'm getting ambiguous column name errors on my join.
Thank you
You need to basically join table Client on table Sales twice because there are two columns on table Sales that are dependent on table Client.
SELECT a.idSales,
b.ClientName ClientName1,
c.ClientName ClientName2
FROM Sales a
INNER JOIN Client b
ON a.idClient1 = b.idClient
INNER JOIN Client c
ON a.idClient2 = c.idClient
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
But when one of the columns or both columns are nullable, INNER JOIN will not give you all records from Sales because it will only select where it has atleast one match on the other table. Instead use LEFT JOIN.
I might add that in cases like this, I use table aliases that hint at what entity you are linking to in the joined table. If for example, the foreign keys were to an address table, and you had a work address, and a Home address, I would use tables aliases of h and w for the two joins. In your case, i.e.,
Selext s.idSales,
c1.ClientName ClientName1,
c2.ClientName ClientName2
From Sales s
Join Client c1
On c1.idClient = s.idClient1
Join Client c2
On c2.idClient = s.idClient2
For those beginner SQL folks who may see this question in the future, it's helpful to add in the AS words, it makes it clearer still:
SELECT
Sale.idSales,
c1.ClientName AS ClientName1,
c2.ClientName AS ClientName2
FROM
Sales AS Sale
INNER JOIN Client AS c1 ON Sale.idClient1 = c1.idClient
INNER JOIN Client AS c2 ON Sale.idClient2 = c2.idClient

Retrieve different row from same table

i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.