创建一个单行表,其列数与 SELECT 查询返回的表中的行数一样多

发布于 2024-09-07 15:02:31 字数 395 浏览 0 评论 0原文

我想要一个查询创建一个单行表,其中的列数与从 SELECT 查询返回的表中的行数一样多,其中创建的列的名称是从 SELECT 查询的某些列中获取的值,并且具有以下值:是从 SELECT 查询的其他一些列获取的值。

例如,

如果我有一个包含两列的表 T1,如下所示:

Field  Value
  A       1
  B       2
  C       3
  D       4

那么我想要一个查询,该查询将返回以下一行表 T2 作为结果:

COLUMN NAMES :    A       B       C       D
COLUMN VALUES:    1       2       3       4

I want a query to create a one row table with as many columns as there are rows in a table returned from a SELECT query., where the columns created have names which are values taken from some column of the SELECT query, and have values which are values taken from some other column of the SELECT query.

e.g.

If I have a table T1 with two columns as follows :

Field  Value
  A       1
  B       2
  C       3
  D       4

Then I want a query that will return the following one row table T2 as result :

COLUMN NAMES :    A       B       C       D
COLUMN VALUES:    1       2       3       4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-09-14 15:02:31

一个简单的查询可以解决这个问题:

SELECT  SUM(CASE C1 WHEN 'A' THEN C2 ELSE 0 END) AS 'A',
        SUM(CASE C1 WHEN 'B' THEN C2 ELSE 0 END) AS 'B',
        SUM(CASE C1 WHEN 'C' THEN C2 ELSE 0 END) AS 'C',
        SUM(CASE C1 WHEN 'D' THEN C2 ELSE 0 END) AS 'D'
FROM T1

一个有机查询:

DECLARE @C1 CHAR(1), @SQL VARCHAR(500)
DECLARE array CURSOR
    FOR SELECT C1
        FROM T1
        ORDER BY C1

OPEN array

FETCH NEXT FROM array
INTO @C1;
SET @SQL = 'SELECT  SUM(CASE C1 WHEN ''' + @C1 + ''' THEN C2 ELSE 0 END) AS ''' + @C1 + ''''
FETCH NEXT FROM array
INTO @C1;
WHILE (@@FETCH_STATUS = 0)
    BEGIN
        SET @SQL = @SQL + ', SUM(CASE C1 WHEN ''' + @C1 + ''' THEN C2 ELSE 0 END) AS ''' + @C1 + ''''
        FETCH NEXT FROM array
        INTO @C1
    END
SET @SQL = @SQL + 'FROM T1'

CLOSE array
DEALLOCATE array

EXEC(@SQL)

A simple query to solve this:

SELECT  SUM(CASE C1 WHEN 'A' THEN C2 ELSE 0 END) AS 'A',
        SUM(CASE C1 WHEN 'B' THEN C2 ELSE 0 END) AS 'B',
        SUM(CASE C1 WHEN 'C' THEN C2 ELSE 0 END) AS 'C',
        SUM(CASE C1 WHEN 'D' THEN C2 ELSE 0 END) AS 'D'
FROM T1

An organic query:

DECLARE @C1 CHAR(1), @SQL VARCHAR(500)
DECLARE array CURSOR
    FOR SELECT C1
        FROM T1
        ORDER BY C1

OPEN array

FETCH NEXT FROM array
INTO @C1;
SET @SQL = 'SELECT  SUM(CASE C1 WHEN ''' + @C1 + ''' THEN C2 ELSE 0 END) AS ''' + @C1 + ''''
FETCH NEXT FROM array
INTO @C1;
WHILE (@@FETCH_STATUS = 0)
    BEGIN
        SET @SQL = @SQL + ', SUM(CASE C1 WHEN ''' + @C1 + ''' THEN C2 ELSE 0 END) AS ''' + @C1 + ''''
        FETCH NEXT FROM array
        INTO @C1
    END
SET @SQL = @SQL + 'FROM T1'

CLOSE array
DEALLOCATE array

EXEC(@SQL)
生生不灭 2024-09-14 15:02:31

这不是一个非常完美的解决方案,但它会让您了解如何使用动态 SQL 来实现这一目标。

--** Set up a table to test this with
DECLARE @TestTable TABLE (Field VARCHAR(20), VALUE INT);

INSERT INTO @TestTable (Field, VALUE)
SELECT 'A',1 UNION
SELECT 'B',2 UNION
SELECT 'C',3 UNION
SELECT 'D',4


--** Set up variables
DECLARE @ColCount INT;
DECLARE @Loop INT;
DECLARE @ColName VARCHAR(20);
DECLARE @Value INT;
DECLARE @SQL VARCHAR(255);

--** Create temp table to hold the results and insert the row for populating
CREATE TABLE #ResultSet (dummy INT);
INSERT INTO #ResultSet  (dummy) VALUES (0);  

--** Get count of required columns
SELECT @ColCount = COUNT(*)
FROM @TestTable AS tt

SET @Loop = 1

WHILE @Loop <= @ColCount BEGIN
    --** Get column name and value
    WITH Cols AS
    (
        SELECT tt.Field
             , tt.VALUE
             , ROW_NUMBER() OVER (ORDER BY tt.Field) AS row
        FROM   @TestTable AS tt
    )
    SELECT @ColName = Field
         , @Value = Value
    FROM   Cols
    WHERE  row = @Loop;

    --** Add the column
    SET @SQL = 'ALTER TABLE #ResultSet ADD ' + @ColName + ' INT;'; 
    EXEC(@SQL);

    --** Insert the value   
    SET @SQL = 'UPDATE #ResultSet SET ' + @ColName + ' = ' + CONVERT(VARCHAR(50),@Value) 
    EXEC(@SQL);

    SET @Loop = @Loop + 1;
END

--** Drop the dummy column
ALTER TABLE #ResultSet DROP COLUMN [dummy];

SELECT *
FROM #ResultSet;

DROP TABLE #ResultSet;

This is not a very polished solution, but it will give you an idea as to how you can achieve this using dynamic SQL.

--** Set up a table to test this with
DECLARE @TestTable TABLE (Field VARCHAR(20), VALUE INT);

INSERT INTO @TestTable (Field, VALUE)
SELECT 'A',1 UNION
SELECT 'B',2 UNION
SELECT 'C',3 UNION
SELECT 'D',4


--** Set up variables
DECLARE @ColCount INT;
DECLARE @Loop INT;
DECLARE @ColName VARCHAR(20);
DECLARE @Value INT;
DECLARE @SQL VARCHAR(255);

--** Create temp table to hold the results and insert the row for populating
CREATE TABLE #ResultSet (dummy INT);
INSERT INTO #ResultSet  (dummy) VALUES (0);  

--** Get count of required columns
SELECT @ColCount = COUNT(*)
FROM @TestTable AS tt

SET @Loop = 1

WHILE @Loop <= @ColCount BEGIN
    --** Get column name and value
    WITH Cols AS
    (
        SELECT tt.Field
             , tt.VALUE
             , ROW_NUMBER() OVER (ORDER BY tt.Field) AS row
        FROM   @TestTable AS tt
    )
    SELECT @ColName = Field
         , @Value = Value
    FROM   Cols
    WHERE  row = @Loop;

    --** Add the column
    SET @SQL = 'ALTER TABLE #ResultSet ADD ' + @ColName + ' INT;'; 
    EXEC(@SQL);

    --** Insert the value   
    SET @SQL = 'UPDATE #ResultSet SET ' + @ColName + ' = ' + CONVERT(VARCHAR(50),@Value) 
    EXEC(@SQL);

    SET @Loop = @Loop + 1;
END

--** Drop the dummy column
ALTER TABLE #ResultSet DROP COLUMN [dummy];

SELECT *
FROM #ResultSet;

DROP TABLE #ResultSet;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文