使用SQL Server中的联接获取此输出时出现问题
我想显示这样的输出使用SQL Server中的联接获取此输出时出现问题,sql,sql-server,join,Sql,Sql Server,Join,我想显示这样的输出 +------------+--------------+---------------------+---------------+-----------+--------------+-----------+ | segment_id | segment_name | segment_description | tot_questions | tot_marks | marks_obtain | neg_marks | +------------+------------
+------------+--------------+---------------------+---------------+-----------+--------------+-----------+
| segment_id | segment_name | segment_description | tot_questions | tot_marks | marks_obtain | neg_marks |
+------------+--------------+---------------------+---------------+-----------+--------------+-----------+
| 10006 | MCQ SECTION | | 5 | 20 | | -.5 |
| 10007 | Non-MCQ | | 5 | 20 | | 0 |
+------------+--------------+---------------------+---------------+-----------+--------------+-----------+
这是我的表,将用于连接
MockTestMaster(使用列tot_标记)
模拟测试详细信息(段id、段名称、tot问题)
prd_模板_问题_类型(使用列neg_标记)
prd_templatesection(我可以使用此表,但在我的应用程序中,用户可以添加一个节,该节不会插入此表中,因为它将更改原始模板,因此我将该节插入MockTestDetails表中)
现在获取我的输出的问题是prd\u模板\u问题类型
表的marks\u if\u error
列
现在这里是如何获得负分数的交易:如果prd\u模板部分的sectiontype\u isMcq
为true
,则在prd\u模板问题类型的列中抓取分数
我尝试了以下查询:
SELECT DISTINCT
md.section_id AS segment_id,
md.section_name AS segment_name,
'' AS segment_description,
md.total_section_question as tot_questions,
total_Marks AS tot_marks,
'' AS marks_obtain,
'' AS neg_marks
--prd_template_question_type.marks_if_wrong AS neg_marks
FROM
dbo.MocktestDetails md
INNER JOIN
dbo.MockTestMaster mm ON mm.test_id = md.test_id
INNER JOIN
prd_templatesection pts ON pts.template_id = mm.template_id
WHERE
mm.test_id = 1;
下面是一个您可以使用以下代码
WITH cte_sales AS (
SELECT DISTINCT md.section_id AS segment_id,
md.section_name AS segment_name,
'' AS segment_description,
md.total_section_question AS tot_questions,
total_Marks AS tot_marks,
'' AS marks_obtain,
pts.template_id AS template_id
--prd_template_question_type.marks_if_wrong AS neg_marks
FROM dbo.MocktestDetails md
INNER JOIN dbo.MockTestMaster mm
ON mm.test_id = md.test_id
INNER JOIN prd_templatesection pts
ON pts.template_id = mm.template_id
WHERE mm.test_id = 1
)
SELECT segment_id,
segment_name,
segment_description,
tot_questions,
tot_marks,
marks_obtain,
COALESCE(
(
SELECT TOP 1 marks_if_wrong
FROM prd_template_question_type AS ptqt
JOIN prd_templatesection AS pt
ON ptqt.template_id = pt.template_id
WHERE ptqt.template_id = cte.template_id
AND pt.section_id = cte.segment_id
),
'0'
)
FROM cte_sales AS cte
+-------------+-------------+----------------+----------------+
| template_id | is_mcq_type | is_nonmcq_type | marks_if_wrong |
+-------------+-------------+----------------+----------------+
| 1 | 1 | 0 | -0.5 |
| 1 | 0 | 1 | 0 |
+-------------+-------------+----------------+----------------+
+-------------+------------+--------------+------------------------+-------------------+
| template_id | section_id | section_name | total_section_question | sectiontype_isMcq |
+-------------+------------+--------------+------------------------+-------------------+
| 1 | 10006 | MCQ SECTION | 5 | true |
| 1 | 10007 | Non-MCQ | 5 | false |
+-------------+------------+--------------+------------------------+-------------------+
SELECT DISTINCT
md.section_id AS segment_id,
md.section_name AS segment_name,
'' AS segment_description,
md.total_section_question as tot_questions,
total_Marks AS tot_marks,
'' AS marks_obtain,
'' AS neg_marks
--prd_template_question_type.marks_if_wrong AS neg_marks
FROM
dbo.MocktestDetails md
INNER JOIN
dbo.MockTestMaster mm ON mm.test_id = md.test_id
INNER JOIN
prd_templatesection pts ON pts.template_id = mm.template_id
WHERE
mm.test_id = 1;
WITH cte_sales AS (
SELECT DISTINCT md.section_id AS segment_id,
md.section_name AS segment_name,
'' AS segment_description,
md.total_section_question AS tot_questions,
total_Marks AS tot_marks,
'' AS marks_obtain,
pts.template_id AS template_id
--prd_template_question_type.marks_if_wrong AS neg_marks
FROM dbo.MocktestDetails md
INNER JOIN dbo.MockTestMaster mm
ON mm.test_id = md.test_id
INNER JOIN prd_templatesection pts
ON pts.template_id = mm.template_id
WHERE mm.test_id = 1
)
SELECT segment_id,
segment_name,
segment_description,
tot_questions,
tot_marks,
marks_obtain,
COALESCE(
(
SELECT TOP 1 marks_if_wrong
FROM prd_template_question_type AS ptqt
JOIN prd_templatesection AS pt
ON ptqt.template_id = pt.template_id
WHERE ptqt.template_id = cte.template_id
AND pt.section_id = cte.segment_id
),
'0'
)
FROM cte_sales AS cte