获取顶级客户的SQL存储过程

获取顶级客户的SQL存储过程,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我试图创建一个存储过程,该过程遍历一个“SALES”表并返回药店中最好的两个客户(花了更多钱的两个客户) 下面是一些代码: 表格创建: create table Customer ( Id_customer int identity(1,1) Primary Key, Name varchar(30), Address varchar(30), DOB datetime, ID_number int not null check (ID_number &g

我试图创建一个存储过程,该过程遍历一个“SALES”表并返回药店中最好的两个客户(花了更多钱的两个客户)

下面是一些代码:

表格创建:

create table Customer (
    Id_customer int identity(1,1) Primary Key,
    Name varchar(30),
    Address varchar(30),
    DOB datetime,
    ID_number int not null check (ID_number > 0),
    Contributor int not null check (Contributor > 0),
    Customer_number int not null check (Customer_number > 0)
    )

create table Sale (
    Id_sale int identity(1,1) Primary Key,
    Id_customer int not null references Customer(Id_customer),
    Sale_date datetime,
    total_without_tax money,
    total_with_tax money
    )
嗯,我不知道这是否有用,但我有一个函数,只要我提供客户ID,它就会返回客户花费的总金额

这是:

CREATE FUNCTION [dbo].[fGetTotalSpent]
(
    @Id_customer int
)
RETURNS money
AS
BEGIN
    declare @total money
    set @total = (select sum(total_with_tax) as 'Total Spent' from Sale where Id_customer=@Id_customer)
    return @total
END
有人能帮我找到两个顶级客户吗

谢谢 恰帕

PS:这里有一些数据要插入,以便您可以更好地测试:

insert into customer values ('Jack', 'Big street', '1975.02.01', 123456789, 123456789, 2234567891)
insert into customer values ('Jim', 'Little street', '1985.02.01', 223456789, 223456789, 2234567891)
insert into customer values ('John', 'Large street', '1977.02.01', 323456789, 323456789, 3234567891)
insert into customer values ('Jenny', 'Huge street', '1979.02.01', 423456789, 423456789, 4234567891)

insert into sale values (1, '2013.04.30', null, 20)
insert into sale values (2, '2013.05.22', null, 10)
insert into sale values (3, '2013.03.29', null, 30)
insert into sale values (1, '2013.05.19', null, 34)
insert into sale values (1, '2013.06.04', null, 21)
insert into sale values (2, '2013.06.01', null, 10)
insert into sale values (2, '2013.05.08', null, 26)

这将与顶级客户一起加入CTE

如果您只想要2个,并且不想包括花费相同的客户,请删除带领带的
选项

WITH Top2
     AS (SELECT TOP 2 WITH TIES Id_customer,
                                SUM(total_with_tax) AS total_with_tax
         FROM   Sale
         GROUP  BY Id_customer
         ORDER  BY SUM(total_with_tax) DESC)
SELECT *
FROM   Customer C
       JOIN Top2 T
         ON C.Id_customer = T.Id_customer 

我对SQL Server方言不太感兴趣,但这本书将为您提供最好的客户以及他们所花的钱:

select Id_customer, total_with_tax from
(select Id_customer, sum(total_with_tax) total_with_tax from Sale group by Id_customer)
order by total_with_tax desc

您可以通过单个查询完成此操作,而无需任何特殊功能:

select top 2 c.id_customer, c.name, sum(s.total_with_tax)
from customer c
join sale s on c.id_customer = s.id_customer
group by c.id_customer, c.name
order by sum(s.total_with_tax) desc

如果多个客户共享同一个名称,这将产生扭曲的结果,因为这将合并他们的销售。很好。我添加了c.id\u客户。我正在考虑显示“有用的”最终用户数据,但没有看到id。Martin的答案可能更好/更有效,但在许多情况下,简单性有其价值。这对于不太高级的用户来说更为明显,因此今后更容易维护和修改。按总和排序以减少畏缩。至少在“最高2”部分做一些努力@ErikE如我所说,我对SQL Server方言一无所知,这就是为什么我没有冒这个险。我所知道的是,当我回答我不太熟悉的DBMS问题时,我总是尝试学习一些东西。我试着给出正确/完整的答案,否则我根本不想回答。埃里克,冷静。这里的重点是试图帮助人们,或多或少准确地说,重要的是意图。想象一下你迷路了,问路:如果有人说“我不知道怎么去那里,但如果你从这里向北走,你最终会找到它”——这是一个有用的信息,尽管不准确,也不符合GPS水平。感谢您的参与