首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有最大连接和多个连接的sql

具有最大连接和多个连接的sql
EN

Stack Overflow用户
提问于 2018-08-16 10:21:45
回答 2查看 53关注 0票数 0

我正在尝试得到每个客户在mysql上为每个产品支付的最后价格。下面的sql没有给我正确的数据。最大值(DateLasFullfillment)不是行值,甚至也不是最大值。这就像group by在最大值之前工作。

代码语言:javascript
复制
select 
    'item' AS type, soitem.productnum as 'SKU',
    (soitem.unitprice / right(uom.code, length(uom.code) - 2)) as unitPrice, 
    replace(customer.name, "#", "") AS priceList, 
    max(soitem.dateLastFulfillment) 
from 
    soitem
left join 
    so ON so.id = soitem.soid
left join
    customer on so.customerid = customer.id
left join 
    product on product.num = soitem.productnum
left join  
    uom on product.uomid = uom.id
where 
    soitem.dateLastFulfillment > now() - interval 6 month 
    and soitem.unitprice > 0 
    and so.statusid in (20, 25, 60)
group by 
    soitem.productnum, customer.name
order by 
    PriceList

以下是一些具有预期结果的表示例。sql必须以select语句、no等开头,除非没有其他选项。

SO表:

代码语言:javascript
复制
id  billToName  customerid  dateCompleted  dateCreated  dateIssued  num
1  Name1  1  6/27/18  6/23/18  6/23/18  ordernum1
2  Name1  1  7/15/18  7/10/18  7/10/18  ordernum2
3  Name1  1  7/29/18  7/20/18  7/20/18  ordernum3
4  Name2  2  6/31/2018  6/30/18  6/30/18  ordernum4
5  Name2  2  7/27/18  7/26/18  7/26/18  ordernum5
6  Name3  3  8/8/18  8/5/18  8/5/18  ordernum6
7  Name3  3  7/25/18  7/20/18  7/20/18  ordernum7

SOITEM表:

代码语言:javascript
复制
id  soId  unitPrice  dateLastFulfillment  productId  productNum  statusId  uomId  qtyOrdered
1  1  10  6/27/18  1  SKU-1  50  11  3
2  1  20  6/27/18  2  SKU-2  50  12  5
3  1  30  6/27/18  3  SKU-3  50  13  6
4  2  11  7/15/18  1  SKU-1  50  11  11
5  2  21  7/15/18  2  SKU-2  50  12  44
6  2  31  7/15/18  3  SKU-3  50  13  5
7  3  12  7/29/18  1  SKU-1  50  11  5
8  3  22  7/29/18  2  SKU-2  50  12  6
9  4  23  6/31/2018  2  SKU-2  50  12  9
10  4  33  6/31/2018  3  SKU-3  50  13  12
11  5  24  7/27/18  2  SKU-2  50  12  14
12  5  34  7/27/18  3  SKU-3  50  13  35
13  6  25  8/8/18  2  SKU-2  50  12  22
14  6  35  8/8/18  3  SKU-3  50  13  55
15  7  26  7/25/18  2  SKU-2  50  12  22
16  7  36  7/25/18  3  SKU-3  50  13  11

产品表:

代码语言:javascript
复制
num  uomid
SKU-1  11
SKU-2  12
SKU-3  13

计量单位表:

代码语言:javascript
复制
id  code
11  cs10
12  cs20
13  cs30

CUSTOMER表:

代码语言:javascript
复制
ID  NAME
1  CUSTOMER1#
2  CUSTOMER2#
3  CUSTOMER3#

预期结果:

代码语言:javascript
复制
type  SKU  unitPrice  priceList  max(soitem.dateLastFulfillment)
item  SKU-1  1.2  customer1  7/29/18
item  SKU-2  1.1  customer1  7/29/18
item  SKU-3  1.03  customer1  7/15/18
item  SKU-2  1.2  customer2  7/27/18
item  SKU-3  1.13  customer2  7/27/18
item  SKU-2  1.25  customer3  8/8/18
item  SKU-3  1.17  customer3  8/8/18
EN

回答 2

Stack Overflow用户

发布于 2018-08-16 13:18:43

试一试,仅仅是代码的第一部分,看看你会得到什么。然后粘贴到其他连接中,等等(删除前两个旧的where项和所有的group by)

代码语言:javascript
复制
-- compute this once, instead of for each row
Declare @now_minus_6mos as date = DATEADD(month, -6, GETDATE());
print @now_minus_6mos;

select 
    'item' AS type, soitem.productnum as 'SKU',
    (soitem.unitprice / 1 ) as unitPrice,   -- I do not have UOM, so simplify to be one
    -- I do not have Customer    replace(customer.name, "#", "") AS priceList, 
    (soitem.dateLastFulfillment) -- remove the max, since we are getting only the last one
from 
    (Select * From
        (Select
            productnum
            ,unitprice
            ,dateLastFulfillment
            ,Row_Number() Over(Partition By productnum  Order By dateLastFulfillment DESC) as dlfRow
         From soitem
         --move where filters here to reduce number of rows returned
         Where
             soitem.dateLastFulfillment > @now_minus_6mos 
         and soitem.unitprice > 0 
         ) aa
     Where dlfRow = 1
     ) soitem
票数 0
EN

Stack Overflow用户

发布于 2018-08-17 03:33:40

你可以用“排名”函数来做这件事。它们在MySQL中并不存在,但您可以模仿它们。

请看这篇文章。

Rank function in MySQL

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51868773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档