是否可以编写具有多个动态支点的查询?

时间:2016-04-08 18:04:39

标签: sql sql-server tsql database-design

我有像

这样的表格
   Partners
===============
id |   name 
---------------
 1 | "John" 
 2 | "Jacob"


   Regions
====================
id | name
--------------------
 1 | "Antarctica"
 2 | "Coruscant"
 3 | "Iraq"

      Destinations 
============================
id | partner_id | region_id 
----------------------------
1  |     1      |    1
2  |     1      |    2
3  |     2      |    2

    Surveys
================
id | title
----------------
 1 | "Part 1"
 2 | "Bonus" 


      Versions 
======================
id |    title   
----------------------
 1 | "First Version"
 2 | "Version #2" 


              Permissions
==========================================
id | partner_id | survey_id | version_id
------------------------------------------
1  |     1      |     1     |     1
2  |     1      |     1     |     2
3  |     2      |     1     |     1


             Sections
=======================================
id | survey_id |   title 
---------------------------------------
 1 |    1      | "Some Section"
 2 |    1      | "Some Other Section"
 3 |    2      | "Yet Another Section"


        Subsections
=================================
id |    title    | section_id 
--------------------------------
 1 | "Subsec"    |     1
 2 | "Subsecc"   |     1
 3 |   "Ss"      |     2 
 4 |  "kasjhja"  |     3
 5 | "aslkdjas"  |     3


                         Questions
===================================================================
id | subsection_id |             qtext            |   version_id
-------------------------------------------------------------------
 1 |      1        | "What is 1+1?"               |       1
 2 |      1        | "What is 2+2?"               |       1
 3 |      1        | "What is one plus one?"      |       2
 4 |      1        | "What is two plus two?"      |       2
 5 |      2        | "How are you doing?"         |       1
 6 |      2        | "What's up?"                 |       2 
 7 |      3        | "What would you rate her?"   |       1
 8 |      3        | "What would you rate her?"   |       2
 9 |      4        | "Number of bits in a byte?"  |       1
10 |      5        | "What year is it?"           |       1
11 |      5        | "The year is ...."?          |       2

              Answers
========================================
id | question_id | partner_id |   val 
---------------------------------------- 
 1 |      1      |     1      |  2 
 2 |      1      |     2      |  2
 3 |      2      |     1      |  4 
 4 |      2      |     2      |  4
 5 |      3      |     1      |  2
 6 |      4      |     1      |  69
 7 |      5      |     1      |  55
 8 |      6      |     1      |  10
 9 |      7      |     1      |  9
10 |      8      |     1      |  10
11 |      9      |     1      |  8
12 |     10      |     1      |  2016
13 |     11      |     1      |  2016


            MarkedAsFinished
===========================================
 id | partner_id | survey_id | version_id 
-------------------------------------------
  1 |    1       |    1      |     1
  2 |    1       |    2      |     1
  3 |    1       |    1      |     2
  4 |    2       |    1      |     1

其中PK / FK关系从我的命名约定中不言自明。如果可能的话,我想要一个查询

CREATE PROCEDURE AnswerDump 
   @versid INT
AS
   ... 
例如,如果在versid(对应Versions.id)等于1的情况下执行,则会返回以下表格。

   name    | Destined for Antarctica? | Destined for Coruscant? | Destined for Iraq? | Finished with Part 1? | Finished with Bonus? | Permission to contact about Part 1? | Permission to contact about Bonus? | What is 1+1? | What is 2+2? | How are you doing? | What would you rate her? | Number of bits in a byte? | What year is it?
=============================================================================================================================================================================================================================================================================================================================================
 "John"    |          Yes             |           Yes           |        No          |          Yes          |         Yes          |                Yes                  |                   No               |       2      |      4       |        55          |             9            |             8             |       2016
 "Jacob"   |          No              |           Yes           |        No          |          Yes          |         No           |                Yes                  |                   No               |       2      |      4       |        0           |             0            |             0             |        0

所以它基本上做的是将Partners用作行,将所有其他相关信息用作列。您可以看到,对于尚未填写的问题,它会填写零答案。

很抱歉,如果这看起来像是“为我编写代码”问题,但我确实付出了很多努力来编写它......

1 个答案:

答案 0 :(得分:0)

是的,它可能,但在SQL视图中没有,您需要在SQL函数或SP中开发它们。

SQL数据透视的示例如下:

普通SQL:

USE AdventureWorks2008R2 ;
GO
SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost 
FROM Production.Product
GROUP BY DaysToManufacture;

透视SQL:

-- Pivot table with one row and five columns
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, 
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost 
    FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;