Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

Newspaper Article view in SQL Server 2012

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
pvsbandi
Asked:
pvsbandi
  • 2
1 Solution
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
pvsbandiAuthor Commented:
Thanks, Nakul!
  This is not for storage, but for display /for reporting purposes.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now