【问题标题】:SQL Stored procedure to obtain top customersSQL存储过程获取顶级客户
【发布时间】:2013-06-25 22:30:32
【问题描述】:

我正在尝试创建一个存储过程,该过程通过“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)

【问题讨论】:

    标签: sql sql-server stored-procedures


    【解决方案1】:

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

    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_customer。我正在考虑显示“有用的”最终用户数据,但错过了 ID。
    • Martin 的答案可能更好/更有效,但在许多情况下,简单性具有其价值。这对于不太高级的用户来说更明显,因此会更容易维护和修改。
    • 按总和排序以减少畏缩。
    【解决方案2】:

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

    如果您只需要 2 个并且不想包含与相同支出相关的客户,请删除 WITH TIES 选项。

    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 
    

    【讨论】:

      【解决方案3】:

      我不是很喜欢 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
      

      【讨论】:

      • 至少在“最高 2”部分做出一些努力...?
      • @ErikE 正如我所说,我对 SQL Server 方言一无所知,这就是我没有冒险这样做的原因。
      • 我所知道的是,当我回答我不太熟悉的 DBMS 的问题时,我总是尝试学习一些东西。我会尝试给出正确/完整的答案,或者我根本不屑于回答。
      • 艾瑞克,冷静点。这里的重点是试图或多或少准确地帮助人们,重要的是意图。想象一下你迷路并问路:如果有人说“我不知道怎么去那里,但如果你从这里往北走,你最终会找到它”——这是一个有用的信息,尽管不准确或 GPS 水平不高.感谢您的参与
      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 2023-03-30
      • 1970-01-01
      • 2020-03-12
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      相关资源
      最近更新 更多