Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按值数据生成SQL数据_Sql_Sql Server - Fatal编程技术网

按值数据生成SQL数据

按值数据生成SQL数据,sql,sql-server,Sql,Sql Server,我有这样的数据表 +----+---------+--------+ | id | name | values | +----+---------+--------+ | 1 | Budi | 4 | | 2 | sanjaya | 3 | | 3 | Dadang | 2 | +----+---------+--------+ 我想选择这样的数据 +----+---------+--------+--------+ | id | name

我有这样的数据表

+----+---------+--------+
| id |  name   | values |
+----+---------+--------+
|  1 | Budi    |      4 |
|  2 | sanjaya |      3 |
|  3 | Dadang  |      2 |
+----+---------+--------+
我想选择这样的数据

+----+---------+--------+--------+
| id |  name   | values | number |
+----+---------+--------+--------+
|  1 | Budi    |      4 |      1 |
|  1 | Budi    |      4 |      2 |
|  1 | Budi    |      4 |      3 |
|  1 | Budi    |      4 |      4 |
|  2 | sanjaya |      3 |      1 |
|  2 | sanjaya |      3 |      2 |
|  2 | sanjaya |      3 |      3 |
|  3 | Dadang  |      2 |      1 |
|  3 | Dadang  |      2 |      2 |
+----+---------+--------+--------+
数字可以通过值行生成

@Dhanang2112

DECLARE @temp TABLE
( 
    [id] int NOT NULL PRIMARY KEY,
    [name] varchar(10),
    [values] int
)
INSERT INTO @temp VALUES(1, 'Budi', 4)
INSERT INTO @temp VALUES(2, 'sanjaya', 3)
INSERT INTO @temp VALUES(3, 'Dadang', 2)

;WITH cte([id], [name], [values], [number]) AS   
(  
    SELECT [id], [name], [values], 1 AS [number]
    FROM @temp
    UNION ALL  
    SELECT cte.[id], cte.[name], cte.[values], cte.[number] + 1 AS [number]
    FROM cte
    WHERE cte.[number] < cte.[values]
)
SELECT * FROM cte ORDER BY [id], [number]
我很感谢您尝试为您的示例格式化数据,但是,如果您创建易于使用的数据,您将得到更好的答案,并且会更快地得到它们

这是您的数据,格式易于使用。。。即代码

 SELECT v.*
   INTO #TestTable
   FROM (VALUES
         (1,'Budi'   ,4)
        ,(2,'sanjaya',3)
        ,(3,'Dadang' ,2)
        ) v (id,name,[values])
;
从现在起,我不会使用递归CTE rCTE,因为即使是格式良好的WHILE循环也可以击败它们,并在过程中使用更少的资源。如果您想更多地了解RCTE的隐藏RBAR,请参阅下面的文章,因为这里的解释太长了

取而代之的是,我们将使用伊齐克·本·甘(Itzik Ben Gan)的方法,将我的名字与他的技术和cCTE串联起来。下面的函数是我最喜欢的方法,因为它使很多事情,比如给定的问题,变得容易得多。如果您的DBA对象,请让他们读取标题。它既不是慢速标量函数,也不是mTVF多语句表值函数。这是一个高性能的iTVF内联表值函数。大部分是评论

 CREATE FUNCTION [dbo].[fnTally] --Rev 04
/**********************************************************************************************************************
 Purpose:
 Return a column of BIGINTs from @ZeroOrOne up to and including @MaxN with a max value of 10 Quadrillion.

 Usage:
--===== Syntax example
 SELECT t.N
   FROM dbo.fnTally(@ZeroOrOne,@MaxN) t
;
 @ZeroOrOne will return a 1 for any number other than 0 and a 0 for a 0.
 @MaxN has an operation domain from 0 to 4,294,967,296.

 Please see the following notes for other important information


 Notes:
 1. This code works for SQL Server 2008 and up.
 2. Based on Itzik Ben-Gan's cascading CTE (cCTE) method for creating a "readless" Tally Table source of BIGINTs.
    Refer to the following URL for how it works.
    https://www.itprotoday.com/sql-server/virtual-auxiliary-table-numbers
 3. To start a sequence at 0, @ZeroOrOne must be 0. Any other value that's convertable to the BIT data-type
    will cause the sequence to start at 1.
 4. If @ZeroOrOne = 1 and @MaxN = 0, no rows will be returned.
 5. If @MaxN is negative or NULL, a "TOP" error will be returned.
 6. @MaxN must be a positive number from >= the value of @ZeroOrOne up to and including 4,294,967,296. If a larger
    number is used, the function will silently truncate after that max. If you actually need a sequence with that many
    or more values, you should consider using a different tool. ;-)
 7. There will be a substantial reduction in performance if "N" is sorted in descending order.  If a descending sort is
    required, use code similar to the following. Performance will decrease by about 27% but it's still very fast 
    especially compared with just doing a simple descending sort on "N", which is about 20 times slower.
    If @ZeroOrOne is a 0, in this case, remove the "+1" from the code.

    DECLARE @MaxN BIGINT; 
     SELECT @MaxN = 1000;
     SELECT DescendingN = @MaxN-N+1 
       FROM dbo.fnTally(1,@MaxN);

 8. There is no performance penalty for sorting "N" in ascending order because the output is implicity sorted by
    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
 9. This will return 1-10,000,000 to a bit-bucket variable in about 986ms.
    This will return 0-10,000,000 to a bit-bucket variable in about 1091ms.
    This will return 1-4,294,967,296 to a bit-bucket variable in about 9:12( mi:ss).

 Revision History:
 Rev 00 - Unknown     - Jeff Moden 
        - Initial creation with error handling for @MaxN.
 Rev 01 - 09 Feb 2013 - Jeff Moden 
        - Modified to start at 0 or 1.
 Rev 02 - 16 May 2013 - Jeff Moden 
        - Removed error handling for @MaxN because of exceptional cases.
 Rev 03 - 07 Sep 2013 - Jeff Moden 
        - Change the max for @MaxN from 10 Billion to 10 Quadrillion to support an experiment. 
          This will also make it much more difficult for someone to actually get silent truncation in the future.
 Rev 04 - 04 Aug 2019 - Jeff Moden
        - Enhance performance by making the first CTE provide 256 values instead of 10, which limits the number of
          CrossJoins to just 2. Notice that this changes the maximum range of values to "just" 4,294,967,296, which
          is the entire range for INT and just happens to be an even power of 256. Because of the use of the VALUES
          clause, this code is "only" compatible with SQLServer 2008 and above.
        - Update old link from "SQLMag" to "ITPro". Same famous original article, just a different link because they
          changed the name of the company (twice, actually).
        - Update the flower box notes with the other changes.
**********************************************************************************************************************/
        (@ZeroOrOne BIT, @MaxN BIGINT)
RETURNS TABLE WITH SCHEMABINDING AS 
 RETURN WITH
  H2(N) AS ( SELECT 1 
               FROM (VALUES
                     (1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    ,(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)
                    )V(N))            --16^2 or 256 rows
, H4(N) AS (SELECT 1 FROM H2 a, H2 b) --16^4 or 65,536 rows
, H8(N) AS (SELECT 1 FROM H4 a, H4 b) --16^8 or 4,294,967,296 rows
            SELECT N = 0 WHERE @ZeroOrOne = 0 UNION ALL
            SELECT TOP(@MaxN)
                   N = ROW_NUMBER() OVER (ORDER BY N)
              FROM H8
;
而且,现在解决像你这样的问题非常简单和快速。这是密码

--===== The problem becomes child's play.
 SELECT tt.id ,tt.name, tt.[values], Numbers = t.N
   FROM ATestTable tt
  CROSS APPLY dbo.fnTally(1, tt.[values]) t
  ORDER BY tt.id, t.N
;
这是结果


p、 我忘了提到这就是所谓的关系乘法。基本上,它是函数为每行生成的值与每行内容之间交叉连接的结果

请显示您尝试使用递归cte轻松实现的查询。请看一看文档谢谢,我会试着读一下为什么你不想在下面的URL上使用递归CTE。感谢@JeffModen的工作,我从来没有听说过递归cte,这可以解决我的问题,非常感谢解决了功能方面。。。对于较小的行集,RCTE看起来不像是性能问题,但仍然是。它们充满了完全不必要的CPU和内存I/U资源使用浪费。考虑使用我发布的代码,它没有这样的问题。虽然我还没有真正理解背后的理论,我尝试了你的解决方案,它实际上有更好的性能,谢谢分享。谢谢你的友好反馈。这就是我们的全部目标。。。表演这是仅次于正确功能的第二位,这是非常接近的第二位。很高兴看到你的名字出现,多年来我在SSC和其他论坛上从你那里学到了很多。我最近在dbt代码库中看到了伊兹克技巧的一个有趣变化:模板使用pow生成一个等价于位移位的值,其中每个CTE的列表示位。它还根据将上限拟合到2次幂的快速搜索中,来正确调整构建时所需的交叉联接数量。Postgres的速度很快,我没有访问SQL Server来测试它。感谢您的友好反馈和指向dbt技巧的指针。我不想在关于dbt的对话中加入这个帖子,所以我不想,但这是一些有趣的代码。谢谢你发布这个链接。我也这么想……多年来我在SSC上从你那里学到了很多