gpt4 book ai didi

sql - 使用 SQL 根据记录计数显示结果

转载 作者:行者123 更新时间:2023-12-03 02:03:26 24 4
gpt4 key购买 nike

我有名为 schoolDB 的数据库和 2 个数据库表,

学生教育

创建学生表:

USE [schoolDB]
GO

/****** Object: Table [dbo].[tblStudent] Script Date: 09/22/2013 17:30:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tblStudent](
[STUDENTNUMBER] [varchar](50) NOT NULL,
[STUDENTNAME] [varchar](50) NULL,
[EDUCATIONID] [varchar](50) NULL,
CONSTRAINT [PK_tblStudent] PRIMARY KEY CLUSTERED
(
[STUDENTNUMBER] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

创建教育表:

USE [schoolDB]
GO

/****** Object: Table [dbo].[tblEducation] Script Date: 09/22/2013 17:31:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tblEducation](
[EDUCATIONID] [varchar](50) NOT NULL,
[STUDENTNUMBER] [varchar](50) NULL,
[INSTITUTIONNAME] [varchar](50) NULL,
[COURSENAME] [varchar](50) NULL,
[GRADE] [varchar](50) NULL,
[YEAROFLEAVING] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

以下是数据截图:

enter image description here

我希望能够找到每一个曾就读于名为中学的机构并且拥有类(class)名称类似的教育记录的人生物。不仅仅限于生物学,我想找到所有的科学,所以我需要放置多个类似的陈述。

我已经尝试过:

SELECT COUNT(*) AS 'Our Students', 
DTOurStudents.STUDENTNAME
FROM (SELECT TOP 2 TBLSTUDENT.STUDENTNUMBER,
TBLSTUDENT.STUDENTNAME,
TBLEDUCATION.INSTITUTIONNAME,
TBLEDUCATION.COURSENAME
FROM TBLEDUCATION
INNER JOIN TBLSTUDENT
ON TBLEDUCATION.STUDENTNUMBER = TBLSTUDENT.STUDENTNUMBER
WHERE TBLEDUCATION.INSTITUTIONNAME LIKE '%Secondary School%')
DTOurStudents
GROUP BY DTOurStudents.STUDENTNAME

SQL FIDDLE: http://sqlfiddle.com/#!3/666f8/2

最佳答案

这将通过将机构表与其本身连接起来,为您提供学生列表和大学类(class)计数(每个大学)。

SELECT
STUDENTNUMBER,
SCHOOL_NAME,
COLLEGE_NAME,
count(*) as COLLEGE_COURSES
FROM (
SELECT
school.STUDENTNUMBER,
school.INSTITUTIONNAME AS SCHOOL_NAME,
college.INSTITUTIONNAME AS COLLEGE_NAME
FROM dbo.tblEducation as school
INNER JOIN dbo.tblEducation as college ON school.STUDENTNUMBER = college.STUDENTNUMBER
WHERE school.INSTITUTIONNAME = 'Secondary School'
AND college.INSTITUTIONNAME <> 'Secondary School'
AND (college.COURSENAME like 'biol%'
OR college.COURSENAME like 'math%'
OR college.COURSENAME like 'etc%')
) AS c
GROUP BY STUDENTNUMBER, SCHOOL_NAME, COLLEGE_NAME

如果您想要大学类(class)名称,那么您可以在内部查询中返回该名称。但由于每个大学类(class)只有一条记录,因此外部 selectgroup by 将是多余的。

SELECT  
school.STUDENTNUMBER,
school.INSTITUTIONNAME AS SCHOOL_NAME,
college.INSTITUTIONNAME AS COLLEGE_NAME,
college.COURSENAME
FROM dbo.tblEducation as school
INNER JOIN dbo.tblEducation as college ON school.STUDENTNUMBER = college.STUDENTNUMBER
WHERE school.INSTITUTIONNAME = 'Secondary School'
AND college.INSTITUTIONNAME <> 'Secondary School'
AND (college.COURSENAME like 'biol%'
OR college.COURSENAME like 'math%'
OR college.COURSENAME like 'etc%'

关于sql - 使用 SQL 根据记录计数显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com