Pisahkan pada satu kolom dan kembalikan baris TOP

9

Bagaimana Anda meminta tiga pelanggan unik dengan yang terbesar Purchase_Cost?

Saya ingin menerapkan DISTINCThanya aktif Customer_Name, tetapi kueri di bawah ini berlaku berbeda pada ketiga kolom. Bagaimana saya harus memodifikasi kueri untuk mendapatkan hasil yang diinginkan?

SELECT DISTINCT TOP 3 customer_name, order_no, Purchase_Cost  
FROM PurchaseTable 
ORDER BY Purchase_Cost

masukkan deskripsi gambar di sini

Neal
sumber

Jawaban:

9

Ganti dbname dan schemaName Anda dalam kueri berikut.

;WITH CTE AS 
(
SELECT  
       [Order_No]
      ,[Customer_Name]
      ,[Purchase_Cost]
     , ROW_NUMBER() OVER(PARTITION BY [customer Name] ORDER BY [Purchase Cost] DESC) AS "RowNumber"
  FROM [dbname].[schemaName].[PurchaseTable]
  )

  SELECT TOP(3)
       [Order_No]
      ,[Customer_Name]
      ,[Purchase_Cost]
  FROM CTE WHERE RowNumber=1
  ORDER BY [Purchase_Cost] DESC

Saya yakin ada cara lain untuk melakukan hal yang sama. Saya sarankan Anda membaca ini .

SqlWorldWide
sumber
1

silakan coba:

SELECT DISTINCT TOP 3  order_no, customer_name,  Purchase_Cost
FROM
(   SELECT order_no, customer_name, Purchase_Cost, ROW_NUMBER() OVER(PARTITION BY customer_name ORDER BY Purchase_Cost DESC) Orders
    FROM PurchaseTable
) A
WHERE A.Orders = 1
ORDER BY Purchase_Cost DESC
Fatih
sumber
0

Saya pikir ini bisa lebih efisien daripada solusi lain, asalkan Anda memiliki indeks pada: (Customer_Name, Purchase_Cost DESC) INCLUDE (Order_No)

;
WITH PurchaseTable AS
(
    SELECT * 
      FROM (VALUES ((501),('Carson'),(3400)),
                   ((502),('Thomas'),(625)),
                   ((503),('Daisy'),(4856)),
                   ((504),('Mary'),(2397)),
                   ((505),('Carson'),(5000))
           ) AS T(Order_No,Customer_Name,Purchase_Cost)
),
DistinctCustomers AS
(
    SELECT DISTINCT
           Customer_Name
      FROM PurchaseTable
)
SELECT TOP(3) 
       MaxCustomerOrder.Order_No,
       dc.Customer_Name,
       MaxCustomerOrder.Purchase_Cost
  FROM DistinctCustomers dc
 CROSS APPLY (SELECT TOP(1) * 
                FROM PurchaseTable pt 
               WHERE pt.Customer_Name = dc.Customer_Name 
               ORDER BY pt.Purchase_Cost DESC
             )    AS MaxCustomerOrder
 ORDER BY MaxCustomerOrder.Purchase_Cost DESC
;

Atau tanpa definisi tabel sebaris:

;
WITH DistinctCustomers AS
(
    SELECT DISTINCT
           Customer_Name
      FROM PurchaseTable
)
SELECT TOP(3) 
       MaxCustomerOrder.Order_No,
       dc.Customer_Name,
       MaxCustomerOrder.Purchase_Cost
  FROM DistinctCustomers dc
 CROSS APPLY (SELECT TOP(1) * 
                FROM PurchaseTable pt 
               WHERE pt.Customer_Name = dc.Customer_Name 
               ORDER BY pt.Purchase_Cost DESC
             )    AS MaxCustomerOrder
 ORDER BY MaxCustomerOrder.Purchase_Cost DESC
;
Jonathan Roberts
sumber
Kueri pertama tidak terlihat valid.
dezso
Saya mencoba memperbaikinya. Tidak yakin apa perbedaannya antara kueri 1 dan 2.
ypercubeᵀᴹ
Kueri 2 baru saja menghapus data tabel sebaris. Anda tidak membutuhkannya jika Anda benar-benar memiliki tabel dengan data di dalamnya. Jika Anda tidak memiliki tabel sebenarnya, seperti saya, Anda akan perlu untuk menguji apakah itu berjalan.
Jonathan Roberts