Solved

Newspaper Article view in SQL Server 2012

Posted on 2016-10-18
4
77 Views
Last Modified: 2016-10-18
Hi,

  I'm using SQL Server 2012 database.
What i'm looking for is as follows:
Is this possible to kind of turn into a Newspaper article view?
Existing format:
ID                 Text
1                  Mary
2                  David
3                  John
4                  Tom

Desired format:
ID                       Text                    ID                   Text
1                        Mary                    2                      David
3                        David                   4                      Tom
0
Comment
Question by:pvsbandi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
Nakul Vachhrajani earned 500 total points
ID: 41848054
Per relational theory, no. Database systems follow SET theory and in a SET, you have n-number of tuples, i.e. rows with each column in the row describing the row.

In a "newspaper-like" view, the representation is such that we have two distinct set of tuples on the same row. This violates the SET theory, and hence, the recommendation would be to do these representational adjustments/formatting strictly in the application layer/UI layer.

That being said, there are always ways to work around, but they are subject to terms & conditions (for example, in the sample code below, the Ids have to be integers). And these "workarounds" come with severe penalties that the team may take during product maintenance and performance.

USE tempdb;
GO
DECLARE @newspaperSample TABLE ([Id] INT NOT NULL,
                                [Text] VARCHAR(10) NOT NULL
                               );

INSERT INTO @newspaperSample ([Id], [Text])
VALUES ('1', 'Mary'),
       ('2', 'David'),
       ('3', 'John'),
       ('4', 'Tom');

SELECT NewsPaperColumn1.[Id]   AS NewsPaperColumn1Id,
       NewsPaperColumn1.[Text] AS NewsPaperColumn1Text,
       NewsPaperColumn2.[Id]   AS NewsPaperColumn2Id,
       NewsPaperColumn2.[Text] AS NewsPaperColumn2Text
FROM @newspaperSample AS NewsPaperColumn1
LEFT OUTER JOIN @newspaperSample AS NewsPaperColumn2 ON NewsPaperColumn1.[Id] = (NewsPaperColumn2.[Id] - 1)
WHERE ((NewsPaperColumn1.[Id] % 2) > 0)  --Odds
  AND ((NewsPaperColumn2.[Id] % 2) = 0); --Evens

/* RESULTS
NewsPaperColumn1Id NewsPaperColumn1Text NewsPaperColumn2Id NewsPaperColumn2Text
------------------ -------------------- ------------------ --------------------
1                  Mary                 2                  David
3                  John                 4                  Tom
*/

Open in new window


Again, my vote would be that although it is possible, NEVER to do this in T-SQL. Do this on the application layer/UI.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41848059
Mind that a database engine is only a data storage system.
You'll need an application or a reporting tool to format the data as you want.
0
 

Author Closing Comment

by:pvsbandi
ID: 41848087
Thanks, Nakul!
  This is not for storage, but for display /for reporting purposes.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41848095
This is not for storage, but for display /for reporting purposes.
I know and that's why I told about a reporting tool. Having SQL Server engine formatting the data is not really the best thing to do.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question