Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I Group By an Unknown Number of Columns?
    primarykey
    data
    text
    <p>I am currently trying to re-write a stored procedure to take into account the normalisation of one of our tables. In the original procedure we have two tables: </p> <pre><code>CREATE TABLE #t_batch (batch_id integer, thread_group NVARCHAR(60), dye_code_1 NVARCHAR(10), dye_conc_1 NUMERIC(19, 7), dye_code_2 NVARCHAR(10), dye_conc_2 NUMERIC(19, 7), dye_code_3 NVARCHAR(10), dye_conc_3 NUMERIC(19, 7), dye_code_4 NVARCHAR(10), dye_conc_4 NUMERIC(19, 7), dye_code_5 NVARCHAR(10), dye_conc_5 NUMERIC(19, 7), dye_code_6 NVARCHAR(10), dye_conc_6 NUMERIC(19, 7)) CREATE TABLE #t_group (group_id INTEGER IDENTITY(1, 1), dye_code_1 NVARCHAR(10), dye_conc_1 NUMERIC(19, 7), dye_code_2 NVARCHAR(10), dye_conc_2 NUMERIC(19, 7), dye_code_3 NVARCHAR(10), dye_conc_3 NUMERIC(19, 7), dye_code_4 NVARCHAR(10), dye_conc_4 NUMERIC(19, 7), dye_code_5 NVARCHAR(10), dye_conc_5 NUMERIC(19, 7), dye_code_6 NVARCHAR(10), dye_conc_6 NUMERIC(19, 7), thread_group NVARCHAR(60), num_batches INTEGER) </code></pre> <p>After a number of actions #t_batch was populated with a number of records. We then inserted data into #t_group in the following way:</p> <pre><code>INSERT INTO #t_group (dye_code_1, dye_conc_1, dye_code_2, dye_conc_2, dye_code_3, dye_conc_3, dye_code_4, dye_conc_4, dye_code_5, dye_conc_5, dye_code_6, dye_conc_6, thread_group, num_batches) SELECT dye_code_1, dye_conc_1, dye_code_2, dye_conc_2, dye_code_3, dye_conc_3, dye_code_4, dye_conc_4, dye_code_5, dye_conc_5, dye_code_6, dye_conc_6, thread_group, COUNT(batch_id_fk) FROM #t_batch GROUP BY dye_code_1, dye_conc_1, dye_code_2, dye_conc_2, dye_code_3, dye_conc_3, dye_code_4, dye_conc_4, dye_code_5, dye_conc_5, dye_code_6, dye_conc_6, thread_group ORDER BY dye_code_1, dye_conc_1, dye_code_2, dye_conc_2, dye_code_3, dye_conc_3, dye_code_4, dye_conc_4, dye_code_5, dye_conc_5, dye_code_6, dye_conc_6, thread_group </code></pre> <p>So, we had a series of records that are grouped by the dye columns and a unique group_id for each unique combination of dyes and their concentrations. Also, there is a count of the batch records for each group. </p> <p>However, since there is in reality no limit to the number of dyes for a batch the tables have been normalised:</p> <pre><code>CREATE TABLE #t_batch (batch_id INTEGER, thread_group NVARCHAR(60)) CREATE TABLE #t_batch_dye (batch_id_fk INTEGER, stage INTEGER, sequence INTEGER, dye_code NVARCHAR(10), dye_conc NUMERIC(19,7)) CREATE TABLE #t_group (group_id INTEGER IDENTITY(1, 1), thread_group NVARCHAR(60), num_batches INTEGER) CREATE TABLE #t_group_dye (group_id INTEGER, stage INTEGER, sequence INTEGER, dye_code NVARCHAR(10), dye_conc NUMERIC(19,7)) </code></pre> <p>Now, my question is: assuming that we have #t_batch and #t_batch_dye populated and that there are a varying number of #t_batch_dye records for each record in #t_batch, how can I insert records into #t_group with a unique group_id for each unique combination of dyes and their concentrations as well as a count of the batches for each group?</p> <p>Is this something I could use the PIVOT keyword for? The examples I have found on the web all seem to assume that the number of pivoted fields is known in advance.</p> <p>Many thanks, </p> <p>David</p> <p>Glasgow, Scotland</p> <hr> <p>Update:</p> <p>What I have done is to use a function that returns a concatenated string of codes and concs and used that to group the data.</p> <pre><code>DECLARE @dyes NVARCHAR(2000) SELECT @dyes = ISNULL(@dyes,'') + dye_code + ' ' + convert(nvarchar, requested_dye_conc) + ' ' FROM #t_batch_dye WHERE batch_id_fk = @batch_id ORDER BY dye_code ASC </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload