SQL Server - Group by with multiple joins and strings -


i have searched many similiar topics still struggling working. trying group on 1 column in particular after doing few table joins. wouldbe appreciated

output today

account system  system  plan code   account plan code   request 12345   sys1    sys1    p1         12345    p1          00001 12345   sys1    sys1    p2         null     null        null 12345   sys2    sys2    p3         12345    p3          00002 34567   sys1    sys1    p1         null     null        null 34567   sys1    sys1    p2         34567    p2          00003 45678   sys3    sys3    p4         null     null        null 

desired output

account system  system  plan code   account plan code   request 12345   sys1    sys1    p1          12345   p1, p2      00001 12345   sys2    sys2    p3          12345   p3          00002 34567   sys1    sys1    p2          34567   p1,p2       00003 45678   sys3    sys3    p4          null    null        null 

i joining few tables , want combine plan codes 1 row per each unique account system mapping.

  • tab1 = account , system
  • tab2 = system , plan code (mapping shows plan codes related each system)
  • tab3 = account,plan code, request #

i lost group tried 1 column group , error not group expression. included columns , not change output @ all.

select      a.account, a.system, b.system, b.pcode, c.account, c.pcode, c.request        mydb.tab1 left join   mydb.tab2 b  on      a.system = b.system left join   mydb.tab3 c on      a.account = c.account , b.pcode = c.pcode       a.account not null order    a.account 

if not need concatenate plans, please see if following group by implementation suit needs. removed several columns script. comply aggregate query limitations used max around c.pcode (which min well) , max c.request seems choice rid of nulls there.

select      a.account, a.system, max(c.pcode), max(c.request)        mydb.tab1 left join   mydb.tab2 b  on      a.system = b.system left join   mydb.tab3 c on      a.account = c.account , b.pcode = c.pcode       a.account not null group a.account, a.system order    a.account 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -