We help IT Professionals succeed at work.

EX-Singles and Partners

wdarnellg
wdarnellg asked
on
I have a need to alter a view so that it displays the result set as data in rows. i.e.

I am getting the data from existing tables. The query works as designed, but the data is being used to create a pie chart. The problem is the returned data is in columns and I need it in rows to fill the chart's x,y axis'. I thought it may be possible to create a column from a variable that would display the current view in rows, but I am not even close it seems. Below is my current hack job that has all kinds of syntax errors.
Help is appreciated.
 
ALTER VIEW [dbo].[vw_sj_TotalSingleOrPartneredFamily]
FamilyTypes nvarchar(50)
AS
SET FamilyTypes = (SUM(CASE WHEN P.OtherParentID > 0 THEN 1 ELSE 0 END) AS PartneredFamily, SUM(CASE WHEN P.OtherParentID IS NULL OR
p.OtherParentID = 0 THEN 1 ELSE 0 END) AS SingleFamily), ISNULL(COUNT(p.LastName), 0)[TotalFamilies]
FROM dbo.sj_Players AS p INNER JOIN
dbo.sj_ParentGuardian AS pg ON p.ParentID = pg.ParentID LEFT OUTER JOIN
dbo.sj_ParentGuardian AS opg ON p.OtherParentID = opg.ParentID

Open in new window

Comment
Watch Question

Commented:
Hi wdarnellg,

First of all, your ALTER VIEW statement looks strange :) Could you post actual view code, so we could understand you DB structure?

If you want to transform columns into rows, you should use UNPIVOT statement:
http://msdn.microsoft.com/en-us/library/ms177410.aspx

I could help you with statement syntax, but you should better explain your table structure, and what results are you trying achieve. Examples would be welcome :)

Author

Commented:
Thank you for looking at this. Here is a create script that I hope gives you the right info.
/****** Object:  View [dbo].[vw_sj_TotalSingleOrPartneredFamily]    Script Date: 12/06/2011 05:49:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[vw_sj_TotalSingleOrPartneredFamily]
AS
SELECT      SUM(CASE WHEN P.OtherParentID > 0 THEN 1 ELSE 0 END) AS PartneredFamily, SUM(CASE WHEN P.OtherParentID IS NULL OR
                      p.OtherParentID = 0 THEN 1 ELSE 0 END) AS SingleFamily, ISNULL(COUNT(p.LastName), 0)[TotalFamilies]
FROM         dbo.sj_Players AS p INNER JOIN
                      dbo.sj_ParentGuardian AS pg ON p.ParentID = pg.ParentID LEFT OUTER JOIN
                      dbo.sj_ParentGuardian AS opg ON p.OtherParentID = opg.ParentID
                      


GO

EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "p"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 123
               Right = 214
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "pg"
            Begin Extent = 
               Top = 6
               Left = 252
               Bottom = 123
               Right = 428
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "opg"
            Begin Extent = 
               Top = 6
               Left = 466
               Bottom = 123
               Right = 642
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 12
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vw_sj_TotalSingleOrPartneredFamily'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'vw_sj_TotalSingleOrPartneredFamily'
GO

Open in new window

Commented:
Actually, you don't need UNPIVOT for that. Try this:

;WITH f AS 
(
   SELECT CASE WHEN OtherParentID > 0 THEN 'PartneredFamily' ELSE 'SingleFamily' END AS FamilyStatus 
   FROM dbo.sj_Players
)
SELECT ISNULL(FamilyStatus, 'TotalFamilies') AS FamilyStatus, COUNT(*) AS FamilyCount 
FROM f GROUP BY FamilyStatus WITH ROLLUP

Open in new window

Commented:
Or, if you need a view:
CREATE VIEW SomeView AS
SELECT ISNULL(FamilyStatus, 'TotalFamilies') AS FamilyStatus, COUNT(*) AS FamilyCount 
FROM (
	SELECT CASE WHEN OtherParentID > 0 THEN 'PartneredFamily' ELSE 'SingleFamily' END AS FamilyStatus 
	FROM dbo.sj_Players) AS f 
GROUP BY FamilyStatus WITH ROLLUP

Open in new window

Author

Commented:
I must say that I am amazed! Mostly because I am having a bit of an AHA! moment over the use of CASE, WHEN, THEN. I am going to need more chart data from this database soon so I am glad to see how elegantly the properly formatted info can be obtained.
Thank you very much.

Author

Commented:
Great Help!