0

I have a problem with SQL query, and I will try to explain what I want to do. This is my query which returns some result rows.

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
min(scn.scndat) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
INNER JOIN sandeliai ON sandeliai.paid = sck.paid
WHERE sck.prid = 1376 GROUP BY sckid

And this query returns "Table 1" of results:

Table 1:

sckid    |    prid    |    sckcen    |    count_of_goods    |      date     |
123      |    1376    |    10009     |          0           |   2012-12-31  |
124      |    1376    |    10007     |          15          |   2013-01-25  |
125      |    1376    |    10005     |          0           |   2013-02-13  |
126      |    1376    |    10000     |          18          |   2013-03-15  |

But I need just one row with all data grouped by prid, and I wrote this query:

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
min(scn.scndat) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
INNER JOIN sandeliai ON sandeliai.paid = sck.paid
WHERE sck.prid = 1376 GROUP BY prid

Then I get one row with this data in table:

Table 2:

sckid    |    prid    |    sckcen    |    count_of_goods    |      date     |
123      |    1376    |    10009     |          23          |   2012-12-31  |

It's seems all correct, but in date field I need to return most earlier date from table one, which have count_of_goods > 0, so for me need this result:

Table 3:

sckid    |    prid    |    sckcen    |    count_of_goods    |      date     |
123      |    1376    |    10009     |          23          |   2013-01-25  |

So any ideas how can I get result like in Table 3?

4

3 回答 3

1

我认为我最初回来的答案可能过于复杂。假设 scd.scdkiek 是数字而不是 id,那么你可以通过简单地改变 INNER JOIN 标准来得到你想要的,即

SELECT 
    sck.sckid,
    sck.prid,
    sck.paid,
    sck.sckcen,
    scn.scndat,
    SUM(scd.scdkiek) AS count_of_goods
FROM sck 
INNER JOIN scd 
    ON scd.sckid = sck.sckid
    AND scd.scdkiek > 0
INNER JOIN sandeliai 
    ON sandeliai.paid = sck.paid
INNER JOIN scn 
    ON scn.scnid = scd.scnid
WHERE sck.prid = 1376 
GROUP BY 
    sck.prid

失败了,尽管我会考虑使用子查询。子查询将计算每行的 count_of_goods。然后,外部查询将使用它来决定是否将该行的日期设置为 NULL。MIN(...somedate...,NULL) 将返回日期。这反过来意味着您应该获得该行上的 count_of_goods 不为 NULL 的最短日期。

当您查找 count_of_goods > 0 的日期时,子查询按日期分组。

当然,这只是一种选择,可能还有其他更有效的方法,但我相信这会如你所愿

SELECT
    sck.sckid,
    sck.prid,
    sck.paid,
    sck.sckcen,
    SUM(d.count_of_goods) AS count_of_goods,
    MIN(IF(d.count_of_goods>0,d.scndat,NULL)) AS `date`
FROM (
    -- Get the data and group it by date
    SELECT 
        sck.sckid,
        sck.prid,
        sck.paid,
        sck.sckcen,
        scn.scndat,
        SUM(scd.scdkiek) AS count_of_goods
    FROM sck 
    INNER JOIN scd ON scd.sckid = sck.sckid
    INNER JOIN sandeliai ON sandeliai.paid = sck.paid
    INNER JOIN scn ON scn.scnid = scd.scnid
    WHERE sck.prid = 1376 
    GROUP BY 
        sck.prid, -- may not really be needed
        scn.scndat
) AS d
GROUP BY d.prid
于 2013-04-24T08:56:04.647 回答
1

尝试使用having,类似

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
min(scn.scndat) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
INNER JOIN sandeliai ON sandeliai.paid = sck.paid
WHERE sck.prid = 1376 GROUP BY prid HAVING count_of_goods > 0;

这应该适合你。这可能会跳过一些 count = 0 的行,我认为这应该没问题(只是我个人的看法)。

请将此作为起点,而不是作为最终解决方案。

编辑:使用别名更新 sum(...)

于 2013-04-24T07:11:41.947 回答
0

现在我的问题不是很合乎逻辑我尝试在我的问题中结合两个查询,然后我得到一个很好的答案,这是我的解决方案:

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
(SELECT 
scn.scndat
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
WHERE sck.prid = 1376 GROUP BY sck.sckid having sum(scd.scdkiek) > 0 ORDER BY scn.scndat LIMIT 1) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
WHERE sck.prid = 1376 GROUP BY prid; 
于 2013-04-24T07:34:39.267 回答