简体   繁体   中英

SQL Results output as a grid/single row resultset to multiple rows

Is it possible to output a resultset as a grid? For example I output the following resultset using sql:

col1    col2    col3    col4    col5    col6    col7    col8    col9
10      23      54      12      23      45      56      24      2

but instead of the output forming one long row is there a function I can use to get it to output as:

col1    col2    col3
10      23      54
12      23      45
56      24      2

So I'm essentially breaking the results row every three columns.

Also the output would be a combinations of various calculations performed on the data in joined sql tables just in case this makes a difference.

If you don't mind manually defining the columns to split, and the format will remain fixed you can use CROSS APPLY ... VALUES to unpivot the data. eg

SELECT  c.Col1, c.Col2, c.Col3
FROM    T
        CROSS APPLY
        (   VALUES
                (Col1, Col2, Col3),
                (Col4, Col5, Col6),
                (Col7, Col8, Col9)
        ) c (Col1, Col2, Col3);

Example on SQL Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM