Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 是否可以编写具有多个动态数据透视的查询?_Sql_Sql Server_Tsql_Database Design - Fatal编程技术网

Sql 是否可以编写具有多个动态数据透视的查询?

Sql 是否可以编写具有多个动态数据透视的查询?,sql,sql-server,tsql,database-design,Sql,Sql Server,Tsql,Database Design,我有像这样的桌子 Partners =============== id | name --------------- 1 | "John" 2 | "Jacob" Regions ==================== id | name -------------------- 1 | "Antarctica" 2 | "Coruscant" 3 | "Iraq" Destinations =========================

我有像这样的桌子

   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
因此,它基本上是使用
合作伙伴
作为行,使用所有其他相关信息作为列。您可以看到,对于尚未填写的问题,它的答案为零


很抱歉,如果这看起来像是一个“为我编写代码”的问题,但我确实花了很多精力来编写它

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

SQL数据透视的示例如下:

普通SQL:

USE AdventureWorks2008R2 ;
GO
SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost 
FROM Production.Product
GROUP BY DaysToManufacture;
Pivot 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;

很好,您可以发布带有示例数据的表,但这样会更好。将文本转换为脚本需要时间。这里是一个很好的起点。这看起来不像是一个为我编写代码的问题——因为您没有问题。然而,如果你的问题是我应该写什么代码,那么这就是为我写代码。所以你的问题是什么?