【发布时间】: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
...
例如,如果使用等于1 的versid(对应于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/…
-
这似乎不是为我编写代码的问题——因为您没有问题。但是,如果您的问题是我应该编写什么代码,那么它就是为我编写代码。那么......你的问题是什么?
标签: sql sql-server tsql database-design