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

Posted

技术标签:

【中文标题】是否可以编写具有多个动态枢轴的查询?【英文标题】:Is it possible to write a query with multiple dynamic pivots? 【发布时间】:2016-04-08 18:04:39 【问题描述】:

我有这样的桌子

   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
   ... 

例如,如果使用等于1versid(对应于Versions.id)执行,则将返回下表。

   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 用作行,将所有其他相关信息用作列。您可以看到它为尚未填写的问题填写了零的答案。

抱歉,这似乎是一个“为我编写代码”的问题,但我确实付出了很多努力来编写它......

【问题讨论】:

很高兴您发布带有示例数据的表格,但sqlfiddle.com 会好得多。将文本转换为脚本需要时间。 这是一个很好的起点。 spaghettidba.com/2015/04/24/… 这似乎不是为我编写代码的问题——因为您没有问题。但是,如果您的问题是我应该编写什么代码,那么它就是为我编写代码。那么......你的问题是什么? 【参考方案1】:

是的,有可能,但在 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;

【讨论】:

以上是关于是否可以编写具有多个动态枢轴的查询?的主要内容,如果未能解决你的问题,请参考以下文章

获取具有多个枢轴的两列的总和

是否可以为视图编写动态查询?

具有多个 postmeta 左连接的 Wordpress 自定义 SQL 查询

是否可以在单个查询中编写两个具有两个条件的 UPDATE 函数?

您如何在 Robot Framework 中编写具有多个标识符的动态元素?

具有动态查询的 JPA 也是 Hibernate 中的动态(可选)参数