Side by Side rows of SQL Server

union sql
except in sql w3school
sql intersect w3schools
sql display two results side by side
sql merge two tables with different fields
sql server join two tables side by side
sql merge two tables with same columns
sql join

I need to get the data from a table, (img 1), and put on same line (img 2)

How can i do that?

It's need to be on SQL Server

i already tried select inside select, but i think that i dont know the right way to do that or if wont work with this.

    select * from tblTest
    where exists(select * from tblTest)

What i have

What i need

I think you are looking for

CREATE TABLE T(
  ID INT,
  Aux INT,
  Test1 VARCHAR(10),
  Test2 VARCHAR(10),
  Test3 VARCHAR(10)
);

INSERT INTO T VALUES
(1, 10, 'ABC', 'DEF', 'GHI'),
(2, 11, '123', '456', '789'),
(3, 12, 'Foo', 'Bar', 'None');

SELECT T2.*,
       T1.*
FROM T T1 RIGHT JOIN T T2 ON T1.ID = T2.ID +1
WHERE T2.ID % 2 > 0;

Returns:

+----+-----+-------+-------+-------+------+------+-------+-------+-------+
| ID | Aux | Test1 | Test2 | Test3 |  ID  | Aux  | Test1 | Test2 | Test3 |
+----+-----+-------+-------+-------+------+------+-------+-------+-------+
|  1 |  10 | ABC   | DEF   | GHI   | 2    | 11   | 123   | 456   | 789   |
|  3 |  12 | Foo   | Bar   | None  | NULL | NULL | NULL  | NULL  | NULL  |
+----+-----+-------+-------+-------+------+------+-------+-------+-------+

Demo

Updates:

INSERT INTO T VALUES
(1, 10, 'ABC', 'DEF', 'GHI'),
(2, 11, '123', '456', '789'),
(3, 12, 'Foo', 'Bar', 'None'),
(4, 13, 'JKL', 'MNO', 'PQR'),
(5, 13, 'STU', 'VWX', 'XZ!'),
(6, 14, 'AZE', 'RTY', 'UIO');

SELECT *
FROM T T1 LEFT JOIN T T2 ON T2.ID - 1 = T1.ID
          LEFT JOIN T T3 ON T3.ID - 1 = T2.ID
WHERE T1.ID % 3 = 1;

Returns:

+----+-----+-------+-------+-------+----+-----+-------+-------+-------+----+-----+-------+-------+-------+
| ID | Aux | Test1 | Test2 | Test3 | ID | Aux | Test1 | Test2 | Test3 | ID | Aux | Test1 | Test2 | Test3 |
+----+-----+-------+-------+-------+----+-----+-------+-------+-------+----+-----+-------+-------+-------+
|  1 |  10 | ABC   | DEF   | GHI   |  2 |  11 | 123   | 456   | 789   |  3 |  12 | Foo   | Bar   | None  |
|  4 |  13 | JKL   | MNO   | PQR   |  5 |  13 | STU   | VWX   | XZ!   |  6 |  14 | AZE   | RTY   | UIO   |
+----+-----+-------+-------+-------+----+-----+-------+-------+-------+----+-----+-------+-------+-------+

Demo

Displaying two rows side by side - MSDN, SQL Server Reporting Services, Power View Any idea how could it be done in SSRS without altering my sql query? want to change the query you can still get the two column side-by-side by using expressions in a table. This is a bit of a guess on the OP's last comment: "what i trying to say its, foreach 3 rows i need to have 1 row with this all.".I suspect they therefore want this: WITH CTE AS( SELECT {YourColumns}, ROW_NUMBER() OVER (ORDER BY ID) - 1 AS RN FROM {YourTable}) SELECT {YourColumns} FROM CTE C1 LEFT JOIN CTE C2 ON C1.RN + 1 = C2.RN LEFT JOIN CTE C3 ON C2.RN + 1 = C3.RN WHERE C1.RN % 3 = 0;

This is a bit of a guess on the OP's last comment: "what i trying to say its, foreach 3 rows i need to have 1 row with this all.". I suspect they therefore want this:

WITH CTE AS(
    SELECT {YourColumns},
           ROW_NUMBER() OVER (ORDER BY ID) - 1 AS RN
    FROM {YourTable})
SELECT {YourColumns}
FROM CTE C1
     LEFT JOIN CTE C2 ON C1.RN + 1 = C2.RN
     LEFT JOIN CTE C3 ON C2.RN + 1 = C3.RN
WHERE C1.RN % 3 = 0;

You'll need to replace the parts in the braces ({}) with the respective object names. Also, be warned, this is very likely to perform awfully on anything but a small dataset. The query is far from SARGable.

Edit: In regards to the query you have in your post, it doesn't make any sense I'm afraid.

select * from tblTest
where exists(select * from tblTest)

This is effectively stating "return all rows and columns from the table tblTest if there are rows in the table tblTest. The EXISTS is therefore completely pointless, as if there are no rows in tblTest then the SELECT would return no rows anyway. Checking for the existence of rows achieves nothing but a further scan on the table.

It seems that you are very new to SQL, so it probably an idea to look up some tutorials and guides online first so that you can get to grip with the basics.

EXCEPT and INTERSECT (Transact-SQL), EXCEPT returns distinct rows from the left input query that aren't by both the query on the left and right sides of the INTERSECT operator. With a server-side cursor, the server manages the result set using resources provided by the server computer. The server-side cursor returns only the requested data over the network. This type of cursor can sometimes provide better performance than the client-side cursor, especially in situations where excessive network traffic is a problem.

Assuming that you have more that 2 or 3 rows in your table, you're going to need dynamic sql... The following should do the trick.

USE tempdb;
GO

SET NOCOUNT ON;

IF OBJECT_ID('tempdb.dbo.TestData', 'U') IS NOT NULL 
BEGIN DROP TABLE dbo.TestData; END;

CREATE TABLE dbo.TestData (
    ID INT NOT NULL,
    Aux INT NOT NULL,
    Test1 CHAR(3) NOT NULL,
    Test2 CHAR(3) NOT NULL,
    Test3 CHAR(3) NOT NULL 
    );
INSERT dbo.TestData (ID, Aux, Test1, Test2, Test3) VALUES
    (1, 10, 'ABC', 'DEF', 'GHI'),
    (2, 11, '123', '456', '789');


-- SELECT * FROM dbo.TestData td;

--=============================================================

DECLARE 
    @sql NVARCHAR(MAX) = N'',
    @debug BIT = 0;

SELECT TOP (2147483647)
    @sql = CONCAT(@sql, N',
    ID  = MAX(CASE WHEN td.ID = ', td.ID, N' THEN ', td.ID , N' END),
    AUX = MAX(CASE WHEN td.ID = ', td.ID, N' THEN ', td.Aux, N' END),
    Test1 = MAX(CASE WHEN td.ID = ', td.ID, N' THEN ''', td.Test1, N''' END),
    Test2 = MAX(CASE WHEN td.ID = ', td.ID, N' THEN ''', td.Test2, N''' END),
    Test3 = MAX(CASE WHEN td.ID = ', td.ID, N' THEN ''', td.Test3, N''' END)'
    )
FROM
    dbo.TestData td
ORDER BY 
    td.ID;

SET @sql = CONCAT(N'
SELECT', STUFF(@sql, 1, 1, ''), N'
FROM 
    dbo.TestData td;');

IF @debug = 1 
BEGIN
    PRINT(@sql);
END;
ELSE
BEGIN
    EXEC sys.sp_executesql @sql;
END;

Results:

ID          AUX         Test1 Test2 Test3 ID          AUX         Test1 Test2 Test3
----------- ----------- ----- ----- ----- ----------- ----------- ----- ----- -----
1           10          ABC   DEF   GHI   2           11          123   456   789

Select from two tables, side by side – SQLServerCentral, The 5 values for TableB will be in the rows. The reverse would be true if TableA had more values for a given ID than TableB. To illustrate: TableA. Thank you for an answer. I have tried It, but unsuccessfully. It's fine with creating column in right side, but when I try to add group on It (right click on new created column's cell > Add Group > Row Group > Parent Group) It automatically creating It in left side of the table1_Details_Group.

SQL UNION, SQL joins allow you to combine two datasets side-by-side, but UNION allows the dataset is appended, and any rows in the appended table that are exactly  2. In the Options dialog box on the left side panel expand the Text Editor option and expand Transact-SQL.Next select General as shown in the snippet below.

Microsoft SQL Server 2000: A Guide to Enhancements and New Features, The server-side consumer thread that captures the requested events returns OLE To reduce the amount of data sent from server to client, each row contains a  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Displaying SELECT Query Results - from Same Table - Side by Side

SQL Server Query Performance Tuning, Client-side cursors have the following cost overhead or drawbacks: • Higher On the other hand, scaling out a transactional SQL Server database is still an art! Only one active cursor-based statement on one connection: As many rows of  With all but the SQL Server 7 running side by side on my 64 bit laptop (actually 32 bit SQL 2005 is excluded) I made a guess on this one since the documentation was as listed above.

Comments
  • How many rows are you wanting to put as columns?
  • This is not how relational data works. What happens when you have 10,000 rows? Do you expect to have 50,000 columns? This screams of being an xy problem.
  • "Its always 3 rows" Yet you're sample data only has 2.
  • Your English seems pretty good to me. But let's avoid that language since it causes a barrier. Instead lets use the international language of data. Post some sample data and the desired output. It would be helpful to know how you know which three rows make up a "group". The query isn't that difficult once we know the scope of the problem.
  • This might be a good place to start. spaghettidba.com/2015/04/24/…
  • its almost worked, i adapted to 3 times, but i have a column that have be always in this order: i'll show with AUX it can be 1:8, 1:4 and 1:2 so i need to have: ID AUX Test1 1 1:8 abc ID AUX Test1 2 1:4 123 ID AUX Test1 3 1:2 Foo
  • i didnt know that use this % 3 = 1 its possible in sql, thanks
  • @Leooso Glad to help ;) and yes it is, read this
  • i think that my query is strange too, but im trying everything to do this work and i'll try what you suggest