This is my following question please have a look in the code snippet.
Hi, I have two tables Table A, Table B as follows:
**Table A** **Table B**
Reg Id (ex: 217) Session Name (ex: Section A, Section B)
First Choice
Second Choice
Third Choice
Reg Id
I need the result as follows:
**Reg id** **Section A** **Section B** **Section C**
217 First Choice First Choice First Choice
Second Choice Second Choice Second Choice
Third Choice Third Choice Third Choice
Here is how I wold do it. Join for each column.
SELECT A.RegID,
BSecA.FirstChoice || ', ' || BSecA.SecondChoice || ', ' || BSecA.ThirdChoice as SectionA,
BSecB.FirstChoice || ', ' || BSecB.SecondChoice || ', ' || BSecB.ThirdChoice as SectionB,
BSecC.FirstChoice || ', ' || BSecC.SecondChoice || ', ' || BSecC.ThirdChoice as SectionC
FROM TableA AS A
LEFT JOIN TableB AS BsecA ON A.RegID = BsecA.RegID AND BsecA.SectionName = "Section A"
LEFT JOIN TableB AS BsecB ON A.RegID = BsecB.RegID AND BsecB.SectionName = "Section B"
LEFT JOIN TableB AS BsecC ON A.RegID = BsecC.RegID AND BsecC.SectionName = "Section C"
Related
I am working claims data and repricing the claims according to a different fee schedule. So I have two tables. The claims and fschedule. In this case, the repricing boils down to CPT_Code and Modifier. So I am conditionally joining them on
claims.CPT = fschedule.CPT
AND
claims.mod = fschedule.mod
Here is how the fschedule looks:
CPT || Modifier || Price
77325 || 26 || 73.25
77325 || TC || 52.77
77325 || XX || 101.21
77333 || XX || 12.31
However, my problem is that claims table may have:
CPT || Modifier
77333 || TC
But the fschedule may have 77333 but not with a TC or 26 only XX. So I want the tables to join on both
claims.CPT = fschedule.CPT
AND
claims.mod = f.schedule.mod
but if there is no match in the fschedule then I want it to match when fschedule modifier XX
I am trying to conditionally join the two tables using case statements.
FROM
claims as c
LEFT JOIN
fschedule as f
ON
CASE WHEN c.mod IN ('TC','26') THEN
CASE WHEN f.mod IN ('TC','26') AND c.cpt= f.cpt AND c.mod = f.mod THEN c.cpt = f.cpt AND c.mod = f.mod
WHEN f.mod NOT IN ('TC','26') AND c.cpt= f.cpt AND c.mod <> f.mod THEN c.cpt = f.cpt AND c.mod <> f.mod
END
WHEN c.mod NOT IN ('TC','26') AND f.mod NOT IN ('TC','26') THEN c.cpt = f.cpt AND c.mod = f.mod
END;
I need to use a conditional left join like:
Claims as c
LEFT JOIN
fschedule as f
ON
c.CPT = f.CPT
AND
c.mod = f.mod
However, if the claims table has:
CPT || Modifier || Paid
77333 || TC || 7.88
But the fschedule only has:
CPT || Modifier || Price
77333 || XX || 12.31
I need the claims table to still output the price of 12.31 found in the fschedule table. The XX modifier means no modifier exists.
Claims table before joining it to the fschedule table has 22,124 rows of data. After joining them with my current code the table outputs 25,283 rows of data. I need the output to stay 22,124.
In short, I need the two tables to join together on both:
c.cpt = f.cpt
AND
c.mod = f.mod
but if the claims table has a CPT code and modifier, but the fschedule only has the CPT code and no modifier match, then I need to force the fschedule to output the the price with the modifier XX since it does not have TC or 26 modifier.
Thank you for your help.
*************Updated issue**************
Claims table:
CPT || Mod || Paid
77067 || TC || 83.10
Fees Table
CPT || Mod || Price
77067 || 26 || 76.23
77067 || XX || 103.01
So even though the claims table has a modifier TC but fees table has the code but not the modifier, I still want it to connect on the claims table using the fees table when the modifier = XX. So I would want the end result looking like:
CPT || Mod || Paid || Fees_Price || Fees_Mod
77067 || TC || 83.10 || 103.01 || XX
As of right now this is the output:
CPT || Mod || Paid || Fees_Price || Fees_Mod
77067 || TC || 83.10 || ||
Thanks
A much simpler ON condition can do the trick:
ON (c.CPT = f.CPT AND c.mod = f.mod) OR (c.CPT = f.CPT AND f.mod ='xx' AND f.CPT NOT IN (SELECT CPT FROM Fees WHERE mod<>'xx'))
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;
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
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;
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