Home

SQL Server Join Question

%3CLINGO-SUB%20id%3D%22lingo-sub-331892%22%20slang%3D%22en-US%22%3ESQL%20Server%20Join%20Question%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-331892%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20new%20to%20the%20forum%20so%20if%20this%20is%20posted%20in%20the%20wrong%20place%2C%20please%20point%20me%20to%20the%20correct%20spot.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20table%20that%20has%20the%20following%20columns%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EState%26nbsp%3B%20County%20%26nbsp%3B%26nbsp%3B%20Rep%20%26nbsp%3B%20%26nbsp%3B%20Amount%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20001%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20100%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24300.00%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20001%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2022.50%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20002%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20300%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24750.00%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20002%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2012.75%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20020%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20400%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24450.00%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20020%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24%26nbsp%3B%2057.00%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20030%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20111%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24645.00%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20other%20rep%20data%20is%20on%20a%20separate%20row%20with%20a%20code%20of%20999.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20I%20need%20to%20do%20is%20create%20output%20that%20looks%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EState%26nbsp%3B%20County%20%26nbsp%3B%26nbsp%3B%20Rep%20%26nbsp%3B%20%26nbsp%3B%20Amount%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Other%20Rep%20%26nbsp%3B%26nbsp%3B%20Other%20Amount%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20001%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20100%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24300.00%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%3E999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2022.50%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20002%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20300%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24750.00%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2012.75%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20020%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20400%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24450.00%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%3CSPAN%3E999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24%26nbsp%3B%2057.00%3C%2FSPAN%3E%26nbsp%3B%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20030%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20111%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24645.00%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%200%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%24%26nbsp%3B%200.00%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20other%20words%2C%20I%20want%20to%20place%20the%20other%20rep%20data%20beside%20the%20Rep%20data%20in%20the%20table%20so%20that%20there%20is%20only%201%20row%20per%20State%2FCounty%20Combo.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20seems%20simple%20but%20as%20a%20novice%20I%20need%20some%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20appreciate%20any%20assistance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMEG_AGI%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-652009%22%20slang%3D%22en-US%22%3ERe%3A%20SQL%20Server%20Join%20Question%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-652009%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F279495%22%20target%3D%22_blank%22%3E%40MEG_AGI%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20use%20below%20query%3C%2FP%3E%3CPRE%3E%3CBR%20%2F%3EDrop%20TABLE%20IF%20EXISTS%20%23temp%3CBR%20%2F%3EGo%3CBR%20%2F%3EDrop%20TABLE%20IF%20EXISTS%20%23tempfinal%3CBR%20%2F%3EGO%3CBR%20%2F%3E%3CBR%20%2F%3ECreate%20table%20%23temp%3CBR%20%2F%3E(%3CBR%20%2F%3EState%20varchar(10)%2C%3CBR%20%2F%3ECounty%20varchar(10)%2C%3CBR%20%2F%3ERep%20int%2C%3CBR%20%2F%3EAmount%20varchar(100)%3CBR%20%2F%3E)%3CBR%20%2F%3Einsert%20into%20%23temp%3CBR%20%2F%3Eselect%20'01'%2C'001'%2C'100'%2C'%24300.00'%20union%3CBR%20%2F%3Eselect%20'01'%2C'001'%2C'999'%2C'%2422.50'%20union%3CBR%20%2F%3Eselect%20'01'%2C'002'%2C'300'%2C'%24750.00'%20union%3CBR%20%2F%3Eselect%20'01'%2C'002'%2C'999'%2C'%2412.75'%20union%3CBR%20%2F%3Eselect%20'02'%2C'020'%2C'400'%2C'%20%24450.00'%20union%3CBR%20%2F%3Eselect%20'02'%2C'020'%2C'999'%2C'%20%2457.00'%20union%3CBR%20%2F%3Eselect%20'02'%2C'030'%2C'111'%2C'%20%24645.00'%3CBR%20%2F%3E%3CBR%20%2F%3EAlter%20table%20%23temp%20add%20%5BOtherRep%5D%20int%2C%5BOther%20Amount%5D%20varchar(200)%3CBR%20%2F%3E%3CBR%20%2F%3E%3Bwith%20CTE%20as%20(%3CBR%20%2F%3Eselect%20*%2CRANK()over(partition%20by%20State%2CCounty%20order%20by%20state%2CCounty%2CRep)%20as%20Rnk%3CBR%20%2F%3Efrom%20%23temp)%3CBR%20%2F%3E%3CBR%20%2F%3Eselect%20C1.State%2CC1.County%2CC1.Rep%2CC1.Amount%2CC2.%5BRep%5D%5Bother%20Rep%5D%2CC2.%5BAmount%5D%20as%20%5BOther%20Amount%5D%3CBR%20%2F%3Efrom%20CTE%20as%20C1%3CBR%20%2F%3Einner%20join%20CTE%20as%20C2%20on%20C2.State%3DC1.State%20and%20C2.county%3DC1.County%20AND%20C2.Rnk%3D2%3CBR%20%2F%3Ewhere%20C1.Rnk%3D1%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-660932%22%20slang%3D%22en-US%22%3ERe%3A%20SQL%20Server%20Join%20Question%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-660932%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F279495%22%20target%3D%22_blank%22%3E%40MEG_AGI%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20a%20problem%20with%20the%20data%20in%20terms%20of%20how%20the%20records%20are%20associated.%20I%20visually%20understand%20that%20you%20would%20like%20the%20first%20rep%20to%20appear%20to%20the%20right%20and%20the%20second%20rep%20appear%20on%20the%20left%20as%20the%20other%20rep.%20It%20is%20a%20common%20problem%20when%20working%20with%20set%20of%20data.%20The%20First%20and%20Other%20need%20to%20have%20a%20very%20specific%20designation.%20For%20example%20an%20additional%20column%20can%20be%20added%20like%20rep_type%20that%20can%20have%20the%20value%20Primary%20or%20Other.%3C%2FP%3E%3CP%3ESo%20the%20data%20would%20look%20like%26nbsp%3B%3C%2FP%3E%3CP%3EState%26nbsp%3B%20County%20%26nbsp%3B%26nbsp%3B%20Rep%20%26nbsp%3B%20%26nbsp%3B%20Rep%20Type%20Amount%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20001%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20100%20%26nbsp%3B%20%26nbsp%3B%20Primary%20%26nbsp%3B%26nbsp%3B%20%24300.00%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20001%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20Other%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2022.50%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20002%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20300%20%26nbsp%3B%20%26nbsp%3B%20Primary%20%26nbsp%3B%26nbsp%3B%20%24750.00%3C%2FP%3E%3CP%3E01%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20002%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20Other%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2012.75%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20020%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20400%20%26nbsp%3B%20%26nbsp%3B%20Primary%20%26nbsp%3B%26nbsp%3B%20%24450.00%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20020%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20Other%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24%26nbsp%3B%2057.00%3C%2FP%3E%3CP%3E02%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20030%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20111%20%26nbsp%3B%20%26nbsp%3B%20Primary%20%26nbsp%3B%26nbsp%3B%20%24645.00%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20the%20table%20contains%20two%20things.%20Primary%20Reps%20and%20Other%20Reps.%20Your%20data%20table%20is%20a%20deviated%20from%20the%20spirit%20of%20relational%20database%20design%20but%20this%20can%20still%20work.%20Basically%20the%20rows%20in%20the%20table%20should%20be%20rows%20of%20the%20same%20thing%20(related)%20but%20in%20fact%20they%20are%20records%20of%20Reps%20and%20Other%20Reps.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESelect%20each%20type%20of%20rep%20from%20the%20table%20with%20additional%20zero%20value%20or%20blank%20columns%20for%20the%20other%20type.%20Union%20the%20results%20together%2C%20then%20group%20by%20State%2C%20County%2C%20max%20Rep%2C%20max%20Amount%2C%20max%20Other%20Rep%2C%20max%20Other%20Amount.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Query%20looks%20like%20this%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3Eselect%20A.%5BState%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20A.%5BCountry%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20max(A.%5BRep%5D)%20as%20%5BRep%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20max(A.%5BAmount%5D)%20as%20%5BAmount%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20max(A.%5BOther%20Rep%5D)%20as%20%5BOther%20Rep%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20max(A.%5BOther%20Amount%5D)%20as%20%5BOther%20Amount%5D%3CBR%20%2F%3Efrom%26nbsp%3B%20(SELECT%20%5BState%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BCountry%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BRep%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BAmount%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20''%20as%20%5BOther%20Rep%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%200%26nbsp%3B%20as%20%5BOther%20Amount%5D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20%5BIN_STAGING_dev%5D.%5Bdbo%5D.%5BTest_Table%5D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20where%20%5BRep%20Type%5D%20%3D%20'Primary'%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20UNION%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SELECT%20%5BState%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BCountry%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20''%20as%20%5BRep%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%200%26nbsp%3B%20as%20%5BAmount%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BRep%5D%20as%20%5BOther%20Rep%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BAmount%5D%26nbsp%3B%20as%20%5BOther%20Amount%5D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20%5BIN_STAGING_dev%5D.%5Bdbo%5D.%5BTest_Table%5D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20where%20%5BRep%20Type%5D%20%3D%20'Other')%20A%3CBR%20%2F%3E%26nbsp%3Bgroup%20by%20A.%5BState%5D%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20A.%5BCountry%5D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3EAnd%20the%20result%20looks%20like%20this%3A%26nbsp%3B%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%3EState%20%26nbsp%3B%20%26nbsp%3B%20Country%26nbsp%3B%20Rep%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20Amount%26nbsp%3BOther%20Rep%26nbsp%3BOther%20Amount%3CBR%20%2F%3E01%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B001%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20100%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B300.00%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2022.50%3CBR%20%2F%3E01%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B002%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20300%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B750.00%20%26nbsp%3B%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2012.75%3CBR%20%2F%3E02%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B020%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20400%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B450.00%20%26nbsp%3B%20999%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%2057.00%3CBR%20%2F%3E02%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B030%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20111%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B645.00%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%200.00%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20suggest%20that%20you%20create%20a%20primary%20key%20that%20contains%20State%2C%20Country%20and%20Rep%20Type.%20This%20means%20a%20record%20represents%20One%20type%20of%20Rep%20(Either%20Rep%20or%20other%20rep%20per%20a%20state%20and%20County)%20This%20way%20the%20query%20will%20always%20produce%20the%20correct%20result.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHave%20a%20nice%20day.%3C%2FP%3E%3C%2FLINGO-BODY%3E
MEG_AGI
Occasional Visitor

I'm new to the forum so if this is posted in the wrong place, please point me to the correct spot.

 

I have a table that has the following columns:

 

State  County    Rep     Amount

01        001        100       $300.00

01        001        999       $  22.50

01        002        300       $750.00

01        002        999       $  12.75

02        020        400        $450.00

02        020        999        $  57.00

02        030        111        $645.00

 

All other rep data is on a separate row with a code of 999.

 

What I need to do is create output that looks like this:

 

State  County    Rep     Amount       Other Rep    Other Amount

01        001        100       $300.00            999       $  22.50

01        002        300       $750.00            999       $  12.75

02        020        400        $450.00            999        $  57.00 

02        030        111        $645.00             0            $  0.00

 

In other words, I want to place the other rep data beside the Rep data in the table so that there is only 1 row per State/County Combo.

 

It seems simple but as a novice I need some help.

 

I appreciate any assistance.

 

MEG_AGI

 

 

 

2 Replies

@MEG_AGI 

Please use below query


Drop TABLE IF EXISTS #temp
Go
Drop TABLE IF EXISTS #tempfinal
GO

Create table #temp
(
State varchar(10),
County varchar(10),
Rep int,
Amount varchar(100)
)
insert into #temp
select '01','001','100','$300.00' union
select '01','001','999','$22.50' union
select '01','002','300','$750.00' union
select '01','002','999','$12.75' union
select '02','020','400',' $450.00' union
select '02','020','999',' $57.00' union
select '02','030','111',' $645.00'

Alter table #temp add [OtherRep] int,[Other Amount] varchar(200)

;with CTE as (
select *,RANK()over(partition by State,County order by state,County,Rep) as Rnk
from #temp)

select C1.State,C1.County,C1.Rep,C1.Amount,C2.[Rep][other Rep],C2.[Amount] as [Other Amount]
from CTE as C1
inner join CTE as C2 on C2.State=C1.State and C2.county=C1.County AND C2.Rnk=2
where C1.Rnk=1


@MEG_AGI 

 

There is a problem with the data in terms of how the records are associated. I visually understand that you would like the first rep to appear to the right and the second rep appear on the left as the other rep. It is a common problem when working with set of data. The First and Other need to have a very specific designation. For example an additional column can be added like rep_type that can have the value Primary or Other.

So the data would look like 

State  County    Rep     Rep Type Amount

01        001        100     Primary    $300.00

01        001        999     Other       $  22.50

01        002        300     Primary    $750.00

01        002        999     Other       $  12.75

02        020        400     Primary    $450.00

02        020        999     Other       $  57.00

02        030        111     Primary    $645.00

 

Now the table contains two things. Primary Reps and Other Reps. Your data table is a deviated from the spirit of relational database design but this can still work. Basically the rows in the table should be rows of the same thing (related) but in fact they are records of Reps and Other Reps.

 

Select each type of rep from the table with additional zero value or blank columns for the other type. Union the results together, then group by State, County, max Rep, max Amount, max Other Rep, max Other Amount.

 

The Query looks like this: 

 

select A.[State],
       A.[Country],
    max(A.[Rep]) as [Rep],
    max(A.[Amount]) as [Amount],
    max(A.[Other Rep]) as [Other Rep],
    max(A.[Other Amount]) as [Other Amount]
from  (SELECT [State],
              [Country],
     [Rep],
     [Amount],
     '' as [Other Rep],
     0  as [Other Amount]
        FROM [IN_STAGING_dev].[dbo].[Test_Table]
       where [Rep Type] = 'Primary'
      UNION
      SELECT [State],
          [Country],
    '' as [Rep],
    0  as [Amount],
    [Rep] as [Other Rep],
    [Amount]  as [Other Amount]
        FROM [IN_STAGING_dev].[dbo].[Test_Table]
       where [Rep Type] = 'Other') A
 group by A.[State],
          A.[Country]

 

And the result looks like this: 

State     Country  Rep        Amount Other Rep Other Amount
01         001         100        300.00    999           22.50
01         002         300        750.00    999           12.75
02         020         400        450.00   999            57.00
02         030         111        645.00                     0.00

 

I suggest that you create a primary key that contains State, Country and Rep Type. This means a record represents One type of Rep (Either Rep or other rep per a state and County) This way the query will always produce the correct result.

 

Have a nice day.