Solved

Newspaper Article view in SQL Server 2012

Posted on 2016-10-18
4
57 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 11

Accepted Solution

by:
Nakul Vachhrajani earned 500 total points
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
Thanks, Nakul!
  This is not for storage, but for display /for reporting purposes.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now