每次SalesMan更改时显示总计的SQL查询

时间:2015-08-31 14:17:28

标签: sql-server-2008

这是我的表格结构,插入了垃圾数据。我想要做的是每个销售人员(弗兰克和乔)在他们下面总共有一排。这是我希望从查询返回时显示数据的方式

Should Display

这是填充了垃圾数据的表格结构,有人可以协助我如何设置它吗?

Create Table TurribleSetUp
(
  ID int,
  si varchar(200),
  salesmanname varchar(50),
  itemsold varchar(100)
)

Insert Into TurribleSetUp Values
(1, 'Home Frank Bad', 'Frank', 'stove'), 
(2, 'Internet Frank Left', 'Frank', 'table'),
(3, 'Store Frank Total Store Card', 'Frank', 'stereo')
,(4, 'Store Joe Bad', 'Joe', 'stove'), 
(5, 'Store Joe Right', 'Joe', 'stove'), 
(6, 'Joe, Person, High Five', 'Joe', 'car')
,(7, 'Frank, Person, High Five', 'Frank', 'car'),
(8, 'Left, Low Five, Joe', 'Joe', 'car')

2 个答案:

答案 0 :(得分:2)

尝试;

select info, total_sale, itemsold piece_of_equipment_sold
from (
    select si info, count(itemsold) total_sale, itemsold, salesmanname 
    from TurribleSetUp 
    group by si, itemsold, salesmanname

    union all

    select 'Total for ' + salesmanname, count(itemsold), '' , salesmanname
    from TurribleSetUp 
    group by salesmanname
) x
order by salesmanname, total_sale

答案 1 :(得分:1)

我做这样的事情:

    select ts.salesmanname, si, count(itemsold) as Cnt, itemsold 
    from TurribleSetUp ts
    group by si, itemsold, ts.salesmanname
    union
    select salesmanname, 'Totals For '+ Salesmanname as si, count(itemsold) as Cnt, NULL as itemsold
    from TurribleSetUp 
    group by salesmanname
    order by ts.salesmanname

我按推销员名称订购了它。我假设您有某种类型的号码,而不是您订购的号码。