0

First timer here so please excuse me if my question is somewhat confusing. I am attempting to display things in a dynamic table using data pulled from a SQL table and I am having difficulty figuring out the logic.

The table structure looks something like this

NAME   Homeruns  Hits    Bunts   Total

Jeff       0       3       1       4

Sally      2       4       0       6

John       3       7       0       10

The data in the table is structured in a way that the type of play being made (Home run, hit, bunt, etc) is in a single column. I'll call this column PLAY. The name is in a separate column. This column will be called NAME. Table name is BASEBALL.

4

1 回答 1

3

这称为旋转查询。您可以在标准 SQL 中使用聚合执行此操作:

select name,
       sum(case when play = 'HomeRun' then 1 else 0 end) as HomeRun,
       sum(case when play = 'hit' then 1 else 0 end) as Hit,
       sum(case when play = 'bunt' then 1 else 0 end) as Bunt,
       count(*) as Total
from baseball bb
group by name;

这是一个非常简单的 SQL 查询,所以我猜您的专业知识在数据库方面不如在编程方面。我建议您花时间正确学习 SQL 语言。

于 2013-07-05T14:42:21.490 回答