按多个字段分组

时间:2015-10-08 01:02:45

标签: sql sql-server sql-server-2008

我有一个ParentID表,它是通过组合所需数量的相应BaseID产品而制成的产品。

产品表:

    ParentID    BaseID    Required    UOH
    -------------------------------------
    1           55        1           400
    1           56        .5          400
    2           55        1           400
    2           57        1           400
    3           58        1           0

我需要选择ParentID,其中有足够的每个必需的基本产品(UOH)来创建父。

Query应该返回

    ParentID
    ----------------
    1
    2

我知道如何执行此操作的唯一方法是使用数据透视视图。有没有其他或更好的方法来实现这一目标?

由于

1 个答案:

答案 0 :(得分:4)

您可以使用//global variable used to generate the element id for each new message var message_index = 0; function get_schedule() { $.getJSON('get_json.php', function(json) { schedule_messages(json); }); } function schedule_messages(schedule) { var current_time = new Date(); //iterate through each message event for(var i = 0; i < schedule.length; i++) { var start_time = new Date(schedule[i].start_time); //make sure the scheduled message time has not passed if(start_time > current_time) { var duration = schedule[i].text_duration * 1000; //calculate time until message placement and removal var display_delay = start_time - current_time; var remove_delay = start_time - current_time + duration; //schedule placement of the message (function(delay, message, index) { setTimeout(function() { display_message(message, index); }, delay); })(display_delay, schedule[i].text_content, message_index); //schedule removal of the message (function(delay, index) { setTimeout(function() { remove_message(index); }, delay); })(remove_delay, message_index); //increment global message index to use for next message message_index++; } } } //do your fancy jQuery effect(s) in here... function display_message(message, index) { $('#user-text').append( $("<p>") .html(message) .attr("id", "message-" + index) ); } //... and here function remove_message(index) { $("#message-" + index).remove(); } group by

having

select parentid from table t group by parentid having sum(case when uoh < required then 1 else 0 end) = 0 子句计算having小于要求的次数。如果计数为零,那么所有基本ID都有足够的数量。