使用计算和条件获取程序值总和的SQL查询

时间:2015-06-04 13:32:50

标签: sql sql-server database

我问了一个类似于此前的问题,但我认为我没有提供足够的信息来解决我想要做的事情。 我有以下表格。

Program Table - 
        prog_ID, 
        prog_Name

Unit Table - 
        unit_ID, 
        unit_name, 
        prog_ID
        max_beds
        max_budgeted-beds
        max_unbudgeted-beds
        max_escalation-beds

Import_Data Table - 
        bed_ID, 
        unit_ID
        ...other unrelated data


Bed Table - 
        bed_ID, 
        bed_type_ID

Bed Type Table - 
        bed_type_ID, 
        bed_type

我正在尝试将以下内容确定为输出中的列标题:

被占领的床

--Total Occupied Beds (SUM of Beds of type 'Regular' in Import Table)
(SUM(CASE WHEN Bed_Type.bed_type_description = 'Regular' THEN 1 ELSE 0 END)) As 'Total Occupied' 

预算床

--OCCUPIED BUDGETED BEDS (IF # of 'Regular' beds for the Program is < max_budgeted_beds than this is the Sum of the 'Regular' beds for the program, If its > than the Budgeted than this value = max_budgeted_beds for the program)

(SUM WHEN < MAX_BUDGETED),(CASE WHEN (SUM (CASE WHEN Bed_Type.bed_type_description =     'Regular' THEN 1 ELSE 0 END)) < UNIT.unit_beds_max_budgeted THEN (SUM (CASE WHEN Bed_Type.bed_type_description = 'Regular' THEN 1 ELSE 0 END)) ELSE Unit.unit_beds_max_budgeted END) AS 'Occupied Budgeted Beds'

未列入预算的床

--OCCUPIED UNBUDGETED BEDS (IF # of 'Regular' beds for the Program is < max_budgeted_beds than this is 0, If its > than the Budgeted than this value = max_budgeted_beds + max_unbudgeted_beds - # of 'Regular' beds for the program)
    , (CASE WHEN (SUM (CASE WHEN Bed_Type.bed_type_description = 'Regular' THEN 1 ELSE 0 END)) > UNIT.unit_beds_max_budgeted THEN Unit.unit_beds_max_unbudgeted + Unit.unit_beds_max_budgeted - (SUM (CASE WHEN Bed_Type.bed_type_description = 'Regular' THEN 1 ELSE 0 END)) ELSE 0 END) AS 'Occupied Unbudgeted Beds'

我在这里围绕对'Unit.unit_beds_max_budgeted'的引用感到错误,我不知道为什么。我在一个不同的查询中使用这个逻辑,但是输出给了我每个单元:

Program  Unit  Occupied_Beds   Budgeted_Beds   Unbudgeted_Beds
---------------------------------------------------------------
MED      A2    44              42              2
MED      A3    34              32              2
CCC      B2    44              42              2
CCC      B3    34              32              2

然而,我正在尝试的输出是将MED和CCC作为一行并卷起值(并排除单位名称)。

非常感谢任何帮助,用我的大脑震撼我的大脑,我觉得它应该很容易,但我无法得到它。我错过了必要的字段来建立链接吗?提前致谢。

1 个答案:

答案 0 :(得分:1)

您的错误消息告诉您需要汇总未列入预算的床栏。 CASE声明中有聚合是不够的。你需要一个外部聚合函数。

如果你像这样重写它,应该修正错误:

Hardware Needed:
<br />&nbsp;
<textarea name="hardware" cols="70" rows="5" maxlength="100" id="hardware"></textarea>
<div id="accordion">
     <h3>Account Distribution</h3>

    <div><font color="##000" , size="-8">Dept.
        <input type="text" name="hardwareDistDept" size="3" />&nbsp;Bus Unit
        <input type="text" name="hardwareDistBus" size="6" />&nbsp;Account
        <input type="text" name="hardwareDistAccount" size="6" />&nbsp;Op Unit
        <input type="text" name="hardwareDistUnit" size="6" />&nbsp;Project
        <input type="text" name="hardwareDistProject" size="6" />&nbsp;Work Id
        <input type="text" name="hardwareDistId" size="6" />&nbsp;Resource
        <input type="text" name="hardwareDistResource" size="6" />
        </font>

    </div>
</div>