我如何最好地实现这个大规模的查询?

时间:2016-02-03 07:26:32

标签: sql sql-server tsql join database-design

所以我有像

这样的表格
--                  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

其中关联是不言自明的(section_id中的Subsections引用id中的Sectionspartner_id中的Answers引用{ {1}}中的{1}}等。

我尝试做的是编写一个sproc,对于给定的id,返回合作伙伴已回答的每个问题的行,Partners表示答案值和答案ID他们没有回答的任何问题,以及问题的文本,问题的id,部分和章节的标题。

问题1:

以来
Partner.id
每次调用sproc时,sproc返回的表的部分都是相同的,我应该创建一个返回该表的单独的sproc吗?为什么或为什么不呢?

编辑: 问题2:以下内容对整个查询是否正确?

NULL

???

正如您所知,我是数据库编程的n00b。

1 个答案:

答案 0 :(得分:0)

这些方面的一些事情应该可以解决问题,从所有合作伙伴的列表开始。对于始终满足条件的问题的INNER JOIN将返回每个合作伙伴的每个问题。然后,您可以根据合作伙伴ID LEFT JOIN输出到Answers,并添加一个子句来过滤特定的question_id

    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 Partner
    INNER JOIN Questions on 1 = 1
    INNER JOIN Subsections on Subsections.Id = Questions.subsection_id
    INNER JOIN Sections on Sections.Id = Subsections.section_id
    LEFT JOIN Answers on Answers.partner_id = Partner.Id
            AND Answers.questions_id = Questions.Id
    WHERE Partner.Id = @pid

您发布的初始查询可以使用,但这看起来更清晰。