Skip to main content

Right approach to use join for particular scenario in sql



I have a table that looks like this:

CREATE TABLE #C (grpType varchar(10), CPTCode varchar(10) NULL, Month int NULL, MTD money NULL, MonthCount int NULL, YTD money NULL, YearCount int NULL, Code varchar(10) NULL)


It has the following data in it:
grpType CPTCode Month MTD MonthCount YTD YearCount Code
Month 76800 5 1321.61 27 6574.54 82 76800
Month 76856 5 246.01 3 380.64 6 76856
Month 76881 5 9778.95 131 50682.59 509 76881
Month 76942 5 22467.33 190 116663.58 674 76942



Then I have another table:

CREATE TABLE #Prod (grpType varchar(10), TotalCharges money NULL, TotalUnits float NULL, RVU float NULL, Code varchar(10) NULL, CPTCode varchar(10) NULL)


This table has the following data in it:

grpType TotalCharges TotalUnits RVU Code CPTCode
Month 6100.00 12 0 76800 76800
Month -475.00 -1 0 76880 76880
Month 38749.00 81 0 76881 76881
Month 54733.00 114 0 76942 76942

What I need to do with these tables is join them so that every CPTCode shows and I get the MTD, TotalCharges and TotalUnits along with the CPTCodes. So my end data should look like this:

CPTCode MTD TotalCharges TotalUnits
76800 1321.61 6100.00 12
76856 246.01 NULL NULL
76880 NULL -475.00 -1
76881 9778.95 38749.00 81
76942 22497.33 54733.00 114
Solution:

 What would be the best join to accomplish this? I have tried the left join and it didn't come out correctly. So I am a little lost. I also thought a pivot query may be what I need?

Based on sample results, we want a result for any CPTCode that appears in either table, with the appropriate fields (the fields from the other table) set to NULL if that table doesn't have a record for the same CPTCode.

My first reaction is that you need a UNION of two queries: a LEFT OUTER JOIN and a RIGHT OUTER JOIN, where each of the sub-queries has a WHERE clause to exclude records where the secondary table isn't NULL. However, that will exclude those CPTCodes that do appear in both tables.

So, one approach is to use three sub-queries: a LEFT OUTER JOIN and a RIGHT OUTER JOIN as described above, plus an INNER JOIN to get the matching records:
SELECT CPTCode, MTD, TotalCharges, TotalUnits
FROM
(SELECT #C.CPTCode, #C.MTD, #Prod.TotalCharges, #Prod.TotalUnits
FROM #C LEFT OUTER JOIN #Prod
ON #C.CPTCode = #Prod.CPTCode
WHERE #Prod.CPTCode IS NULL)
UNION
(SELECT #C.CPTCode, #C.MTD, #Prod.TotalCharges, #Prod.TotalUnits
FROM #C RIGHT OUTER JOIN #Prod
ON #C.CPTCode = #Prod.CPTCode
WHERE #C.CPTCode IS NULL)
UNION
(SELECT #C.CPTCode, #C.MTD, #Prod.TotalCharges, #Prod.TotalUnits
FROM #C INNER JOIN #Prod
ON #C.CPTCode = #Prod.CPTCode)

Comments

Popular posts from this blog

Resolved : Power BI Report connection error during execution

Getting Below Power BI Report connection error during execution . Error: Something went wrong Unable to connect to the data source undefined. Please try again later or contact support. If you contact support, please provide these details. Underlying error code: -2147467259 Table: Business Sector. Underlying error message: AnalysisServices: A connection cannot be made. Ensure that the server is running. DM_ErrorDetailNameCode_UnderlyingHResult: -2147467259 Microsoft.Data.Mashup.ValueError.DataSourceKind: AnalysisServices Microsoft.Data.Mashup.ValueError.DataSourcePath: 10.10.10.60;T_CustomerMaster_ST Microsoft.Data.Mashup.ValueError.Reason: DataSource.Error Cluster URI: WABI-WEST-EUROPE-redirect.analysis.windows.net Activity ID: c72c4f12-8c27-475f-b576-a539dd81826a Request ID: dfb54166-c78f-4b40-779f-e8922a6687ad Time: 2019-09-26 10:03:29Z Solution: We found report connection not able to connect to SQL Analysis service so tried below option. ...

Song- Khamoshiyan Piano keyboard Chord,Notation and songs Lyrics

Song Aankhen Khuli Ho lyrics notation

Song : Aankhen Khuli Ho Movie: Mohabbatein Notes used : W=>Western - C D E F G- A- B-/ H=>Hindustani - S R G M P- D- N- ( Here for western, G=G-, A=A-, & B=B- ) ( For hindustani, P=P-, D=D-, & N=N- ) Song I : Aankhen Khuli...Ho Ya.. Ho Bandh W=> A.... C... B..C.. E.. E...... A... A.... H=> D... S... N..S.. G G....... D... D.... Deedaar Un Ka Ho.o.taa Hai.. W=> A...B....A....D.BAG....ADB... H=> D...N...D.....R.NDP...DRN... Kaise Kahoon Main O..Yaaraa W=> B..D.. D....E.... D.....C..C..C... H=> N..R.. R....G... R.....S..S..S..... Ye Pyaar Kaise Hota Hai W=> E...B.....DB...AG...B..AA H=> G...N....RN...DP...N...DD (Tururu ru ru, ru ru rururu ru......) W=> AA...GA...BCE..., B...DB..GA H=> DD...PD...NSG..., N..RN.. PD Song II: Aa.aj He Kisi..par Yaa.ro.on..., Marke De..Khe..gein Hum W=> E....FEDCBABC.D.. D D......., G A B C.... E.......D...D..... H=> G....MGRSNDNS.R. R R......., P D N S.....G........R...R.... Pyaar Ho...