SQL Server 2008 解析未知数量的条目

发布于 2024-11-09 14:46:52 字数 860 浏览 0 评论 0 原文

由于可能性的数量,我不知道每个人将有多少门课程,也不知道课程的名称是什么,所以我无法使用正常的子选择或案例。

我有一个像这样的表:

ID    NAME     CLASS
----------------------
1     BOB      Course1
1     BOB      Course2
1     BOB      Course3
2     JOHN     Course1
2     JOHN     Course2
3     SAM      Course1
4     BILL     Course2
4     BILL     Course3

我想让它看起来像这样,其中最后一列是针对拥有最多类的人:

ID    NAME     CLASS     CLASS2     CLASS3     CLASS4..........
------------------------------------------------------
1     BOB      Course1   Course2    Course3    NULL............
2     JOHN     Course1   Course2    NULL       NULL............
3     SAM      Course1   NULL       NULL       NULL............
4     BILL     Course2   Couse3     NULL       NULL............

我最好的想法是继续获取​​最大类并删除它,直到我用完记录。

编辑:澄清一下:我的列标题不是实际的课程,只是一种对教师分配的课程进行编号的方法。

I do not know how many courses each person will have or what the names of the courses are due to the number of possibilities so I can't use a normal sub-select or case.

I have a table like this:

ID    NAME     CLASS
----------------------
1     BOB      Course1
1     BOB      Course2
1     BOB      Course3
2     JOHN     Course1
2     JOHN     Course2
3     SAM      Course1
4     BILL     Course2
4     BILL     Course3

I want to make it look like this where the last column is for whoever has the most classes:

ID    NAME     CLASS     CLASS2     CLASS3     CLASS4..........
------------------------------------------------------
1     BOB      Course1   Course2    Course3    NULL............
2     JOHN     Course1   Course2    NULL       NULL............
3     SAM      Course1   NULL       NULL       NULL............
4     BILL     Course2   Couse3     NULL       NULL............

The best idea I had was to keep getting the max CLASS and deleting it until I ran out of records.

edit: To Clarify: My column headers will not be actual courses just a way to number how many courses a teacher is assigned to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

塔塔猫 2024-11-16 14:46:52

Abe 的建议如下所示:

 select * from Class
 pivot (COUNT(ID) for CLASS in ([Course1],[Course2],[Course3])) as Taking

给出的结果如下:

Name    Course1 Course2 Course3
BILL    0       1       1
BOB     1       1       0       
JOHN    1       1       0
SAM     1       0       0       

当然,这与您想要的略有不同......您希望课程 1 具有“第一道菜”的值。实现此目的的唯一方法是使用动态 SQL。

如果您想要一个以逗号分隔的课程列表(在一列中),那么生成起来会更容易、更快捷。


逗号列表

select c.Name, 
       STUFF((SELECT ( ', ' + c2.Class )
               FROM #Class c2
               WHERE c.Name = c2.Name
               ORDER BY c2.Class 
               FOR XML PATH( '' )),1,2,'') as [Class List]
FROM #Class c 
GROUP BY c.Name 

为您提供:

Name   Class List
BILL   Course2, Course3
BOB    Course1, Course2, Course3
JOHN   Course1, Course2
SAM    Course1

Abe's suggestion looks like this:

 select * from Class
 pivot (COUNT(ID) for CLASS in ([Course1],[Course2],[Course3])) as Taking

Which gives results like this:

Name    Course1 Course2 Course3
BILL    0       1       1
BOB     1       1       0       
JOHN    1       1       0
SAM     1       0       0       

Of course this is slightly different than what you want... you want course 1 to have a value for the "first course." The only way to do this is with dynamic SQL.

If you want a comma separated list of courses (in one column) that is much easier and faster to generate.


Comma list

select c.Name, 
       STUFF((SELECT ( ', ' + c2.Class )
               FROM #Class c2
               WHERE c.Name = c2.Name
               ORDER BY c2.Class 
               FOR XML PATH( '' )),1,2,'') as [Class List]
FROM #Class c 
GROUP BY c.Name 

Gives you:

Name   Class List
BILL   Course2, Course3
BOB    Course1, Course2, Course3
JOHN   Course1, Course2
SAM    Course1
可是我不能没有你 2024-11-16 14:46:52

您可以查看使用 PIVOT 的

详细信息:

http://msdn。 microsoft.com/en-us/library/ms177410.aspx

我相信您可以使用 ID 上的 MAX 作为聚合函数。

You can look at using a PIVOT

Read more about it here:

http://msdn.microsoft.com/en-us/library/ms177410.aspx

You can use MAX on ID as your aggregate function I believe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文