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
Solved

Newspaper Article view in SQL Server 2012

Posted on 2016-10-18
4
74 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
  • 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 48

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 48

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

860 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