1

对于星型模式,我有以下数据库设计:

alll table: ID, user_id, cat_id, qa_id
user table: user_id, name, department
category table: cat_id, cat_text, sub_cat_text
QA table: aq_id, q_id, answer_id
question table: q_id, q_text, sub_q_text
answer table: answer_id, text

我必须提出一个查询,显示每个问题的参与者总数,包括其子问题及其所有答案,即使这些答案中的任何一个都没有参与者。幸运的是,我可以提出一个查询,显示:问题、子问题、每个答案中的参与者总数。然而,现在的问题是展示部门。如果其中一个答案没有参与者,则部门将显示为 NULL。那么如何修改呢?

我的查询:

SELECT     dbo.question.q_text, dbo.question.sub_q_text, COUNT(dbo.[user].user_id) AS Expr1, dbo.answer.text, dbo.[user].department
FROM         dbo.alll INNER JOIN
                      dbo.category ON dbo.alll.cat_id = dbo.category.cat_id INNER JOIN
                      dbo.QA ON dbo.alll.qa_id = dbo.QA.aq_id INNER JOIN
                      dbo.[user] ON dbo.alll.user_id = dbo.[user].user_id INNER JOIN
                      dbo.question ON dbo.QA.q_id = dbo.question.q_id RIGHT OUTER JOIN
                      dbo.answer ON dbo.QA.answer_id = dbo.answer.answer_id
WHERE     (dbo.[user].department = 'pmod') AND (dbo.question.q_text = 'q1') OR
                      (dbo.[user].department IS NULL)
GROUP BY dbo.answer.text, dbo.question.q_text, dbo.question.sub_q_text, dbo.[user].department

这是数据库的脚本:

USE [master]
GO
/****** Object:  Database [survey2]    Script Date: 05/22/2012 13:18:16 ******/
CREATE DATABASE [survey2] ON  PRIMARY 
( NAME = N'survey2', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\survey2.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'survey2_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\survey2_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [survey2] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [survey2].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [survey2] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [survey2] SET ANSI_NULLS OFF
GO
ALTER DATABASE [survey2] SET ANSI_PADDING OFF
GO
ALTER DATABASE [survey2] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [survey2] SET ARITHABORT OFF
GO
ALTER DATABASE [survey2] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [survey2] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [survey2] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [survey2] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [survey2] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [survey2] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [survey2] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [survey2] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [survey2] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [survey2] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [survey2] SET  DISABLE_BROKER
GO
ALTER DATABASE [survey2] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [survey2] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [survey2] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [survey2] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [survey2] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [survey2] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [survey2] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [survey2] SET  READ_WRITE
GO
ALTER DATABASE [survey2] SET RECOVERY SIMPLE
GO
ALTER DATABASE [survey2] SET  MULTI_USER
GO
ALTER DATABASE [survey2] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [survey2] SET DB_CHAINING OFF
GO
USE [survey2]
GO
/****** Object:  Table [dbo].[category]    Script Date: 05/22/2012 13:18:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[category](
    [cat_id] [int] IDENTITY(1,1) NOT NULL,
    [cat_text] [varchar](50) NOT NULL,
    [sub_cat_text] [varchar](50) NULL,
 CONSTRAINT [PK_category] PRIMARY KEY CLUSTERED 
(
    [cat_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[answer]    Script Date: 05/22/2012 13:18:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[answer](
    [answer_id] [int] IDENTITY(1,1) NOT NULL,
    [text] [varchar](50) NOT NULL,
 CONSTRAINT [PK_answer] PRIMARY KEY CLUSTERED 
(
    [answer_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[user]    Script Date: 05/22/2012 13:18:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[user](
    [user_id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [department] [varchar](50) NOT NULL,
 CONSTRAINT [PK_user] PRIMARY KEY CLUSTERED 
(
    [user_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[question]    Script Date: 05/22/2012 13:18:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[question](
    [q_id] [int] IDENTITY(1,1) NOT NULL,
    [q_text] [varchar](50) NOT NULL,
    [sub_q_text] [varchar](50) NULL,
 CONSTRAINT [PK_question] PRIMARY KEY CLUSTERED 
(
    [q_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[QA]    Script Date: 05/22/2012 13:18:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[QA](
    [aq_id] [int] IDENTITY(1,1) NOT NULL,
    [q_id] [int] NOT NULL,
    [answer_id] [int] NOT NULL,
 CONSTRAINT [PK_QA] PRIMARY KEY CLUSTERED 
(
    [aq_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[alll]    Script Date: 05/22/2012 13:18:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[alll](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [user_id] [int] NULL,
    [cat_id] [int] NULL,
    [qa_id] [int] NULL,
 CONSTRAINT [PK_alll] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[showing number of participants in each Q]    Script Date: 05/22/2012 13:18:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[showing number of participants in each Q]
AS
SELECT     COUNT(dbo.question.q_id) AS Expr1, dbo.[user].department, dbo.category.cat_text, dbo.category.sub_cat_text, dbo.question.q_text
FROM         dbo.alll INNER JOIN
                      dbo.answer ON dbo.alll.answer_id = dbo.answer.answer_id INNER JOIN
                      dbo.category ON dbo.alll.cat_id = dbo.category.cat_id INNER JOIN
                      dbo.question ON dbo.alll.q_id = dbo.question.q_id INNER JOIN
                      dbo.[user] ON dbo.alll.user_id = dbo.[user].user_id
GROUP BY dbo.[user].department, dbo.category.cat_text, dbo.category.sub_cat_text, dbo.question.q_text
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 = "alll"
            Begin Extent = 
               Top = 61
               Left = 226
               Bottom = 178
               Right = 386
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "answer"
            Begin Extent = 
               Top = 182
               Left = 0
               Bottom = 269
               Right = 160
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "category"
            Begin Extent = 
               Top = 36
               Left = 426
               Bottom = 138
               Right = 586
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "question"
            Begin Extent = 
               Top = 189
               Left = 427
               Bottom = 291
               Right = 587
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "user"
            Begin Extent = 
               Top = 45
               Left = 0
               Bottom = 147
               Right = 160
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 2265
         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' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'showing number of participants in each Q'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N' = 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'showing number of participants in each Q'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'showing number of participants in each Q'
GO
/****** Object:  ForeignKey [FK_QA_answer]    Script Date: 05/22/2012 13:18:17 ******/
ALTER TABLE [dbo].[QA]  WITH CHECK ADD  CONSTRAINT [FK_QA_answer] FOREIGN KEY([answer_id])
REFERENCES [dbo].[answer] ([answer_id])
GO
ALTER TABLE [dbo].[QA] CHECK CONSTRAINT [FK_QA_answer]
GO
/****** Object:  ForeignKey [FK_QA_question]    Script Date: 05/22/2012 13:18:17 ******/
ALTER TABLE [dbo].[QA]  WITH CHECK ADD  CONSTRAINT [FK_QA_question] FOREIGN KEY([q_id])
REFERENCES [dbo].[question] ([q_id])
GO
ALTER TABLE [dbo].[QA] CHECK CONSTRAINT [FK_QA_question]
GO
/****** Object:  ForeignKey [FK_alll_category]    Script Date: 05/22/2012 13:18:17 ******/
ALTER TABLE [dbo].[alll]  WITH CHECK ADD  CONSTRAINT [FK_alll_category] FOREIGN KEY([cat_id])
REFERENCES [dbo].[category] ([cat_id])
GO
ALTER TABLE [dbo].[alll] CHECK CONSTRAINT [FK_alll_category]
GO
/****** Object:  ForeignKey [FK_alll_QA]    Script Date: 05/22/2012 13:18:17 ******/
ALTER TABLE [dbo].[alll]  WITH CHECK ADD  CONSTRAINT [FK_alll_QA] FOREIGN KEY([qa_id])
REFERENCES [dbo].[QA] ([aq_id])
GO
ALTER TABLE [dbo].[alll] CHECK CONSTRAINT [FK_alll_QA]
GO
/****** Object:  ForeignKey [FK_alll_user]    Script Date: 05/22/2012 13:18:17 ******/
ALTER TABLE [dbo].[alll]  WITH CHECK ADD  CONSTRAINT [FK_alll_user] FOREIGN KEY([user_id])
REFERENCES [dbo].[user] ([user_id])
GO
ALTER TABLE [dbo].[alll] CHECK CONSTRAINT [FK_alll_user]
GO
4

1 回答 1

2

这是我认为您正在寻找的查询。它首先交叉连接用户和问题。现在两者之间有所有可能的组合,您可以开始计算匹配的答案。

编辑:将连接更改为交叉连接,以便能够计算 0。

SELECT 
       dbo.[user].department, 
       dbo.question.q_text, 
       dbo.question.sub_q_text, 
       dbo.answer.text, 
       COUNT(qa.answer_id) AS Users_Answered
  FROM [user]
 CROSS JOIN question
 CROSS JOIN answer
  LEFT JOIN alll
    ON [user].User_ID = alll.User_ID
  LEFT JOIN qa
    ON alll.qa_id = qa.aq_id
   AND question.q_id = qa.q_id
   AND qa.answer_id = answer.answer_id
 GROUP BY      
       dbo.[user].department, 
       dbo.question.q_text, 
       dbo.question.sub_q_text, 
       dbo.answer.text
 ORDER BY 1, 2

你可以检查一下@Sql Fiddle

新版查询@Sql Fiddle

于 2012-05-21T13:38:09.057 回答