Two select statements turn into one - sql

I have set of two queries. In first query, if is separate from second, I got good results.
First query
SELECT *
FROM
(
SELECT nks.[Id]
, nks.[IdNarudzbe]
, nks.[IdArtikla] as artikal
, nks.[IdUsluge]
, nks.[Naziv]
, nks.Kolicina
, p.Naziv as kupac
, p.Id as kupacId
, p.Adresa
, p.Telefon
, nkz.[BrojDokumenta] AS nalog
, nkz.[BrojDokumentaKroz] AS nalogKroz
, nkz.[RokIsporuke]
, nkz.[IdNastaloOdDokumenta]
, d.Naziv as drzava
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0
--...
from first query nkz.[IdNastaloOdDokumenta] is important to second
SELECT BrojDokumenta
, BrojDokumentaKroz
FROM .[dbo].[NarudzbeKupacaZaglavlje]
where id = nkz.[IdNastaloOdDokumenta]
For ex. In first query I got nkz.[IdNastaloOdDokumenta] = 20. Number 20 I use in second query in where statement, and value I get from BrojDokumenta, I would like to join to first query.
I was wondering if is possible to make one query out of these two. I think I can not union operator because number of column from these two queries don't match.

The same table, and the same columns are already in the first query. Perhaps you want a self-join, like this:
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz2
ON nkz2.Id = nkz.[IdNastaloOdDokumenta]
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0

Related

Which query is faster to execute

Following are two options to query CTE and NFSE data. Pls advise !
A similar approach may have to be used in many places.
I would like you to verify them and suggest, if there are better options.
Option 1: Query CTE with all necessary joins UNION with query on NFSe with all necessary joins
SELECT DISTINCT 'CTE' as docTypeID
, cte.isqn_mstr_cd as ctePk
, cte.ct_e_cd
, cter.stat_desc
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM cte_identity_master cte
INNER JOIN cte_response_detail cter ON (cte.isqn_mstr_cd = cter.isqn_mstr_cd)
LEFT JOIN match_ref_awb mawb ON (cte.isqn_ref_cd = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (cte.isqn_ref_cd = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
UNION
SELECT DISTINCT 'NFSE' as docTypeID
, nfse.isqn_mstr_cd as nfsePk
, nfse.rp_s_id
, nfser.stat_desc
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM nfse_request_detail nfse
INNER JOIN nfse_response_detail nfser ON (nfse.isqn_mstr_cd = nfser.isqn_mstr_cd)
LEFT JOIN match_ref_awb mawb ON (nfse.isqn_ref_cd = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (nfse.isqn_ref_cd = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
;
Option 2: Use Union of CTe and NFSe first and then apply joins with other tables
SELECT ctnf.*
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM (
SELECT DISTINCT 'CTE' as docTypeID
, cte.isqn_mstr_cd as docPk
, cte.ct_e_cd as docNbr
, cter.stat_desc as docStat
, cte.isqn_ref_cd as matchRef
FROM cte_identity_master cte
INNER JOIN cte_response_detail cter ON (cte.isqn_mstr_cd = cter.isqn_mstr_cd)
UNION
SELECT DISTINCT 'NFSE' as docTypeID
, nfse.isqn_mstr_cd as nfsePk
, nfse.rp_s_id
, nfser.stat_desc
, nfse.isqn_ref_cd
FROM nfse_request_detail nfse
INNER JOIN nfse_response_detail nfser ON (nfse.isqn_mstr_cd = nfser.isqn_mstr_cd)
) ctnf
LEFT JOIN match_ref_awb mawb ON (ctnf.matchRef = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (ctnf.matchRef = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
;
Both approaches of the above will have following Where Clause:
WHERE lower(cte.sttn_cd) = lower(:stationId)
and (:documentType is null or lower(:documentType) = 'cte')
and (:shipperName is null or lower(awb.shipperNm) like lower(concat(concat('%',:shipperName),'%')))
and (:awbCreated is null or to_char(awb.createDt, 'MM-DD-YYYY') = :awbCreated)
and (:awbNumber is null or m2.awbNbr like concat(concat('%',:awbNumber),'%'))
and (:serviceType = 0 or awbs.baseServiceCd = :serviceType)
and (:commitmentDate is null or awbs.commitmentDate = :commitmentDate)
and (:ursa is null or lower(awbs.ursaCd) like lower(concat(concat('%',:ursa),'%')))
and (:destLocationId is null or lower(awbs.destLocCd) like lower(concat(concat('%',:destLocationId),'%')))
and (:nfeNumber is null or nfe.fiscalDocumentNumber like concat(concat('%',:nfeNumber),'%'))
PLEASE SUGGEST - Output of these two approaches to get data which one will be better to fix at Java End to retrieve the data.
Any help will be greatly appreciated. Also suggest if there is any other better query apart from these two!

GROUP BY with MAX()

I am new to sql and I have a problem to resolve so i need some help please
For example, during the week 04/2019, 720 mutations are missing compared to the real information
In fact the problem comes from query . Indeed in this one a group by CODE_COMMUNE and NOM_SITE is made and the value MAX of the fields is returned for this commune and this site. This means that when there are several mutations on the same site of the same commune, only one mutation is returned, which explains the missing mutations.
So i would like to return all the mutation on the same site of the same commune.
How can i do that ?
`select IPO_VILLE.CODE_COMMUNE,
IPO_SITE.NOM_SITE,
MAX(IPO_AT.DATE_MUTATION) DATE_MUTATION,
MAX(IPO_AT.IAR_NDFICTIF) IAR_NDFICTIF,
MAX(IPO_AT.MUTATION_REALISEE_PAR) MUTATION_REALISEE_PAR,
MAX(IPO_AT.CAUSE_MUTATION) CAUSE_MUTATION,
MAX(IPO_AT.CODE_BASICAT) CODE_BASICAT,
MAX(IPO_AT.PIGTAIL_FIBRE_CONN_COURANT) FIBRE_COURANT,
MAX(IPO_AT.PIGTAIL_FIBRE_CONN_CIBLE) FIBRE_CIBLE,
MAX(IPO_AT.NOM_PB_COURANT) NOM_PB_COURANT,
MAX(IPO_AT.NOM_PB) NOM_PB_CIBLE,
MAX(IPO_AT.NOM_PM_COURANT) NOM_PM_COURANT,
MAX(IPO_AT.NOM_PM) NOM_PM_CIBLE,
MAX(IPO_AT.INTERVENTION_NUMBER) INTERVENTION_NUMBER,
MAX(IPO_AT.GPC_BASE) GPC_BASE
from EDRPRE.IPO_AT IPO_AT
left join EDRPRE.IPO_LOGEMENT IPO_LOGEMENT ON IPO_AT.OBJECTID_LOGEMENT = IPO_LOGEMENT.OBJECTID_LOGEMENT
left join EDRPRE.IPO_SITE IPO_SITE ON IPO_LOGEMENT.OBJECTID_IMMEUBLE = IPO_SITE.OBJECTID_SITE
left join EDRPRE.IPO_RUE IPO_RUE ON IPO_SITE.OBJECTID_RUE = IPO_RUE.OBJECTID_RUE
left join EDRPRE.IPO_VILLE IPO_VILLE ON IPO_RUE.OBJECTID_COMMUNE = IPO_VILLE.OBJECTID_COMMUNE
where IPO_AT.DATE_MUTATION is not null and IPO_AT.DATE_MUTATION >= TRUNC(sysdate-5, 'DD')
and IPO_VILLE.CODE_COMMUNE is not null and IPO_SITE.NOM_SITE is not null
group by IPO_VILLE.CODE_COMMUNE,IPO_SITE.NOM_SITE
order by IPO_VILLE.CODE_COMMUNE,IPO_SITE.NOM_SITE;`
try this to get all records:
select IPO_VILLE.CODE_COMMUNE,
IPO_SITE.NOM_SITE,
IPO_AT.DATE_MUTATION ,
IPO_AT.IAR_NDFICTIF ,
IPO_AT.MUTATION_REALISEE_PAR ,
IPO_AT.CAUSE_MUTATION ,
IPO_AT.CODE_BASICAT ,
IPO_AT.PIGTAIL_FIBRE_CONN_COURANT ,
IPO_AT.PIGTAIL_FIBRE_CONN_CIBLE ,
IPO_AT.NOM_PB_COURANT ,
IPO_AT.NOM_PB ,
IPO_AT.NOM_PM_COURANT ,
IPO_AT.NOM_PM ,
IPO_AT.INTERVENTION_NUMBER ,
IPO_AT.GPC_BASE
from EDRPRE.IPO_AT IPO_AT
left join EDRPRE.IPO_LOGEMENT IPO_LOGEMENT ON IPO_AT.OBJECTID_LOGEMENT = IPO_LOGEMENT.OBJECTID_LOGEMENT
left join EDRPRE.IPO_SITE IPO_SITE ON IPO_LOGEMENT.OBJECTID_IMMEUBLE = IPO_SITE.OBJECTID_SITE
left join EDRPRE.IPO_RUE IPO_RUE ON IPO_SITE.OBJECTID_RUE = IPO_RUE.OBJECTID_RUE
left join EDRPRE.IPO_VILLE IPO_VILLE ON IPO_RUE.OBJECTID_COMMUNE = IPO_VILLE.OBJECTID_COMMUNE
where IPO_AT.DATE_MUTATION is not null and IPO_AT.DATE_MUTATION >= TRUNC(sysdate-5, 'DD')
and IPO_VILLE.CODE_COMMUNE is not null and IPO_SITE.NOM_SITE is not null
order by IPO_VILLE.CODE_COMMUNE,IPO_SITE.NOM_SITE;

Need to select ONLY if visiting one location

In the following SQL, it was looking at only those vouchers from location_ID = 5. How can I code that, I ONLY want from a Patient_ID who ONLY visited location_ID 5?
SELECT "Vouchers"."Patient_ID", "vwGenPatInfo"."Patient_Number",
"Practitioners"."Practitioner_ID", "Practitioners"."First_Name",
"Practitioners"."Last_Name", "vwGenPatInfo"."Patient_First_Name",
"vwGenPatInfo"."Patient_Last_Name", "vwGenPatInfo"."Patient_DOB",
"vwGenPatInfo"."Patient_Sex", "Vouchers"."Carrier_ID",
"Vouchers"."Billing_Date", "Vouchers"."Patient_Policy_ID",
"Vouchers"."Location_ID"
FROM ("Ntier_70751"."PM"."vwGenPatInfo" "vwGenPatInfo"
INNER JOIN "Ntier_70751"."PM"."Vouchers" "Vouchers"
ON "vwGenPatInfo"."Account_ID"="Vouchers"."Account_ID")
INNER JOIN "Ntier_70751"."PM"."Practitioners" "Practitioners"
ON "Vouchers"."Actual_Prov_Practitioner_ID"="Practitioners"."Practitioner_ID"
--
WHERE "Vouchers"."Location_ID"=5
Here is one way to do this. I also got rid of all those unneeded double quotes and used proper aliases.
SELECT V.Patient_ID
, gpi.Patient_Number
, P.Practitioner_ID
, P.First_Name
, P.Last_Name
, gpi.Patient_First_Name
, gpi.Patient_Last_Name
, gpi.Patient_DOB
, gpi.Patient_Sex
, V.Carrier_ID
, V.Billing_Date
, V.Patient_Policy_ID
, V.Location_ID
FROM Ntier_70751.PM.vwGenPatInfo gpi
INNER JOIN Ntier_70751.PM.Vouchers V ON gpi.Account_ID = V.Account_ID
INNER JOIN Ntier_70751.PM.Practitioners P ON V.Actual_Prov_Practitioner_ID = P.Practitioner_ID
cross apply
(
select V2.Account_ID
from Ntier_70751.PM.Vouchers V2
where V2.Account_ID = V.Account_ID
group by V2.Account_ID
HAVING MAX(Location_ID) = 5
AND MIN(Location_ID) = 5
) x
Put a condition as say;
WHERE "Vouchers"."Location_ID" = 5
I would go with not exists
SELECT "Vouchers"."Patient_ID", "vwGenPatInfo"."Patient_Number",
"Practitioners"."Practitioner_ID", "Practitioners"."First_Name", "Practitioners"."Last_Name", "vwGenPatInfo"."Patient_First_Name", "vwGenPatInfo"."Patient_Last_Name", "vwGenPatInfo"."Patient_DOB", "vwGenPatInfo"."Patient_Sex", "Vouchers"."Carrier_ID", "Vouchers"."Billing_Date", "Vouchers"."Patient_Policy_ID", "Vouchers"."Location_ID"
FROM "Ntier_70751"."PM"."vwGenPatInfo" "vwGenPatInfo" INNER JOIN
"Ntier_70751"."PM"."Vouchers" "Vouchers"
ON "vwGenPatInfo"."Account_ID" = "Vouchers"."Account_ID" INNER JOIN
"Ntier_70751"."PM"."Practitioners" "Practitioners"
ON "Vouchers"."Actual_Prov_Practitioner_ID" = "Practitioners"."Practitioner_ID"
WHERE "Vouchers"."Location_ID"=5
and not exists (select 1
FROM "Ntier_70751"."PM"."Vouchers" "Vouchers2"
WHERE "Vouchers2"."Patient_ID" = "Vouchers2"."Patient_ID"
AND "Vouchers2"."Location_ID"<>5)
Just using the condition 'WHERE "Vouchers"."Location_ID" = 5'will return all Patient_IDs that visited the that location at least once but not exclusively. There are several ways to do it but the cleanest would be using having max (location_id) <5 and min (location_id) >5

How do I remove duplicate results by looking for max date?

I know I have duplicate results from this query because the tables ReleaseHistory and IterationHistory have multiple records per ReleaseID and IterationID. I would like to only select the records with max date from dbo.ReleaseHistory and dbo.IterationHistory. How would I do that in this query? SQL SERVER 2008
SELECT dbo.Assignable.AssignableID AS ID,
dbo.EntityType.Abbreviation AS Entity,
dbo.General.Name, dbo.Assignable.Effort,
dbo.Assignable.EffortCompleted,
dbo.Assignable.EffortToDo,
dbo.EntityState.Name AS State,
dbo.ReleaseHistory.Name AS Release,
dbo.IterationHistory.Name AS Iteration,
dbo.General.CustomField3 AS [Scrum Team]
FROM dbo.Assignable INNER JOIN
dbo.General ON dbo.Assignable.AssignableID =
dbo.General.GeneralID INNER JOIN
dbo.EntityType ON dbo.General.EntityTypeID =
dbo.EntityType.EntityTypeID INNER JOIN
dbo.EntityState ON dbo.Assignable.EntityStateID =
dbo.EntityState.EntityStateID AND
dbo.EntityType.EntityTypeID =
dbo.EntityState.EntityTypeID INNER JOIN
dbo.ReleaseHistory ON dbo.Assignable.ReleaseID =
dbo.ReleaseHistory.ReleaseID INNER JOIN
dbo.IterationHistory ON
dbo.Assignable.IterationID =
dbo.IterationHistory.IterationID LEFT OUTER JOIN
dbo.CustomField ON dbo.General.CustomField3 =
dbo.CustomField.CustomFieldID
WHERE (dbo.Assignable.ProjectID = 4054)
GROUP BY dbo.Assignable.AssignableID,
dbo.EntityType.Abbreviation,
dbo.General.Name,
dbo.Assignable.Effort,
dbo.Assignable.EffortCompleted,
dbo.Assignable.EffortToDo,
dbo.EntityState.Name,
dbo.ReleaseHistory.Name,
dbo.IterationHistory.Name,
dbo.General.CustomField3
Brian,
I am amusing you are doing this in MS SQL and there will always be at least one record in ReleaseHistory and IterationHistory tables. If assumptions are correct then you can simply use CROSS APPLY to get top 1 record from both tables.
SELECT
dbo.Assignable.AssignableID AS ID ,
dbo.EntityType.Abbreviation AS Entity ,
dbo.General.Name ,
dbo.Assignable.Effort ,
dbo.Assignable.EffortCompleted ,
dbo.Assignable.EffortToDo ,
dbo.EntityState.Name AS State ,
Release ,
Iteration ,
dbo.General.CustomField3 AS [Scrum Team]
FROM
dbo.Assignable
INNER JOIN dbo.General ON dbo.Assignable.AssignableID = dbo.General.GeneralID
INNER JOIN dbo.EntityType ON dbo.General.EntityTypeID = dbo.EntityType.EntityTypeID
INNER JOIN dbo.EntityState ON dbo.Assignable.EntityStateID = dbo.EntityState.EntityStateID
AND dbo.EntityType.EntityTypeID = dbo.EntityState.EntityTypeID
CROSS APPLY( SELECT TOP 1 name Release FROM ReleaseHistory WHERE ReleaseID = Assignable.ReleaseID ORDER BY MaxDateColumn) a
CROSS APPLY( SELECT TOP 1 name Iteration FROM IterationHistory WHERE IterationID = Assignable.IterationID ORDER BY MaxDateColumn) b
LEFT OUTER JOIN dbo.CustomField ON dbo.General.CustomField3 = dbo.CustomField.CustomFieldID
WHERE
( dbo.Assignable.ProjectID = 4054 )
GROUP BY
dbo.Assignable.AssignableID ,
dbo.EntityType.Abbreviation ,
dbo.General.Name ,
dbo.Assignable.Effort ,
dbo.Assignable.EffortCompleted ,
dbo.Assignable.EffortToDo ,
dbo.EntityState.Name ,
dbo.ReleaseHistory.Name ,
dbo.IterationHistory.Name ,
dbo.General.CustomField3

SQL Sub-select as field?

I'm a bit lost here...
I have several tables I'd like to pull a unified record from: Unit, Building, Owner, and Picture.
Here's my query so far:
SELECT building.`Street_Address`
, building.`Building_Name`
, building.`Building_Type`
, CONCAT(building.`Cross_Street_1`, ' & ', building.`Cross_Street_2`) Cross_Streets
, building.`Cross_Street_1`
, building.`Cross_Street_2`
, building.`Access` Building_Access
, owner.`Company_Name`
, owner.`Contact_Or_Reference`
, owner.`Landlord_Phone`
, picture.`Path_To_Picture_On_Server`
, picture.`Picture_Category`
, unit.`Apartment_Number`
, unit.`Unit_Size_Number` Size
, unit.`Is_Doorman`
, unit.`Is_Furnished`
, unit.`Is_Elevator`
, unit.`Is_Pets`
, unit.`Is_OutdoorSpace`
, unit.`Rent_Price`
, unit.`Baths`
, unit.`Access` Unit_Access
, unit.`fourd_id`
, unit.`Updated_Date`
, unit.`Occupancy_Date`
, unit.`Term`
, unit.`Incentives`
, unit.`Info_OutdoorSpace`
, unit.`List_Date`
, zone.`Description`
FROM 4D_Units unit
JOIN 4D_Building building
ON unit.`BUILDING_RecID` = building.`fourd_id`
JOIN 4D_Zones zone
ON building.`ZONES_RecID` = zone.`fourd_id`
LEFT JOIN 4D_Owners owner
ON unit.`OWNER_RecID` = owner.`fourd_id`
LEFT JOIN 4D_Building_Picts picture
ON (building.`fourd_id` = picture.`BUILDING_RecID` AND picture.`Picture_Category` = 'Front')
WHERE unit.`id` = 49901
This works fine as-is, except that the return record will only ever have the "Front" picture in the record (if present). My issue is that there are several different types of photos that could be associated with a return record, including 'Panorama', 'Interior', and 'Floorplan'... all are different possible values for picture.Picture_Category.
Is there a way to return those values (if they are present, as above) in the returned set without doing a separate query? I want the returned set to include (if present) aliased values for all four possible options of picture.Picture_Category: 'Front', 'Panorama', 'Interior', & 'Floorplan' (with their own unique picture.Path_To_Picture_On_Server associated with it).
Does that make sense?
If I understand you correctly, you want to have 4 sets of picture columns in your result set - one of reach of 4 categories? Right now you have just one for front, right?
You can join to the same table multiple times with different aliases and different join clauses. Just join to 4D_Building_Picts 4 times, once for each picture you want.
select
--whatever
, pic_front.`Path_To_Picture_On_Server` AS Front_Path_To_Picture_On_Server
, pic_panorama.`Path_To_Picture_On_Server` AS Panorama_Path_To_Picture_On_Server
, pic_interior.`Path_To_Picture_On_Server` AS Interior_Path_To_Picture_On_Server
, pic_floorplan.`Path_To_Picture_On_Server` AS Floorplan_Path_To_Picture_On_Server
--whatever
FROM 4D_Units unit
JOIN 4D_Building building
ON unit.`BUILDING_RecID` = building.`fourd_id`
JOIN 4D_Zones zone
ON building.`ZONES_RecID` = zone.`fourd_id`
LEFT JOIN 4D_Owners owner
ON unit.`OWNER_RecID` = owner.`fourd_id`
LEFT JOIN 4D_Building_Picts pic_front
ON (building.`fourd_id` = pic_front.`BUILDING_RecID` AND pic_front.`Picture_Category` = 'Front')
LEFT JOIN 4D_Building_Picts pic_panorama
ON (building.`fourd_id` = pic_panorama.`BUILDING_RecID` AND pic_panorama.`Picture_Category` = 'Panorama')
LEFT JOIN 4D_Building_Picts pic_interior
ON (building.`fourd_id` = pic_interior.`BUILDING_RecID` AND pic_interior.`Picture_Category` = 'Interior')
LEFT JOIN 4D_Building_Picts pic_floorplan
ON (building.`fourd_id` = pic_floorplan.`BUILDING_RecID` AND pic_floorplan.`Picture_Category` = 'Floorplan')
WHERE unit.`id` = 49901
I think you want the coalesce function. It takes multiple fields, and returns the first of them that's non-null. So something like:
Select
Coalesce(A.Panorama, A.Interior, A.Floorplan, '') as ImagePath
From
Table A
You only end up with one value this way though, which may not actually be what you're after. If you want all of them I'd suggest using correlated subqueries, like so:
Select
(Select P.Path_To_Picture From 4D_Building_Picts P where P.Building_RecID = B.fourd_Id And P.Picture_Category = 'Front') as Front_Pic,
(Select P.Path_To_Picture From 4D_Building_Picts P where P.Building_RecID = B.fourd_Id And P.Picture_Category = 'Panorama') as Panamora_Pic,
(Select P.Path_To_Picture From 4D_Building_Picts P where P.Building_RecID = B.fourd_Id And P.Picture_Category = 'FloorPlan') as FloorPlan_Pic,
...
From
4D_Building B