SQL中的多行单行

时间:2017-12-17 00:30:36

标签: php mysql sql

大家好我有这张桌子,我希望按照客户ID显示单行组中的结果,并显示库存1和2的可用性。

这是我的表

id      client  stock   material    quantity    availability    date    
62      56      1       0           0           100             2017-12-16 23:55:01 
63      56      2       0           0           900             2017-12-16 23:55:01 
64      56      1       100         -20         80              2017-12-16 23:55:20 
65      56      1       80          100         180             2017-12-16 23:56:06 
66      56      1       180         200         380             2017-12-16 23:56:21 
67      56      1       380         500         880             2017-12-16 23:58:11 
68      56      1       880         -580        300             2017-12-16 23:58:38 
69      56      2       900         -90         810             2017-12-17 23:59:18 

我想要的结果是从上次日期获得结果,按客户ID分组并将库存1和库存2合并为单行

client  availability1   availability2
56      300             810

我尝试这个查询

SELECT
    historys.id
    ,(CASE WHEN historys.stock = 1 THEN availability END) AS availability1
    ,(CASE WHEN historys.stock = 2 THEN availability END) AS availability2
FROM historys
GROUP BY historys.client
ORDER by historys.id

结果是

id  availability1   availability2 
56  NULL            810

如果有人帮助我,我将不胜感激。感谢。

5 个答案:

答案 0 :(得分:4)

您需要在聚合之前过滤到正确的行。这是一种方法:

SELECT h.client,
       MAX(CASE WHEN h.stock = 1 THEN h.availability END) AS availability1
       MAX(CASE WHEN h.stock = 2 THEN h.availability END) AS availability2
FROM historys h
WHERE h.date = (SELECT MAX(h2.date) FROM historys h2 WHERE h2.stock = h.stock)
GROUP BY h.client

答案 1 :(得分:1)

使用联合

SELECT 
client, max(availability1) AS availability1, max(availability2) AS availability2
FROM 
(
SELECT
client
 ,availability AS availability1
 ,0 AS availability2
FROM historys hist
WHERE id = (select max(id) from historys where client = hist.client and stock = 1)
    UNION ALL
SELECT
client
 ,0 AS availability1
 ,availability AS availability2
FROM historys hist2
WHERE id = (select max(id) from historys where client = hist2.client and stock = 2)
) a
GROUP by client
ORDER by client

答案 2 :(得分:1)

您拥有的NULL是因为您从历史记录行中选择的数据不能同时包含stock = 1和stock = 2的可用性。

您可以像上面那样使用历史记录绕过它:

在子请求中(作为d),我们按客户端获取最大日期,然后我们连接历史记录两次以获得连续可用性。

Public Function RangeToArray(rng As Range) As Variant
    Dim i As Long, r As Range
    ReDim arr(1 To rng.Count)

    i = 1
    For Each r In rng
        arr(i) = r.Value
        i = i + 1
     Next r

    RangeToArray = arr
End Function

您可以在此处找到SQLFiddle:http://sqlfiddle.com/#!9/d1ea4/5

由于

答案 3 :(得分:1)

这可能会对您有所帮助:

 SELECT T.client client ,
    SUM(CASE WHEN T.Stock = 1 THEN T.availability
        END) availability1 ,
    SUM(CASE WHEN T.Stock = 2 THEN T.availability
        END) availability2
 FROM   historys T
    INNER JOIN ( SELECT MAX(date) Max_Date ,
                        stock
                 FROM   historys
                 GROUP BY stock
               ) T1 ON T1.Max_Date = T.date
                       AND T1.stock = T.stock
  GROUP BY T.client

答案 4 :(得分:0)

根据您的MySQL版本,您应该可以使用窗口功能&公用表表达式:

-- Build derived src table with "latest date" rows
WITH src AS (
    SELECT id, client, stock, material, quantity, availability, date
    FROM (
        SELECT 
            id, client, stock, material, quantity, availability, date,
            ROW_NUMBER() OVER(PARTITION BY client, stock ORDER BY date DESC) ClientStockRank -- For each (client, stock) pair, rank each row by date
        FROM historys a
    ) src
    WHERE ClientStockRank = 1 -- Only get rows with "latest date"
)
SELECT 
    client,
    MAX(CASE WHEN stock = 1 THEN availability ELSE NULL) END AS Availability1, -- Get "max" value for "stock 1"
    MAX(CASE WHEN stock = 2 THEN availability ELSE NULL) END AS Availability2 -- Get "max" value for "stock 2"
FROM src
GROUP BY client

我认为你需要MySQL 8+。如果没有,还应该有其他方法来做到这一点。如果有效,请告诉我。

<强>更新
实际上,这也应该足够了:

SELECT DISTINCT
    client,
    MAX(CASE WHEN stock = 1 THEN availability ELSE NULL) OVER(PARTITION BY client ORDER BY date DESC) AS Availability1,
    MAX(CASE WHEN stock = 2 THEN availability ELSE NULL) OVER(PARTITION BY client ORDER BY date DESC) AS availability2
FROM historys