I do not have very good knowledge in advanced SQL. I have been facing a problem for a long time to show data in a specific way.
Desired output as follows
+-------------+------------+------+------+------+----------+------+------+------+----------+------+------+------+----------+--+
| DisplayText | Type | BC | AK | NH | 2010 FYA | BC | AK | NH | 2011 FYA | BC | AK | NH | 2012 FYA | |
+-------------+------------+------+------+------+----------+------+------+------+----------+------+------+------+----------+--+
| Key Drugs | GROUP | NULL | NULL | NULL | | NULL | NULL | NULL | | NULL | NULL | NULL | | |
| R&D | LINEITEM | 10 | 99 | 12 | | 56 | 26 | 21 | | 11 | 87 | 71 | | |
| Net Revenue | LINEITEM | 12 | 20 | 17 | | 79 | 12 | 22 | | 55 | 17 | 27 | | |
| Key Fin | GROUP | NULL | NULL | NULL | | NULL | NULL | NULL | | NULL | NULL | NULL | | |
| Diluted | LINEITEM | 10 | 99 | 12 | | 56 | 26 | 21 | | 11 | 87 | 71 | | |
+-------------+------------+------+------+------+----------+------+------+------+----------+------+------+------+----------+--+
I have two tables called tblCSMModels & tblOutputDetl. The tblCSMModels table has parent child data, the relationship is established with ID and ParentID.
Screenshot of the table with sample data:
Group are master and line items are child. each group may have multiple child line item and a group may child group too which can have another line items as child.
Here I am showing the table structures and sample data as a result anyone can reproduce this at their end.
CREATE TABLE [dbo].[tblCSMModels]
(
[Ticker] [varchar](20) NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NULL,
[DisplayText] [varchar](max) NULL,
[Type] [nchar](10) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tblCSMModels] ON
GO
INSERT INTO [dbo].[tblCSMModels] ([Ticker], [ID], [ParentID], [DisplayText], [Type])
VALUES (N'ANAB', 1, 0, N'Key Drugs', N'GROUP ')
INSERT INTO [dbo].[tblCSMModels] ([Ticker], [ID], [ParentID], [DisplayText], [Type])
VALUES (N'ANAB', 2, 1, N'R&D, Proforma', N'LINEITEM ')
INSERT INTO [dbo].[tblCSMModels] ([Ticker], [ID], [ParentID], [DisplayText], [Type])
VALUES (N'ANAB', 3, 1, N'Net Revenue', N'LINEITEM ')
INSERT INTO [dbo].[tblCSMModels] ([Ticker], [ID], [ParentID], [DisplayText], [Type])
VALUES (N'ANAB', 4, 0, N'Key Fin', N'GROUP ')
INSERT INTO [dbo].[tblCSMModels] ([Ticker], [ID], [ParentID], [DisplayText], [Type])
VALUES (N'ANAB', 5, 4, N'Diluted', N'LINEITEM ')
GO
CREATE TABLE [dbo].[tblOutputDetl]
(
[Ticker] [varchar](20) NULL,
[Section] [varchar](max) NULL,
[LineItem] [varchar](max) NULL,
[Broker] [varchar](max) NULL,
[Period] [varchar](20) NULL,
[ItemValue] [decimal](18, 2) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT INTO tblOutputDetl (Ticker, Section, LineItem, Broker, Period, ItemValue)
VALUES ('ANAB','Consensus Model','R&D','BC','2010 FYA',10),
('ANAB','Consensus Model','R&D','BC','2011 FYA',56),
('ANAB','Consensus Model','R&D','BC','2012 FYA',11),
('ANAB','Consensus Model','R&D','AK','2010 FYA',99),
('ANAB','Consensus Model','R&D','AK','2011 FYA',26),
('ANAB','Consensus Model','R&D','AK','2012 FYA',87),
('ANAB','Consensus Model','R&D','NH','2010 FYA',12),
('ANAB','Consensus Model','R&D','NH','2011 FYA',21),
('ANAB','Consensus Model','R&D','NH','2012 FYA',71),
('ANAB','Consensus Model','Net Revenue','NH','2010 FYA',12),
('ANAB','Consensus Model','Net Revenue','NH','2011 FYA',79),
('ANAB','Consensus Model','Net Revenue','NH','2012 FYA',55),
('ANAB','Consensus Model','Net Revenue','NH','2010 FYA',20),
('ANAB','Consensus Model','Net Revenue','NH','2011 FYA',12),
('ANAB','Consensus Model','Net Revenue','NH','2012 FYA',17),
('ANAB','Consensus Model','Net Revenue','NH','2010 FYA',21),
('ANAB','Consensus Model','Net Revenue','NH','2011 FYA',22),
('ANAB','Consensus Model','Net Revenue','NH','2012 FYA',27),
('ANAB','Key Drugs','Diluted','BC','2010 FYA',10),
('ANAB','Key Drugs','Diluted','BC','2011 FYA',56),
('ANAB','Key Drugs','Diluted','BC','2012 FYA',11),
('ANAB','Key Drugs','Diluted','AK','2010 FYA',99),
('ANAB','Key Drugs','Diluted','AK','2011 FYA',26),
('ANAB','Key Drugs','Diluted','AK','2012 FYA',87),
('ANAB','Key Drugs','Diluted','NH','2010 FYA',12),
('ANAB','Key Drugs','Diluted','NH','2011 FYA',21),
('ANAB','Key Drugs','Diluted','NH','2012 FYA',71)
This way tried but could not write the correct SQL which produces the desired output.
;WITH DirectReports (Ticker, ID, ParentID, DisplayText, Type) AS
(
SELECT Ticker, ID, ParentID, DisplayText, Type
FROM tblCSMModels AS e
WHERE isnull(ParentID ,0) = 0
UNION ALL
SELECT e.Ticker, e.ID, e.ParentID, e.DisplayText, e.Type
FROM tblCSMModels AS e
INNER JOIN DirectReports AS d ON e.ParentID = d.ID
)
SELECT *
FROM
(SELECT r.Ticker, r.DisplayText, r.Type, d.Broker, d.ItemValue, d.Period
FROM DirectReports r
INNER JOIN tblOutputDetl d ON (r.Ticker = d.Ticker AND r.DisplayText = d.LineItem)
WHERE r.Ticker = 'ANAB') t
PIVOT
(MAX(ItemValue)
FOR Broker IN ([BC], [AK], [NH])
) AS P
Basically I want to show parent data and their related child. Broker will be coming horizontally and ItemValue will be placed under broker. after showing all broker horizontally then one Period will come say 2010 FYA.
Period will be sorted like 2010 FYA, 2011 FYA, 2012 FYA, 1Q 2013A,2Q 2013A,3Q 2013A,4Q 2013A, 2013 FYA. Period is not fixed. it will vary ticker wise. some ticker period start from 2010 FY to 2080 FY
Broker is also not fixed. Here i mention 3 brokers but it can be 40 or more in real scenario.
This way specific period wise Broker data will be shown horizontally. so all brokers and period will be showing horizontally. see my output first at top with in ASCII table.
Under each period of every row SUM of Broker value should be shown.
I can understand to get desired output SQL will be very complex and I am not being able to write that SQL. So my request please some one help me. I badly stuck at this SQL from morning.