2

我有一张桌子叫Orders. 它有列id, orderid, qty, cost, location, currency... 等。

我必须将此订单表中的值插入给定的另一个表中order id,但问题出在currency.

对于每一个orderid,表中会有多行,order每一行都可以有差异currency

因此,在从该表中为 a 插入新行时orderid,我需要获取不同货币的计数并插入计数最高的货币。

如何比较列中不同值的计数?这是在 SQL Server 中。

4

5 回答 5

2
with cte as
(
select orderid,currency, row_number() over
    (partition by orderid,currency order by orderid,currency) rownumber
  from orders 
)  

select cte.orderid,cte.currency 
from cte
join
(select orderid,max(rownumber) rownumber2
  from cte group by orderid) A
on cte.orderid=a.orderid and cte.rownumber=a.rownumber2

SQLFIDDLE 演示:http ://sqlfiddle.com/#!3/1c046/25

感谢 bluefeet 提供样本数据

于 2013-01-16T12:20:34.193 回答
1

您的要求并不完全清楚,但似乎您可以使用这样的东西:

select o.id,
  o.orderid,
  o.currency,
  o.cost
from orders o
inner join
(
  select orderid, currency,
    CountCurrency,
    max(CountCurrency) over(partition by orderid) mx
  from
  (
    select orderid, currency, count(currency) CountCurrency
    from orders
    group by orderid, currency
  ) o
) m
  on o.orderid = m.orderid
  and o.currency = m.currency
where m.CountCurrency = m.mx
order by o.orderid, o.id

请参阅带有演示的 SQL Fiddle

这将返回包含出现最多的货币的所有行orderid。使用样本数据:

CREATE TABLE orders
    ([id] int, [orderid] int, [currency] varchar(3), [cost] int)
;

INSERT INTO orders
    ([id], [orderid], [currency], [cost])
VALUES
    (1, 10, 'USD', 1000),
    (2, 10, 'EUR', 522),
    (3, 10, 'USD', 999),
    (4, 10, 'INR', 999),
    (5, 20, 'TST', 557),
    (6, 25, 'GRB', 24),
    (7, 20, 'TST', 78),
    (8, 30, 'HYT', 3)
;

结果是:

| ID | ORDERID | CURRENCY | COST |
----------------------------------
|  1 |      10 |      USD | 1000 |
|  3 |      10 |      USD |  999 |
|  5 |      20 |      TST |  557 |
|  7 |      20 |      TST |   78 |
|  6 |      25 |      GRB |   24 |
|  8 |      30 |      HYT |    3 |
于 2013-01-16T02:53:16.627 回答
0

One more options

SELECT *
FROM Orders t
WHERE EXISTS(
             SELECT CASE WHEN MAX(COUNT(t2.currency)) OVER() = COUNT(t2.currency) THEN t2.currency END
             FROM Orders t2             
             WHERE t2.orderid = t.orderid            
             GROUP BY t2.currency
             INTERSECT
             SELECT t.currency
             )

Demo on SQLFiddle

于 2013-01-16T12:06:47.543 回答
0

据我了解,您是否要将行从 Orders 迁移到 otherTable,包括另外两列:maxCurrency 和 totalDistinctCurrency。

如果我的理解是正确的,那么您可以这样做:

INSERT INTO otherTable(
  maxCurrency, 
  totalDistinctCurrency, 
  otherColsFromOrders) 
SELECT 
  MAX(currency), 
  COUNT(DISTINCT currency), 
  * 
FROM Orders  
WHERE orderid = SOME_ORDER_ID_YOU_WANT;

注意两点:

  1. 您可以通过您想要的orderid更改 SOME_ORDER_ID_YOU_WANT ;
  2. 您可以将 otherColsFromOrders 更改为otherTable中与 Orders 表中列的顺序匹配的相应列的名称,因为选择时带有 *。

希望能帮助到你!

于 2013-01-16T00:02:23.023 回答
0

如果我正确理解了您的问题,那么您正试图在Orders给定特定 ? 的表中找到记录最多的货币OrderId

像这样的东西——如果用相同的计数表示多种货币,它可能会返回多条记录?

SELECT MAX(c.CurrencyCount), c.currency
FROM 
(
    SELECT currency, Count(currency) as CurrencyCount
    FROM Orders
    WHERE OrderId = 12345
    GROUP BY currency
) c
GROUP BY c.currency
于 2013-01-15T23:22:55.893 回答