Solved

Newspaper Article view in SQL Server 2012

Posted on 2016-10-18
4
79 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 14

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 51

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 51

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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 part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

630 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