在我的项目中,我有一个要求,即我的选择查询返回多列,但要求总和,我必须按一个列分组。详细信息如下。
示例:这只是一个示例,因为我的查询很大,有多个联接。
Table -tb1
---------------------------
CASE | Amount | Customer
---------------------------
CS001| 50 | 1
---------------------------
CS003| 100 | 2
---------------------------
CS001 | 2000 | 3
Table -tb2
---------------------------
Customer | Name| Age
---------------------------
1| John | 69
---------------------------
2| Robert| 23
---------------------------
3| Tammy| 23
---------------------------
现在,我想通过加入这2个表来获取所有记录,但总和为 Amount 。因此,我只想按 CASE
分组非常感谢您的帮助。
答案 0 :(得分:0)
这是您想要的吗?我曾与group by一起使用过。如果存在于table1或2中,则可以使用左连接或右连接来拥有更多记录
Select
a.[case] -- CASE is a reservedkeyword
,a.customer,sum(amount),
b.name,b.age from table1 a
join -- or left join/right join
table2 b
on
a.customer=b.customer
group by a.case
答案 1 :(得分:0)
这是您想要的吗?
select tb1.*, tb2.*, sum(amount) over (partition by case) as case_amount
from tb1 join
tb2
on tb1.customer = tb2.customer;
答案 2 :(得分:0)
进行分组时,出现在 function appendSomeItems(url, id, name, style) {
return '<div><div class="md-card md-card-hover"> <div class="gallery_grid_item md-card-content getImage"> <img class ="uk-align-center imageClick" src="https://media.testsite' + url + '" alt=""></a> <div class="gallery_grid_image_caption"> <span class="gallery_image_title uk-text-truncate">' + name + '</span> <span>' + style + '</span> </div></div></div></div>';
}
$(document).on('click', '.imageClick', function handleImage() {
console.log('good');
var img = $(this).closest(".getImage").children("img").clone(true);
$("#holdImage").html('');
$("#holdImage").append(img);
$('#image-modal').hide();
});
<!--image placeholder->
<div id="holdImage" style="margin-top: 15px;">
</div>
<!--open modal-->
<div style="text-align: center;">
<a href="#image-modal" data-uk-modal class="uk-button uk-button-primary">Choose an existing image</a>
</div>
<!--Modal-->
<div id="image-modal" class="uk-modal">
<div class="uk-modal-dialog" style="width:80%;">
<a class="uk-modal-close uk-close"></a>
{!! Form::open(array('id' => 'search_form')) !!}
<div class="uk-grid">
<div class="uk-width-1-1">
<div class="md-card">
<div class="md-card-content">
<div class="input-group">
<input type="text" class="md-input" placeholder="Search" name="srch-term" id="srch-term">
<p class="text-center">Search for images by Name </p>
</div>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
<br/>
<div id="imageResult" class="gallery_grid uk-grid-width-medium-1-5 uk-grid-width-large-1-6" data-uk-grid="{gutter: 16}">
</div>
<button style="display: none;" id="hidden_button"></button>
</div>
</div>
列表中的每一列都必须是SELECT
列表的一部分,或者必须对其应用聚合函数(例如COUNT,SUM,MAX等)。 / p>
GROUP BY
由于SELECT
t1."CASE", t2.Customer, t2.Name, t2.Age, SUM(t1.Amount) As TotalAmount
FROM
tbl1 t1
INNER JOIN tbl2 t2
ON t1.Customer = t2.Customer
GROUP BY
t1."CASE", t2.Customer, t2.Name, t2.Age
是SQL中的保留字,因此我以Oracle方式通过将其用双引号引起来对其进行了转义。