我需要使用(group by
)选项选择表值。 Table1
包含我需要汇总的周数据列表,并根据Desc 2
表1:
╔════════╦═══════╦════════╦═══════╗
║ ID ║ Desc ║ Desc 2 ║ Amt ║
╠════════╬═══════╬════════╬═══════╣
║ 1 ║ Total ║ Spent ║ 25.00 ║
║ 2 ║ Total ║ Net ║ 55.00 ║
║ 3 ║ Total ║ Spent ║ 78.00 ║
║ 4 ║ Total ║ avg ║ 99.00 ║
║ 5 ║ Total ║ Net ║ 54.00 ║
║ 6 ║ Total ║ vv ║ 58.00 ║
║ 7 ║ Total ║ vv ║ 55.00 ║
║ 8 ║ Total ║ avg ║ 55.00 ║
╚════════╩═══════╩════════╩═══════╝
我需要Table1
结果表:
╔════════╦═══════╦════════╦═══════╗
║ ID ║ Desc ║ Desc 2 ║ Amt ║
╠════════╬═══════╬════════╬═══════╣
║ 1 ║ Total ║ Null ║479.00 ║
║ 2 ║ Total ║ Net ║109.00 ║
║ 3 ║ Total ║ Spent ║103.00 ║
║ 4 ║ Total ║ avg ║154.00 ║
║ 5 ║ Total ║ vv ║113.00 ║
╚════════╩═══════╩════════╩═══════╝
答案 0 :(得分:3)
如果您想要额外的行总金额,可以使用GROUPING SETS
DECLARE @SampleData AS TABLE
(
ID int IDENTITY (1,1),
[Desc] varchar(10),
Desc2 varchar(10),
Amt int
)
INSERT INTO @SampleData
(
[Desc],
Desc2,
Amt
)
VALUES
('Total','Spent',25),
('Total','Net',55),
('Total','Spent',78),
('Total','avg',99),
('Total','Net',54),
('Total','vv',58),
('Total','vv',55),
('Total','avg',55)
SELECT row_number() over(ORDER BY [desc],[desc2]) AS Id,
[Desc], Desc2, sum(Amt) AS SumAmt
FROM @SampleData sd
GROUP BY GROUPING SETS (([Desc], Desc2), ([Desc]))
在此处查看我的演示http://rextester.com/OYTFG21446
答案 1 :(得分:0)
您可以尝试此查询
select desc , desc2 ,sum(amt) as T_amt from table1 group by desc ,dec2
答案 2 :(得分:0)
试试这个:
<script type="text/javascript">
$(document).ready(function () {
/* Get the checkboxes values based on the class attached to each check box */
$("#buttonClass").click(function() {
getValueUsingClass();
ajaxPost();
});
});
function getValueUsingClass(){
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function() {
var checker=chkArray.push($(this).val());
});
var result=[];
for(var i=0;i<length.chkArray;i++)
{
result[i]=chkArray[i];
}
/* we join the array separated by the comma */
var selected;
selected = chkArray.join(',') ;
var exame_name=$("#exame_name").val();
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if(selected.length > 1){
alert("You have selected " + chkArray);
alert("exame_name "+exame_name);
}else{
alert("Please at least one of the checkbox");
}
}
$.ajax({
type: "POST",
data:JSON.stringify( {check:chkArray,exame_name:exame_name}),
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
</script>
答案 3 :(得分:0)
尝试以下SQL语句:
SELECT ID, Desc, Desc2, SUM(Amt) as amount
FROM table1
GROUP BY Desc2
注意: 如果“Desc”是您的真实字段名称,请将其更改为其他名称,因为SQL解析器会将其与ORDER BY子句中的“DESC”混淆。