简体   繁体   English

用于从具有复杂结构的表中选择数据的SQL查询

[英]sql query for selecting data from a table with complex structure

I have a table with below structure. 我有一张下面结构的桌子。

I am not having control over changing the table. 我无法控制更换桌子。

It has three columns: student_name, student_id, name_id 它有三列:student_name,student_id,name_id

Now student name could be any number of words. 现在学生姓名可以是任意数量的单词。 Exactly one word will come in one row. 确切地说,一个单词将出现在一行中。 Based on the number of words, name_id will be entered and the student_id will be repeated. 根据单词的数量,将输入name_id并重复student_id。

The structure would be some thing like: 结构可能是这样的:

say name1 is: Ram Laxman Prasad Sharma 说name1是:Ram Laxman Prasad Sharma

and name2 is: Pandit Gangadhar Vidyadhar Mayadhar Omkarnath Shastri 和名字2是:Pandit Gangadhar Vidyadhar Mayadhar Omkarnath Shastri

So the table will look like: 所以表格看起来像:

student_name  |   student_id    |   name_id
-------------------------------------------------
 Ram                 1               1
 Laxman              1               2
 Prasad              1               3
 Sharma              1               4
 Pandit              2               1
 Gangadhar           2               2
 Vidyadhar           2               3
 Mayadhar            2               4
 Omkarnath           2               5
 Shastri             2               6

I hope I explained the structure clearly. 我希望我能清楚地解释这个结构。

Now, I want to write a query to read only first four names per student. 现在,我想编写一个查询,只读取每个学生的前四个名字。 However, if the number of names is less then four, empty string should go and if its greater then four, first four should go and rest just ignored. 但是,如果名称的数量小于4,则应该使用空字符串,如果其大于4,则前四个应该去休息而忽略。

I need to put it in a single select query only, since this query will be passed in a spring batch program. 我只需要将它放在一个select查询中,因为这个查询将在spring批处理程序中传递。 But I am not getting how to loop through nameid column to take first four name id for every student. 但我没有得到如何遍历nameid列以获取每个学生的前四个名称ID。

How to design this sql for DB2 database v8?? 如何为DB2数据库v8设计这个sql?

Thanks for reading. 谢谢阅读。

First off, I don't have DB2, so there may be some syntax changes 首先,我没有DB2,因此可能会有一些语法更改

Try the following 请尝试以下方法

select t1.student_id, ifnull(t2.names, ' ') from
(select distinct(student_id) as student_id 
 from tab ) as t1
left outer join
(
select tab1.student_id, ifnull(concat(tab1.student_name, ' ',
   tab2.student_name, ' ',
   tab3.student_name, ' ',
   tab4.student_name),'') as names
from (select * from tab where name_id = '1') tab1
inner join (select * from tab where name_id = '2') tab2
on tab1.student_id = tab2.student_id
inner join (select * from tab where name_id = '3') tab3
on tab1.student_id = tab3.student_id
inner join (select * from tab where name_id = '4') tab4
on tab1.student_id = tab4.student_id
) as t2
on t1.student_id = t2.student_id

I've worked on the assumption that your name_id are chars. 我已经假设你的name_id是chars。 Also keep in mind that I had written this query for MySQL and DB2 may have a different syntax 还要记住,我已经为MySQL编写了这个查询,DB2可能有不同的语法

improved version inspired by Amit - if you need all 4 names in 1 column :) 受Amit启发的改进版本 - 如果你需要1列中的所有4个名字:)

  select
    t1.student_name || 
    coalesce(' ' || t2.student_name, '') ||
    coalesce(' ' || t3.student_name, '') ||
    coalesce(' ' || t4.student_name, '') as "first 4 names"
  from mytable t1
  left join mytable t2 on t1.student_id = t2.student_id and t2.name_id = 2
  left join mytable t3 on t1.student_id = t3.student_id and t3.name_id = 3
  left join mytable t4 on t1.student_id = t4.student_id and t4.name_id = 4
  where t1.name_id = 1

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

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