简体   繁体   English

从单个行上的查询返回所有结果

[英]Return all results from a Query on a single row

I have a query that will return all the results for matching row values between two tables.我有一个查询将返回两个表之间匹配行值的所有结果。

The tables are Students and StudentRace .这些表是StudentsStudentRace The match in the WHERE statement is STUDENTS.ID = STUDENTRACE.STUDENTID . WHERE语句中的匹配项是STUDENTS.ID = STUDENTRACE.STUDENTID

I am trying to return the column STUDENTRACE.RACECD where all the matching RACECDs are in a single row in the resulting table.我正在尝试返回STUDENTRACE.RACECD列,其中所有匹配的 RACECD 都在结果表的一行中。 The query does something different, however.然而,查询做了一些不同的事情。 IF a STUDENTID has more than 1 RACECD it does not repeat each RACECD in a single row for the STUDENTID.如果一个 STUDENTID 有超过 1 个 RACECD,它不会在一行中为 STUDENTID 重复每个 RACECD。 It will return a separate row for each RACECD that matches the STUDENTID.Here is my query:它将为每个匹配 STUDENTID 的 RACECD 返回一个单独的行。这是我的查询:

select 
    STUDENTS.ID as ID,
    STUDENTS.STUDENT_NUMBER as STUDENT_NUMBER,
    STUDENTRACE.STUDENTID as STUDENTID,
    STUDENTS.FIRST_NAME as FIRST_NAME,
    STUDENTS.LAST_NAME as LAST_NAME,
    STUDENTRACE.RACECD as RACECD,
    STUDENTS.ENROLL_STATUS as ENROLL_STATUS
from 
    STUDENTRACE STUDENTRACE,
    STUDENTS STUDENTS
where 
    STUDENTS.ID = STUDENTRACE.STUDENTID
    and ENROLL_STATUS = 0

Here is the result for a STUDENT ID = 23:这是 STUDENT ID = 23 的结果:

StudentID Racecd
23        B
23        W

This is close but not exactly what I would like to see.这很接近但不完全是我想看到的。 I would like the result to be:我希望结果是:

StudentID Racecd
23        B,W

or something similar to that.或类似的东西。 I think I may need the CONCAT function and possibly a nested SELECT statement as well, but I am not sure.我想我可能需要CONCAT function 和嵌套的SELECT语句,但我不确定。 I am new to SQL so I am not sure how to move forward.我是 SQL 的新手,所以我不确定如何前进。

Like jarlh said I am not sure how you select 7 columns but result in 2?就像 jarlh 说的,我不确定你是如何将 select 7 列结果变成 2 列的? but Listagg is what I think you are looking for.但 Listagg 是我认为您正在寻找的。 You can separate it by any delimiter (in this case I put comma).您可以使用任何分隔符将其分隔(在本例中我使用逗号)。 Also any outlying columns will need to appear in the group by此外,任何外围列都需要出现在组中

 select 
        STUDENTRACE.STUDENTID as STUDENTID,
        listagg(STUDENTRACE.RACECD,',') as RACECD
        from STUDENTRACE STUDENTRACE,
        STUDENTS STUDENTS
        where STUDENTS.ID=STUDENTRACE.STUDENTID
        AND ENROLL_STATUS = 0
    group by STUDENTRACE.STUDENTID

If you want there two records to become one, you want to group by something, aggregating on something else.如果你想让两条记录成为一条记录,你想按某些东西分组聚合其他东西。 In this case you want a single record for each student so you can group by the student_id , and you want all races aggregated so you can use GROUP_CONCAT (in MYSQL, but you can use corresponding aggregation function if in other RDBMS) to concatenate the races.在这种情况下,您希望每个学生都有一个记录,以便您可以按student_id分组,并且您希望聚合所有种族,以便您可以使用GROUP_CONCAT (在 MYSQL 中,但如果在其他 RDBMS 中,您可以使用相应的聚合 function)来连接种族. It would be like this:它会是这样的:

SELECT s.id, s.name, GROUP_CONCAT(sr.race)
FROM students s join studentrace sr on s.id = sr.student_id
GROUP BY s.id

That is the base to get what you want, then you can add the other fields you are interested in on the select and on the where filters.这是获得所需内容的基础,然后您可以在 select 和 where 过滤器上添加您感兴趣的其他字段。

SQL Fiddle: http://www.sqlfiddle.com/#!9/ad59d6/1/0 SQL 小提琴: http://www.sqlfiddle.com/#!9/ad59d6/1/0

Hope that helps.希望有所帮助。

If you are using Microsoft SQL Server, you might want to try the following code.如果您使用的是 Microsoft SQL 服务器,您可能想尝试以下代码。

create table Person (
  Name nvarchar(450)
)

insert into Person values ('Fabio'), ('Laura')

select stuff((select ',' + Name from Person for xml path('')), 1, 1, '')

The above select statement returns the following string.上述select语句返回以下字符串。

Fabio,Laura

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM