0

您好,下面是我的示例表“tbltemptransaction”和“tblProduct”

tbltemptransaction table    

userid  pid pname   description qty
3   1   HP PROBOOK 4440S    Intel Core i5   5
3   1   HP PROBOOK 4440S    Intel Core i5   3
3   6   DocuPrint C1110B    16ppm black 2
3   2   iMac 21.5"         Intel Core i5    3
tblProduct  table

PID PName                   Qty Description UnitPrice   
1   HP PROBOOK 4440S    20  Intel Core i5   2300    1    
2   iMac 21.5"          20  Intel Core i5   3999    2    
3   NEC V260 Projector  10  2700 lumens 2899    3    
4   DES-1228P Web Smart     10  24-Port     899 5    
5   DES-1210 Series          5  8-Port Web      699 5    
6   DocuPrint C1110B    15  16ppm black 899 4   

我现在可以在其中运行我的 sql 的 userid=3 的表是这样的。

   pid  pname                    description   qty
    1   HP PROBOOK 4440S    Intel Core i5   5
    1   HP PROBOOK 4440S    Intel Core i5   3
    6   DocuPrint C1110B    16ppm black 2
    2   iMac 21.5"          Intel Core i5   3

我可以知道如何编码使其如下表所示吗?

   pid  pname                    description    qty
    1   HP PROBOOK 4440S    Intel Core i5   8
    6   DocuPrint C1110B    16ppm black 2
    2   iMac 21.5"          Intel Core i5   3

如果 pid 相同,则 qty 将相加。

谢谢。

4

3 回答 3

1

pid和相pname同时description(如您的示例中),group by则将这些结果汇总到一行。

select t.pid, t.pname, t.description, sum(t.qty) as qty
from tbltemptransaction t
group by t.pid, t.pname, t.description

编辑

select t.pid, sum(t.qty) as qty
from tbltemptransaction t
where pid = @userid
group by t.pid

@user id 是 session("userid") 的参数

最终编辑

select t.pid, p.pname, p.description, sum(t.qty) as qty
from tbltemptransaction t
inner join tblproducts p on p.pid = t.pid
where t.userid = 3
group by t.pid, p.pname, p.description
于 2013-07-10T06:40:33.477 回答
0
 select sum(qty) as qty, pid, pname, description from 
 tbltemptransaction group by pid,pname, description
于 2013-07-10T06:41:22.057 回答
0

试试这个看看

Select distinct t1.pid,t1.name,t1.description,sum(t2.total2) From
(select pid,name,description,count(qty) as total1 from tbltemptransaction) as t1
left join (select COUNT(qty) as total2 from tbltemptransaction) as t2
on t1.pid=t2.pid
group by t1.pid,t1.name,t1.description
于 2013-07-10T06:44:47.333 回答