0
I have a table in SQL Server Management Studio in which I have the fields test | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 Create a for loop in Visual Studio’s C# in another table that counted how many times a given Y note came out after the X note, and this calculation took more than 18 minutes, so I created a whole statement in SQL and the calculation was reduced to 50 seconds. I tried to migrate the instruction format to this table above, but since the columns have 2 decimal places (01, 02, 03 .. 08) I cannot implement the sql statement for this table in question. The instruction in question would be:
DECLARE @nota1 INT
DECLARE @nota2 INT
DECLARE @posicao1 INT
DECLARE @posicao2 INT
SET @nota1 = 1
SET @nota2 = 2
SET @posicao1 = 1
SET @posicao2 = 2
WHILE @nota2 <= 20
BEGIN
WHILE @nota1 <= 20
BEGIN
WHILE @posicao1 <= 7
BEGIN
WHILE @posicao2 <= 8
BEGIN
INSERT INTO tabela2 ([nota1], [posicao1], [nota2], [posicao2], [qtde]) VALUES
(
@nota1,
@posicao1,
@nota2,
@posicao2,
(SELECT COUNT(1) AS 'Valor'
FROM tabela
WHERE '[0' + @posicao1 + ']' = @nota1 AND '[' + @posicao2 + ']' = @nota2))
SET @posicao2 += 1
END
SET @posicao1 += 1
END
SET @nota1 += 1
END
SET
@nota2 += 1
END
The SELECT instruction to work would have to be the same as in the example:
SELECT COUNT(1) AS 'Valor' FROM tabela WHERE [01] = @nota1 AND [02] = @nota2
How do I add the while instruction to two decimal places? Or how to concatenate?
I didn’t quite understand the question, but to enter two decimal places you can use
select campo + 0.00 as meuCampoFloat from tabela
– Ronaldo Araújo Alves
I created the While instruction to scroll through columns from 01 to 08. In the sql statement it would have to be for example: SELECT * FROM table WHERE [01] = 5 but the while statement will not put between [ ] nor put with 2 house, [01] and when adding with SET @posicao1 += 1 goes out of 1 and goes to 2 and not [02]
– i_melo