SQL - 我需要将总值分成另一个表中的多个行

时间:2012-07-21 05:44:39

标签: sql sql-server sql-server-2008 tsql sqlite

假设我在SQL Server 2008中有以下表格

学校表

School_Id |Course_Id | Total Students | 
--------------------------------------- 
   1         Acct101        150
   1         Acct102        100
   2         Acct101        110
   2         Acct102        130

班级表

School_Id |Course_Id | Class_ID | Capacity
--------------------------------------- 
   1         Acct101     A1       65
   1         Acct101     A2       50
   1         Acct101     A3       70
   1         Acct102     Ab1      100
   1         Acct102     Ab2      100
   2         Acct101     B1       80
   2         Acct101     B2       90

根据给定的信息,我需要按照以下方式显示每个班级的学生总数:

    School_Id |Course_Id | Class_ID | Capacity | Students 
    ------------------------------------------------- 
       1         Acct101      A1        65        60
       1         Acct101      A2        45        40
       1         Acct101      A3        70        50
       1         Acct102      Ab1       100       70
       1         Acct102      Ab2       100       30
       2         Acct101      B1        80        60
       2         Acct101      B2        90        50

我根据学校表第一行中的学生总数值得到前3行的学生价值,这是150

因此,我将150分为3个班级,并在划分时考虑到班级能力。 (我只需要对数字进行任何汇总,使我的总数达到班级限制。此外,所有教室都必须有学生,所以班级中的零学生是不可接受的)

我怎样才能实现这个目标?

请注意,我有一个包含+500,000行的表和另一个包含总计汇总值的表,因此我需要根据总计摘要插入第一个表中的值。

3 个答案:

答案 0 :(得分:0)

如果你继续提供全部容量,那么你可能会错过一个或多个课程。但是在这种情况下你可以做总计直到达到说150为例你的例子。但是对于第二课你有足够的容量为100和总数学生是100,所以只有1个学生将被填写,2个学生将有0个学生。这是更容易的方法。

其他方法是你得到总学生的比例,即150和总capcty,即180,并使用这个比率乘以每一行,以获得每个班级的学生但这里的问题是,对于某些课程,你可能会得到分数值是不可能的,因此你必须使用圆形或上限但这些将导致1或2个学生缺失,这取决于每行的总分数。这很棘手,如此多的行可能会更加棘手。

编辑:在这里添加第二种方法.. 试试这个..

select cs.*,round(cs.capacity*rt.ratio,0) from
(
select cp.*,sc.totalstudents/cp.cap as ratio
from (select schoolid,courseid,sum(capacity) cap from class ) as cp
inner join 
school sc on cp.schoolid = sc.schoolid and sc.courseid = cp.courseid
) rt
inner join class cs 
on cs.schoolid = rt.schoolid and rt.courseid = cs.courseid

这应该大概适用,但可能会错过一些东西或有额外的学生。这应该是非常有效的,基于索引。

您也可以使用NTILE,但这可能会妨碍性能,因为每行最多可能会使用nesteed循环连接与数字表连接。

答案 1 :(得分:0)

你可以这样做,这个查询首先填充最大容量的教室:

DECLARE @School TABLE (School_Id INT,Course_Id 
    VARCHAR(50), Total_Students INT)
DECLARE @Class TABLE (School_Id INT,Course_Id 
    VARCHAR(50), Class_ID VARCHAR(50), Capacity INT)
INSERT @School VALUES 
   (1,         'Acct101'        ,150),
   (1,         'Acct102'        ,100),
   (2,         'Acct101'        ,110),
   (2,         'Acct102'        ,130)
INSERT @Class VALUES 
   (1,         'Acct101'     ,'A1'       ,65),
   (1,         'Acct101'     ,'A2'       ,50),
   (1,         'Acct101'     ,'A3'       ,70),
   (1,         'Acct102'     ,'Ab1'      ,100),
   (1,         'Acct102'     ,'Ab2'      ,100),
   (2,         'Acct101'     ,'B1'       ,80),
   (2,         'Acct101'     ,'B2'       ,90)

;WITH y AS (
SELECT  a.*,
        ROW_NUMBER() OVER 
            (PARTITION BY a.School_ID, a.Course_ID ORDER BY a.Capacity DESC) 
            CapacitiyOrderPerSchoolAndCourse,
        SUM(a.Capacity) OVER 
            (PARTITION BY a.School_ID, a.Course_ID) 
            TotalCapacityForSchoolAndCourse,
        b.Total_Students TotalParticipants
FROM    @Class a
JOIN    @School b ON 
        b.School_Id = a.School_Id
        AND b.Course_Id = a.Course_Id
), z AS(
SELECT  x.School_Id, 
        x.Course_Id, 
        y.TotalCapacityForSchoolAndCourse, 
        y.TotalParticipants,
        CASE WHEN y.TotalParticipants < SUM(x.Capacity) THEN 
                y.TotalParticipants
            ELSE 
                SUM(x.Capacity) 
            END NumberOfStudentsInClasses,
        MIN(y.Capacity) ClassCapacity,
        y.Class_ID ClassName,
        MIN(y.Capacity) - 
        CASE WHEN y.TotalParticipants - SUM(x.Capacity) < 0 THEN 
               ABS(y.TotalParticipants - SUM(x.Capacity))
            ELSE
               0
            END StudentsInClass
FROM    y
JOIN    y x ON x.School_Id = y.School_Id 
        AND x.Course_Id = y.Course_Id 
        AND x.CapacitiyOrderPerSchoolAndCourse 
                <= y.CapacitiyOrderPerSchoolAndCourse
GROUP   BY x.School_Id, 
        x.Course_Id, 
        y.CapacitiyOrderPerSchoolAndCourse, 
        y.Class_ID, 
        y.TotalCapacityForSchoolAndCourse, 
        y.TotalParticipants
)

SELECT  
        z.School_Id, 
        z.Course_Id, 
        z.TotalCapacityForSchoolAndCourse, 
        z.TotalParticipants,
        z.ClassName,
        z.ClassCapacity,
        CASE WHEN StudentsInClass < 0 THEN 
                0 
            ELSE 
                StudentsInClass 
            END StudentsInClass
FROM    z

如果您希望每个教室都有一定数量的学生,您可以这样做(根据学校的容量为每个教室分配一些学生):

;WITH y AS (
SELECT  a.*,
        SUM(a.Capacity) OVER 
            (PARTITION BY a.School_ID, a.Course_ID) 
            AS TotalCapacityForSchoolAndCourse,
        b.Total_Students TotalParticipants
FROM    @Class a
JOIN    @School b ON 
        b.School_Id = a.School_Id
        AND b.Course_Id = a.Course_Id
), z AS(
SELECT  y.School_Id, 
        y.Course_Id, 
        y.TotalCapacityForSchoolAndCourse, 
        y.TotalParticipants,
        MIN(y.Capacity) ClassCapacity,
        y.Class_ID,
        MIN(y.Capacity) * 1.0 / y.TotalCapacityForSchoolAndCourse 
            AS PercentOfCapacity,
        ROUND(
            MIN(y.Capacity) * 1.0 / y.TotalCapacityForSchoolAndCourse 
                * TotalParticipants
            , 0, 0) 
            AS NumberOfStudents
FROM    y
GROUP   BY y.School_Id, 
        y.Course_Id, 
        y.Class_ID, 
        y.TotalCapacityForSchoolAndCourse, 
        y.TotalParticipants
)
, i AS(
SELECT  
        z.School_Id, 
        z.Course_Id, 
        z.TotalCapacityForSchoolAndCourse, 
        z.TotalParticipants,
        z.Class_ID,
        z.ClassCapacity,
        PercentOfCapacity,
        NumberOfStudents,
        SUM(NumberOfStudents) OVER 
            (PARTITION BY z.School_Id, z.Course_Id) 
            AS SumNumberOfStudents,
        ROW_NUMBER() OVER 
            (PARTITION BY z.School_Id, z.Course_Id 
                ORDER BY NumberOfStudents) 
            AS ClassWithSmallestCapacity
FROM    z
), j AS(
SELECT  i.School_Id, 
        i.Course_Id, 
        i.TotalCapacityForSchoolAndCourse, 
        i.TotalParticipants,
        i.Class_ID,
        i.ClassCapacity,
        i.PercentOfCapacity,
        i.NumberOfStudents,
        i.NumberOfStudents +
        CASE WHEN ClassWithSmallestCapacity = 1 THEN 
                TotalParticipants - SumNumberOfStudents 
            ELSE 0 
            END AS NumberOfStudents2
FROM    i
)

SELECT  *
FROM    j

答案 2 :(得分:0)

试试这个。你必须使用你自己的表和逻辑基于this.Ntile与光标将返回完美的数据,也应该很快安静。你需要一个数字表也适用于这种方法。

set nocount on
go
drop table soh
go
select salesorderid,round(totaldue -10,0)%180 totaldue into soh
from Sales.SalesOrderHeader
go
create unique clustered index idx_soh_id on soh(salesorderid) 
go
drop table sod
go
select salesorderid,salesorderdetailid,round(linetotal,0)%200 as linetotal  into sod
from sales.SalesOrderDetail
go
create unique clustered index idx_sod_id on sod(salesorderid,salesorderdetailid) 
go
drop table #sod
go
declare @salesorderid int,@totaldue float,@cnt int,@sm float
declare cr_cursor cursor fast_forward 
for select salesorderid ,totaldue from soh
select salesorderid,salesorderdetailid, linetotal,0 as val,ROW_NUMBER() over(partition by salesorderid order by linetotal asc) as rn 
into #sod from sod
create unique clustered index idx_#sod on #sod(salesorderid,salesorderdetailid)
open cr_cursor
while(1=1)
begin
    fetch cr_cursor into @salesorderid,@totaldue
    if (@@FETCH_STATUS <> 0)
    begin
        break
    end
    select @sm =sUM(linetotal) ,@cnt = COUNT(salesorderdetailid) 
    from #sod where salesorderid = @salesorderid
    ;with cte as 
    (select @salesorderid salesorderid,nt,count(*) cnt from 
    (select NTILE(@cnt) over( order by n.cnt asc) nt from nums n where n.cnt <= @sm - @totaldue ) dta
    group by nt
    )
    update s 
    set val = c.cnt
    from #sod s inner join cte c on s.SalesOrderID= c.salesorderid and s.rn= c.nt
end
close cr_cursor
deallocate cr_cursor

select * from #sod
go