我有3张桌子,每张桌子都包含学生的不同信息(例如个人详细信息,课程详情,学术细节等),其中学生有4个不同的类别(SC,ST,OBC和Gen)。我想根据类别和Plus2Percentage检索学生列表。 例如。 1.我想从SC类别中检索2名学生,其Plus2Percentage> = 60, 然后 我想从ST类别中检索2名学生,其Plus2Percentage> = 65, 然后 我想从OBC类别中检索1名学生,其Plus2Percentage> = 60, 然后 我想从All 4(SC,ST,OBC和Gen)类别中检索2名学生,其Plus2Percentage> = 70,但在此我不想检索那些已经检索过的学生信息。(例如,来自SC类别已从ST OBC类别中已从步骤中检索过等等)
[表1] :
| Roll No | Applicant Name| Gender | Category | Father's Name |
|------------|---------------|------------|------------|----------------|
| 001 | A | M | SC | as |
| 002 | B | F | ST | hg |
| 003 | C | F | ST | yj |
| 004 | D | M | OBC | uy |
| 005 | E | F | SC | bn |
| 006 | F | M | OBC | kl |
| 007 | E | F | Gen | bn |
| 008 | F | M | OBC | vg |
| 009 | E | F | Gen | gh |
| 010 | F | M | SC | we |
|------------|---------------|------------|------------|----------------|
[表2] :
| ID | Semester | Major | Applied Course|
|------------|---------------|------------|---------------|
| 001 | 1 | English | B.A |
| 002 | 1 | English | B.A |
| 003 | 1 | History | B.A |
| 004 | 1 | botany | B.Sc |
| 005 | 1 | Hindi | B.A |
| 006 | 1 | History | B.A |
| 007 | 1 | Maths | B.A |
| 008 | 1 | Hindi | B.A |
| 009 | 1 | History | B.A |
| 010 | 1 | Pol.Science| B.A |
|------------|---------------|------------|---------------|
[表3] :
| ID |Plus2Percentage|
|------------|---------------|
| 001 | 60 |
| 002 | 65 |
| 003 | 70 |
| 004 | 73 |
| 005 | 87 |
| 006 | 91 |
| 007 | 59 |
| 008 | 78 |
| 009 | 88 |
| 010 | 57 |
|------------|---------------|-
[输出] :
| Roll No |Plus2Percentage| Category |
|------------|---------------|-----------|
| 005 | 87 | SC |
| 001 | 60 | SC |
| 003 | 70 | ST |
| 002 | 65 | ST |
| 006 | 91 | OBC |
| 009 | 88 | Gen |
| 008 | 78 | OBC |
|------------|---------------|-----------|
2个SC类别的学生,其百分比高于或等于60%。 从sc。
2名ST类学生,其百分比高于或等于65%。 来自圣
1 OBC类别的学生,其百分比高于或等于60%。 来自OBC的006号卷 以及
2所有类别的学生,其百分比超过70%,但不包括以前检索过的学生。 来自各地的009号和008号卷
以前正在使用的代码,当我从1表中检索数据时,而不是3个表:
WITH PRIMARY_CHOICE AS (
SELECT
RollNo,
ApplicantName,
FatherName,
Gender,
Major,
Category,
Plus2Percentage
FROM (
SELECT
RollNo,
ApplicantName,
FatherName,
Gender,
Semester,
Major,
AppliedCourse,
Category,
Plus2Percentage,
row_number() over (partition by Category, Semester, Major, AppliedCourse order by Plus2Percentage desc) as rn
FROM [College Management System].[dbo].[ApplicantPersonalDetail]
) as T
WHERE
rn <= CASE
WHEN Category='SC' AND Semester='1' AND AppliedCourse= 'B.A' AND Plus2Percentage >= '60' THEN '2'
WHEN Category='ST' AND Semester= '1' AND AppliedCourse= 'B.A' AND Plus2Percentage >= '65'THEN '2'
WHEN Category='OBC' AND Semester= '1' AND AppliedCourse= 'B.A' AND Plus2Percentage >= '60' THEN '1'
ELSE 0
END
)
SELECT
RollNo,
ApplicantName,
FatherName,
Gender,
Major,
Category,
Plus2Percentage
FROM PRIMARY_CHOICE
UNION ALL
SELECT
RollNo,
ApplicantName,
FatherName,
Gender,
Major,
Category,
Plus2Percentage
FROM (
SELECT
RollNo,
ApplicantName,
FatherName,
Gender,
Semester,
Major,
AppliedCourse,
Category,
Plus2Percentage,
row_number() over (partition by Semester, Major1, AppliedCourse order by Plus2Percentage desc) as rn
FROM [College Management System].[dbo].[ApplicantPersonalDetail] x
WHERE NOT EXISTS (
select 1 from primary_choice y
where x.RollNo = y.RollNo
)
) AS T2
WHERE
rn <= 2
AND Semester = @semester
AND AppliedCourse = 'B.A'
AND Plus2Percentage >= 70
order by Plus2Percentage desc
答案 0 :(得分:1)
SELECT RollNo, Plus2Percentage, Category
FROM TABLE1 a
INNER JOIN Table3 b on a.rollno=b.id
WHERE a.category='SC' and b.Plus2Percentage>=60
前三个子弹应该足够了。第四个是设置谓词或封装前三个联合然后执行NOT IN的问题。
虽然我觉得很难相信知道足够的SQL使用ROW_NUMBER
的人无法解决JOIN
问题。
答案 1 :(得分:0)
我已经实现了输出但是查询成本很高,如果你有很多数据将每个分钟填充到你的数据库中,那么这个查询可能不支持执行时间。
我使用了CTE表和UNION子句来生成输出 - 我没有使用你的table2,因为输出中没有表格所需的数据或者不依赖于table2
SQL SELECT CODE - 您可以相应地替换表名和列名 - 评论后编辑: - 解决方案被改为从表变量读取数据的程序
CREATE PROCEDURE usp_SelectCategorywiseData
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tbl_LIST TABLE (RollNo int, [Plus2Percentage] int, Category varchar(10));
WITH CTE AS
(
SELECT A.RollNo, P.Percentage AS [Plus2Percentage], A.Category
, row_number() OVER (PARTITION BY A.Category ORDER BY P.Percentage DESC) AS Rank
FROM APPLICANT A INNER JOIN Plus2Percentage P ON A.RollNo = P.ID
)
INSERT INTO @tbl_LIST
SELECT RollNo, Plus2Percentage as [Plus2Percentage], Category
FROM CTE
WHERE rank <=
CASE
WHEN Category='SC' AND [Plus2Percentage] >= '60' THEN '2'
WHEN Category='ST' AND [Plus2Percentage] >= '60' THEN '2'
WHEN Category='OBC' AND [Plus2Percentage] >= '55' THEN '1'
ELSE 0
End
INSERT INTO @tbl_LIST
SELECT TOP 2 A.RollNo, P.Percentage as [Plus2Percentage], A.Category
FROM APPLICANT A INNER JOIN Plus2Percentage P ON A.RollNo = P.ID
WHERE Percentage > 70 and RollNo NOT IN (SELECT RollNo FROM @tbl_LIST) ORDER BY P.Percentage DESC
SELECT * FROM @tbl_LIST
END
您需要做的是 - 创建一个临时表,其中包含SC,ST,OBC类别的前5条记录的数据
然后按照desc的百分比顺序插入所有类别TOP 2记录的记录,其中Rollno不在临时表中。这样就可以排除重复项并获得数据。
ORDER BY
不应该被用来作为订购数据可以在以后完成 - 这将花费执行时间。
我将在这里解释所有条款的使用: