我正在尝试编写此SQL语句以从另一个表中选择日期范围并将其用于过滤结果。
我把SQL语句放在一起,但不管怎么说它不起作用。
感谢您的帮助
SELECT
a.jednota,datum,
SUM(CASE WHEN a.co='prijem' THEN a.kusy ELSE NULL END) as prijem_ks,
SUM(CASE WHEN a.co='predaj' THEN a.kusy ELSE NULL END) as predaj_ks
FROM
jednotypredaj as a
SELECT
b.id, b.fromd, b.tod
FROM
datadocasne as b
WHERE
(a.tovar LIKE 'kalan' AND datum BETWEEN 'b.fromd' AND 'b.tod')
GROUP BY
a.jednota
ORDER BY
predaj_ks DESC
表datadocasne
:
id fromt tod
---------------------------------
1 12.4.2016 22.5.2016
表jednotypredaj
:
co datum jednota tovar kusy
------------------------------------------------
predaj 12.5.2016 TR003 kalan 5
predaj 25.5.2016 TR278 aechmea 6
predaj 11.4.2016 TR202 phalenopsis 8
结果应如下所示
co datum jednota tovar kusy
predaj 12.5.2016 TR003 kalan 5
答案 0 :(得分:1)
只需在JOIN
子句中添加条件。
SELECT
J.co,
J.datum,
J.jednota,
J.tovar,
J.kusy
FROM
datadocasne D
INNER JOIN jednotypredaj J ON J.datum BETWEEN D.fromd AND D.tod
答案 1 :(得分:0)
您可能需要做这样的事情......
SELECT a.jednota,b.id,b.fromd,b.tod,
SUM(CASE WHEN a.co='prijem' THEN a.kusy ELSE NULL END) as prijem_ks,
SUM(CASE WHEN a.co='predaj' THEN a.kusy ELSE NULL END) as predaj_ks
FROM jednotypredaj a
join datadocasne b on a.datum between b.fromd and b.tod
WHERE a.tovar LIKE 'kalan'
GROUP BY a.jednota,b.id,b.fromd,b.tod,
SUM(CASE WHEN a.co='prijem' THEN a.kusy ELSE NULL END),
SUM(CASE WHEN a.co='predaj' THEN a.kusy ELSE NULL END)
ORDER by predaj_ks DESC