【发布时间】:2016-05-12 06:52:47
【问题描述】:
所以我有像
这样的表格-- Partners
-- ============================================================
-- id | name | email
-- ============================================================
-- 1 | 'Haliburton' | 'DCheney@Haliburton.org'
-- 2 | 'Berkshire Hathaway' | 'WarrenBridgemaster@bershire.org'
-- 3 | 'Jason' | 'jason89@gmail.com'
-- Sections
-- =========================================
-- id | title
-- =========================================
-- 1 | 'Partner planning'
-- 2 | 'Partner prospecting'
-- 3 | 'Partner recruiting'
-- 4 | 'Partner activation'
-- 5 | 'Partner growth'
-- Subsections
-- ==============================================
-- id | title | section_id
-- ==============================================
-- 1 | 'Target Markets' | 1
-- 2 | 'Target Customers' | 1
-- 3 | 'Competition' | 1
-- 4 | 'Partner Profile' | 2
-- 5 | 'Partner value' | 3
-- 6 | 'Partner Qualification' | 3
-- 7 | 'Business Terms' | 4
-- 8 | 'Getting Traction' | 5
-- Questions
-- ====================================================================================================================
-- id | qtext | subsection_id
-- ====================================================================================================================
-- 1 | 'Have you defined the target markets in terms of industries, verticals and subverticals?' | 1
-- 2 | 'Have you identified the number of potential customers in each state or region?' | 1
-- 3 | 'Have you built a market coverage model estimating the number of partners in each region?' | 1
-- 4 | 'Have you built the revenue model to estimate sales by partners in each region?' | 1
-- 5 | 'Have you built a market coverage model estimating the number of partners in each region?' | 2
-- . . .
-- . . .
-- . . .
-- . . .
-- . . .
-- 61 | 'Have you defined the customer references acquisition process?' | 8
-- Answers
-- =========================================
-- id | question_id | partner_id | val
-- =========================================
-- 1 | 1 | 1 | 24
-- 2 | 2 | 1 | 50
-- 3 1 | 2 | 0
-- 4 | 3 | 1 | 90
其中的关联是不言自明的(Subsections 中的section_id 指的是Sections 中的id,Answers 中的partner_id 指的是Partners 中的id,等等)。
我要做的是编写一个存储过程,对于给定的Partner.id,返回合作伙伴已回答的每个问题的行,NULL 用于回答值,并为他们没有回答的任何问题返回 ID回答,连同问题的文本、anw 的 id、问题的 id、部分和小节的标题。
问题一:自从
-- =============================================================================================
-- section_title | subsection_title | question_id | question_text
-- =============================================================================================
-- 'Partner Planning' | 'Target Markets' | 1 | 'Have you defined the target ...?'
-- 'Partner Planning' | 'Target Markets' | 2 | 'Have you identified the .......?'
-- 'Partner Planning' | 'Target Markets' | 3 | 'Have you built a market .......?'
-- 'Partner Planning' | 'Target Markets' | 4 | 'Have you built the revenue ....?'
-- 'Partner Planning' | 'Target Customers' | 5 | 'Have you defined the ideal ....?'
-- . . . .
-- . . . .
-- . . . .
-- . . . .
-- 'Partner Growth' | 'Getting Traction' | 61 | 'Have you defined the ideal ....?'
每次调用 sproc 时,sproc 返回的表的一部分都是相同的,我是否应该创建一个单独的 sproc 来返回该表?为什么或为什么不?
编辑: 问题 2:以下对于整个查询是否正确?
BEGIN
SELECT Sections.title AS section_title,
Subsections.title AS subsection_title,
Questions.id AS question_id,
Questions.qtext AS question_text,
Answers.id AS answer_id,
Answers.val AS answer_val
FROM
Answers RIGHT OUTER JOIN (
SELECT *
FROM Questions INNER JOIN (
SELECT *
FROM Subsections INNER JOIN Sections
ON Subsections.section_id = Sections.id
)
ON Questions.id = Answers.question_id
)
ON Question.id = Answers.question_id
WHERE Answers.partner_id = @pid
END
???
如您所知,我是数据库编程的 n00b。
【问题讨论】:
-
您在 Q2 中的左连接需要一个条件来仅返回特定合作伙伴的答案,否则无论合作伙伴如何,您都会得到问题的所有答案。除此之外,乍一看还可以,但当然测试可能会发现其他异常情况。
-
@JoachimIsaksson 谢谢。你是对的。
标签: sql sql-server tsql join database-design