sql server - Aggregating SQL Data and concatenating strings / combining records -


I am trying to gather data from one table to another. I inherited this project; I did not design this database nor can I change its format.

[RawData] The table will have 1 record per channel, per ChannelCodeID. The table (where is currently the data) has the following fields:

  [account] int [channelcode id] int [channelcode] varchar (10)  

[Integrated Data] will have 1 record per account in the table. This table contains the following fields (where I need to insert data):

  [account] int [calculation] int [channel1] int [channel 2] int [channel 3] int [ Name] varchar For example, I can have the following records in the [ródata] table:  
  account ChannelCodeID ChannelCode 12345 2 ABC 12345 4 DEF 12345 6 GI 54321 2 ABC 54321 6 GHI 99 999 2 ABC  

And, after collecting them, let me create the following records in my [Collected Data] table Mr.'ll need:

  Account numbers Channel 1 Channel 2 Channel 3 Name 12345 3 2 4 6 ABC. DEF.GHI 54321 2 2 6 0 ABC. As you can see, how many records are present in the table in my [ródata], Channel1 is the first channelcode ID, Channel 2 is second, and the channel 3 is the third. If there is not enough channelcode ID from my [ródata] table, the additional channel column gets the '0' value. In addition, I have to add the 'ChannelCode' column and it must be stored in the 'Name' column of the [Collected Data] table, but (obviously) if there is only one record, then I do not want to add ''. '' / P> 

I did not know how without a bunch of cursors and variables - but I'm guessing that there is a better way. It does not need to be too fast because it will run only once a month, but at least 10-15,000 records must be processed at each time.

Thanks in advance ...

Edit:

ChannelCode and ChannelCode ID map each other directly and always have the same Are there. For example, ChannelCoded2 is always 'ABC'

Also, in the [Collected Data] table, Channel 1 is always the following value, although it is relevant.

test data
  DECLARE @table table (account INT, ChannelCodeID Insert value (12345, 2, 'ABC'), (12345, 4, 'DEF),' (12345, 6, 'GHI'), (54321, 2, '', 'INT, ChannelCode VARCHAR' Choose ABC ', (54321, 6,' GHI '), (No. 99992,' ABC ')  

Question

  Account, [Count], ISNULL ([Channel 1], 0) AS [Channel 1 AS [Channel 3], 0) AS [Channel 3], name F.R.M. (SELECT t.Account, T.ChannelCodeID, c. [Calculation] , 'Channel' +], [Channel 2] AS [Channel 2], ISUnl [C] AST (ROW_NUMBER) (Order by T.Can Code IDID ASC by Participant) as VARCHAR (10) Channel, STUFF ((SELECT '.' + Channel code @table where account = t.Acc XML path ( For TYP type '), type in value ('. ',' NVARCHAR (MAX) '), 1,1,' ') to join TB T Inner (Selection Account, COUNT (*) as [ Povt (Max (ChannelCodeID)) P   / Pre> 

Result

  ╔══ in Channel 1], [Channel 2], [Channel 3]) ═══════╦═══════╦══════════╦═══ ═══════╦═════ ════╦═════════════╗ ║ Account ║ Calculation ║ Channel 1 ║ Channel 2 ║ Channel 3 ║ Name ║ ╠═══ ══════╬════ ═══╬══════════╬══════════╬══════════╬══ ═══════════ ╣ ║ 12345 ║ 3 ║ 2 ║ 4 ║ 6 ║ ABC DEF.GHI ║ ║ 54321 ║ 2 ║ 2 ║ 6 ║ 0 ║ ABC.GHI ║ 99 999 ║ 1 ║ 2 ║ 0 ║ 0 ║ ABC ║ ╚═══ ══════╩═══════╩═════ ═════╩══════════╩══════════╩══ ═══════════╝  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -