SQLite Join Help - sql

Consider this table structure
people{id,firstName,lastName,preferredName}
list{id,person1_id,person2_id}
I want to get all data from list but with the names of the persons instead of IDs and with a twist: if preferredName is set, use that, otherwise use firstName+SPACE+lastName.
The query if I only needed one name would be this:
SELECT list.id,
CASE WHEN people.preferredName IS NULL OR people.preferredName="" THEN people.firstName||' '||people.lastName ELSE people.preferredName END AS preferredName
FROM list LEFT JOIN people ON list.person1_id=people.id;
Thank you.

COALESCE() returns its first non-null argument.
COALESCE(preferredName, firstName || ' ' || lastName) as person_name
I don't think you need left joins, but I could be wrong. (If you have a foreign key constraint such that person1_id and person2_id must exist in people, you don't need a left join.)
SELECT list.id,
COALESCE( p1.preferredName, p1.firstName || ' ' || p1.lastName) as person1_name,
COALESCE( p2.preferredName, p2.firstName || ' ' || p2.lastName) as person2_name,
FROM list
INNER JOIN people p1 ON list.person1_id= p1.id
INNER JOIN people p2 on list.person2_id = p2.id

Use table aliases to let you join the same table twice.
SELECT list.id,
CASE WHEN people1.preferredName IS NULL OR people1.preferredName = ""
THEN people1.firstName || ' ' || people1.lastName
ELSE people1.preferredName
END AS preferredName1,
CASE WHEN people2.preferredName IS NULL OR people2.preferredName = ""
THEN people2.firstName || ' ' || people2.lastName
ELSE people2.preferredName
END AS preferredName2,
FROM list
LEFT JOIN people people1 ON list.person1_id = people1.id
LEFT JOIN people people2 ON list.person2_id = people2.id

Related

How to count employees who are involved in a proct and list there names in one cell?

I try to write a query which shows me the number of employees who are involved in a project and also list there names in just one cell.
select p.projectname, count(main.projectnr) "Involved employees" from projects_karl p
join mainpro_karl main
on main.projectnr = p.projectnr
join employees_karl m
on m.employeenr = main.employeenr
group by p.beschreibung
;
This query counts the amount of employees who are working in a certain project.
But I also want to list the names of the involved employees.
select p.projectname, count(main.projectnr) "Anzahl beteiligter MAs", m.firstname || ' ' || m.surname "Name" from projects_karl p
join mainpro_karl main
on main.projectnr = p.projectnr
join Employees_karl m
on m.employeenr = main.employeenr
group by p.projectname, m.firstname || ' ' || m.surname
;
This query list involved employees but not in a cell but in a own row. And the count function does not works anymore. It just counts '1' in every row.
use listagg()
select p.projectname, count(main.projectnr) "Anzahl beteiligter MAs",
listagg(m.firstname || ' ' || m.surname, ',') within group (order by p.projectname) "Name"
from projects_karl p join mainpro_karl main on main.projectnr = p.projectnr
join Employees_karl m on m.employeenr = main.employeenr
group by p.projectname

Merge two SELECT statements with different data types and number of columns into one output in PostgreSQL

I have two queries. The first -
SELECT
communications.creation_date as message_date,
message as message_text,
employees.first_name || ' ' || coalesce(employees.middle_name,'') || ' ' || employees.last_name as message_by
FROM app.communications
INNER JOIN app.employees ON communications.message_from = employees.emp_id
WHERE send_as_sms = TRUE AND com_id = (SELECT MAX(com_id) FROM app.communications)
which basically outputs - | message_date | message_text | message_by |
And the second query -
SELECT
cs.com_id,
cs.first_name ||' ' || cs.last_name AS recipient_name,
cs.sim_number AS phone_number
FROM app.communication_sms cs
WHERE cs.com_id = (SELECT MAX(cs2.com_id) FROM app.communication_sms cs2)
ORDER BY first_name ASC
which outputs - | com_id | recipient_name | phone_number |
As you can tell from the queries, both tables have a "com_id" column. What I need is to make a single query that will merge the two queries above to get a single output, something like -
|message_date|message_text|message_by|recipient_name|phone_number|
How can I achieve that? I can't use UNION because of the different data types and different number of columns. I'll appreciate your help guys.
Not sure if the com_id will be equal or not, but in case they might not be then I suggest this:
select * -- list out the columns, I haven't bothered here
FROM (
SELECT MAX(com_id) as com_id FROM app.communications
UNION
SELECT MAX(cs2.com_id) FROM app.communication_sms cs2
) u
left join (
SELECT
com_id -- don't know which table this comes from
communications.creation_date as message_date,
message as message_text,
employees.first_name || ' ' || coalesce(employees.middle_name,'') || ' ' || employees.last_name as message_by
FROM app.communications
INNER JOIN app.employees ON communications.message_from = employees.emp_id
WHERE send_as_sms = TRUE AND com_id = (SELECT MAX(com_id) FROM app.communications)
) s1 on u.com_id = s1.com_id
left join (
SELECT
cs.com_id,
cs.first_name ||' ' || cs.last_name AS recipient_name,
cs.sim_number AS phone_number
FROM app.communication_sms cs
WHERE cs.com_id = (SELECT MAX(cs2.com_id) FROM app.communication_sms cs2)
ORDER BY first_name ASC
) s2 on u.com_id = s2.com_id
Note a small amount of repetition could be avoided by using CTEs
Is there a reason why you would need to union or join? In the context you are asking the question com_id should not matter.
Try something like this, ( this query is nothing special basically just merged the two together )
SELECT
communications.creation_date as message_date,
message as message_text,
employees.first_name || ' ' || coalesce(employees.middle_name,'') || ' ' || employees.last_name as message_by
cs.com_id,
cs.first_name ||' ' || cs.last_name AS recipient_name,
cs.sim_number AS phone_number
FROM app.communications, app.communication_sms cs
INNER JOIN app.employees ON communications.message_from = employees.emp_id
WHERE send_as_sms = TRUE AND com_id = (SELECT MAX(com_id) FROM app.communications)
AND cs.com_id = (SELECT MAX(cs2.com_id) FROM app.communication_sms cs2)
ORDER BY first_name ASC
First query: You want the last communication identified by MAX(c.com_id), but only in case it is an SMS (c.send_as_sms = TRUE). For this SMS you want the sender's name. This query results in one or zero rows depending on whether the last communication was an SMS.
Second query: You want the last SMSs for the last SMS communication this time identified by MAX(cs.com_id). This looks redundant. Why do you have a send_as_sms in communications when you see from the existence of matching communication_sms records that this is an SMS? You may want to think over this design. Anyway, from this you want to get the recipient's name plus some phone number.
How to combine the two queries is not evident. Do you really want the latest SMSs with the recipients' names, but only add the sender's name in case this last SMS is also the last communication? This doesn't seem likely. I guess you rather want either
the lastest communication in case its an SMS or
the latest SMS communication
for both the sender and recipient. Or can you have non-SMS communications with SMSs attatched?
Here is a query for the last SMS communication:
SELECT
c.creation_date AS message_date,
c.message AS message_text,
e.first_name || ' ' || coalesce(e.middle_name,'') || ' ' || e.last_name AS message_by,
cs.com_id,
cs.first_name ||' ' || cs.last_name AS recipient_name,
cs.sim_number AS phone_number
FROM (SELECT * FROM app.communication_sms ORDER BY com_id DESC FETCH FIRST ROW ONLY) cs
JOIN app.communications c ON c.com_id = cs.com_id
JOIN app.employees e ON c.message_from = e.emp_id
ORDER cs.first_name ASC;

SQL query columns spreading

I have the following SQL query and as we see in the screenshot, there are two repeating row with the same constr_id(2015) value but with different assigned_insurance_packages.
Query:
select
ac.constr_id,
AIP.NAME as ASSIGNED_INSURANCE_PACKAGES,
ac.code,ac.constr_name,
ac.offer_name,
ac.repay_freq,
ac.min_amt,
ac.max_amt,
ac.min_downpay_percent,
ac.max_downpay_percent,
ac.downpay_amount,
ac.min_term,
(select
listagg(AF.NAME, '; ') within group(order by ACF.CONSTR_ID)
from
CREDILOGIC.ACQ_CONSTR_FEE acf, CREDILOGIC.ACQ_FEE af
where
ACF.CONSTR_ID = AC.CONSTR_ID and AF.FEE_ID = ACF.FEE_ID) as CONSTRUCTION_FEES,
INS_COMPANY.SHORT_NAME,
(select listagg(x.DURATION_MIN || '-' || TO_CHAR(x.RATE_SHIFT, '90.99') || ',' || x.DURATION_MAX || '-' || TO_CHAR(x.RATE_SHIFT, '90.99'), '; ') within group (order by X.CONSTRUCTION_ID)
from credilogic.ACQ_CONSTRUCTION_BASERATE x
where X.CONSTRUCTION_ID = AC.CONSTR_ID) as RATE
from
CREDILOGIC.ACQ_CONSTRUCTION ac
left join
CREDILOGIC.ACQ_CONSTR_INSPACKAGE aci on ACI.CONSTR_ID = AC.CONSTR_ID
and ACI.DELETED_TSTAMP is null
left join
CREDILOGIC.ACQ_INSURANCE_PACKAGE aip on AIP.INSURANCE_PACKAGE_ID = ACI.INSURANCE_PACKAGE_ID
left join
credilogic.ath_party ins_company ON INS_COMPANY.PARTY_ID = aip.PARTY_ID
left join
credilogic.ACQ_CONSTRUCTION_DUEDATE acd on ac.constr_id = acd.constr_id
left join
credilogic.ACQ_APPLICATION acp on ac.constr_id = acp.construction_id
I am just going to create two additional columns like 0.3% insurance pack and 0.5% insurance pack to put ASSIGNED_INSURANCE_PACKAGES values into different columns like in the picture below

Check special character in sql view

Hi i am in fix for a particular scenario, i have a view which is created by joining multiple tables, the requirement is that i have to find whether the columns in that view will return special character.
SELECT SI.ShipmentId,
CASE
WHEN SA.AddressType = 1 THEN 'SH'
ELSE 'CN'
END AS AddressType,
SI.Pieces,
SI.PalletCount,
SI.Weight,
SI.UserDescription,
SI.Class,
SA.CompanyName,
SA.Street,
SA.City,
SA.State,
SA.ZipCode,
CASE
WHEN SA.Country = 1 THEN 'USA'
WHEN SA.Country = 2 THEN 'CANADA'
END AS Country,
SA.ContactPerson,
Cast(Replace(Replace(Replace(Replace(SA.Phone, ')', ''), '(', ''), '-', ''), ' ', '') AS VARCHAR(25)) AS Phone,
S.PoNo,
S.EstimatedDueDate,
Cast(S.ShipmentReadyTime AS VARCHAR(10)) AS ShipmentReadyTime,
Cast(S.ShipmentCloseTime AS VARCHAR(10)) AS ShipmentCloseTime,
B.BOLNumber,
S.HazMatEmergencyNo
FROM CarrierRate.Shipment AS S
INNER JOIN CarrierRate.BOL AS B
ON B.ShipmentId = S.ID
INNER JOIN CarrierRate.ShipmentItems AS SI
ON SI.ShipmentId = S.ID
INNER JOIN CarrierRate.ShipmentAddresses AS SA
ON SA.ShipmentId = S.ID
INNER JOIN CarrierRate.Carriers AS C
ON C.ID = S.CarrierId
WHERE ( SI.AccessorialId = 1 )
AND ( SA.AddressType IN ( 1, 2 ) )
This is that view, i just want to know what all columns will have special character as its data.
For E.g: i have SA.CompanyName as one of the column, i have to check whether that column can be filled with special characters?
Please let me know the probable solutions, i am clueless.
You need to look at the column types of the tables/columns/expressions which contribute to the columns of the view you are interested in. Assuming they are defined as some form of text or varchar then they CAN contain special characters, unless there exists some form of constraint on those columns/tables.

issues combining two columns using concat

I am having issues with combining two columns into the one using mssql
table 1 format:
|| WJCPrefix || WJCNo ||
|| UK-R/SWJC/14/ || 1234 ||
|| UK-R/CUWJC/14/ || 2345 ||
|| UK-R/CUWJC/14/ || 3456 ||
|| UK-R/SWJC/14/ || 4567 ||
|| UK-R/CUWJC/14/ || 5678 ||
The desired out would be:
UK-R/CUWJC/14/3456
UK-R/CUWJC/14/5678
the sql statement i am using is:
SELECT tblWJCItem.AddedDescription, concat(tblWJC.WJCPrefix, tblWJC.WJCNo) AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;
I've also used:
SELECT tblWJCItem.AddedDescription, tblWJC.WJCPrefix + '' + tblWJC.WJCNo AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;
I can't seem to connect these two columns could anyone point out what I am doing wrong here?
Thanks
Your first query (Concat() function) should work if you are using SQL Server 2012 or later.
For other versions, you may need to Convert()/Cast() WJCNo to a string type
SELECT t2.AddedDescription,
t1.WJCPrefix + CONVERT(Varchar(10),t1.WJCNo) AS OurRef
FROM tblWJC t1
INNER JOIN tblWJCItem t2 ON t1.WJCID = t2.WJCID;
Your first query should be fine. But you might try:
SELECT tblWJCItem.AddedDescription, tblWJC.Prefix + cast(tblWJC.WJCNo as varchar(255)) AS OurRef
FROM tblWJC INNER JOIN
tblWJCItem
ON tblWJC.WJCID = tblWJCItem.WJCID;
You might get an error in the second version if WJCno is numeric rather than a string.
I think WJCNo is numeric or int field so Convert this field to Varchar first then concat:-
SELECT tblWJCItem.AddedDescription,
tblWJC.WJCPrefix + '' + CONVERT(Varchar(10),tblWJC.WJCNo) AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;