clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2591543 fiddles created (45740 in the last week).

WITH d AS ( SELECT '2010-01' AS YearMonthDate UNION ALL SELECT '2010-02' UNION ALL SELECT '2010-03' UNION ALL SELECT '2010-04' UNION ALL SELECT '2010-05' UNION ALL SELECT '2010-06' UNION ALL SELECT '2010-07' UNION ALL SELECT '2010-08' UNION ALL SELECT '2010-08' UNION ALL SELECT '2010-09' UNION ALL SELECT '2010-10' UNION ALL SELECT '2010-11' UNION ALL SELECT '2010-12' UNION ALL SELECT '2011-01' UNION ALL SELECT '2011-02' UNION ALL SELECT '2011-03' UNION ALL SELECT '2011-04' UNION ALL SELECT '2011-05' UNION ALL SELECT '2011-06' UNION ALL SELECT '2011-07' UNION ALL SELECT '2011-08' UNION ALL SELECT '2011-08' UNION ALL SELECT '2011-09' UNION ALL SELECT '2011-10' UNION ALL SELECT '2011-11' UNION ALL SELECT '2011-12' UNION ALL SELECT '2012-01' UNION ALL SELECT '2012-02' UNION ALL SELECT '2012-03' UNION ALL SELECT '2012-04' UNION ALL SELECT '2012-05' UNION ALL SELECT '2012-06' UNION ALL SELECT '2012-07' UNION ALL SELECT '2012-08' UNION ALL SELECT '2012-08' UNION ALL SELECT '2012-09' UNION ALL SELECT '2012-10' UNION ALL SELECT '2012-11' UNION ALL SELECT '2012-12' UNION ALL SELECT '2013-01' UNION ALL SELECT '2013-02' UNION ALL SELECT '2013-03' UNION ALL SELECT '2013-04' UNION ALL SELECT '2013-05' UNION ALL SELECT '2013-06' UNION ALL SELECT '2013-07' UNION ALL SELECT '2013-08' UNION ALL SELECT '2013-08' UNION ALL SELECT '2013-09' UNION ALL SELECT '2013-10' UNION ALL SELECT '2013-11' UNION ALL SELECT '2013-12' UNION ALL SELECT '2014-01' UNION ALL SELECT '2014-02' UNION ALL SELECT '2014-03' UNION ALL SELECT '2014-04' UNION ALL SELECT '2014-05' UNION ALL SELECT '2014-06' UNION ALL SELECT '2014-07' UNION ALL SELECT '2014-08' UNION ALL SELECT '2014-08' UNION ALL SELECT '2014-09' UNION ALL SELECT '2014-10' UNION ALL SELECT '2014-11' UNION ALL SELECT '2014-12' UNION ALL SELECT '2015-01' UNION ALL SELECT '2015-02' UNION ALL SELECT '2015-03' UNION ALL SELECT '2015-04' UNION ALL SELECT '2015-05' UNION ALL SELECT '2015-06' UNION ALL SELECT '2015-07' UNION ALL SELECT '2015-08' UNION ALL SELECT '2015-08' UNION ALL SELECT '2015-09' UNION ALL SELECT '2015-10' UNION ALL SELECT '2015-11' UNION ALL SELECT '2015-12' UNION ALL SELECT '2016-01' UNION ALL SELECT '2016-02' UNION ALL SELECT '2016-03' UNION ALL SELECT '2016-04' UNION ALL SELECT '2016-05' UNION ALL SELECT '2016-06' UNION ALL SELECT '2016-07' UNION ALL SELECT '2016-08' UNION ALL SELECT '2016-08' UNION ALL SELECT '2016-09' UNION ALL SELECT '2016-10' UNION ALL SELECT '2016-11' UNION ALL SELECT '2016-12' UNION ALL SELECT '2017-01' UNION ALL SELECT '2017-02' UNION ALL SELECT '2017-03' UNION ALL SELECT '2017-04' UNION ALL SELECT '2017-05' UNION ALL SELECT '2017-06' UNION ALL SELECT '2017-07' UNION ALL SELECT '2017-08' UNION ALL SELECT '2017-08' UNION ALL SELECT '2017-09' UNION ALL SELECT '2017-10' UNION ALL SELECT '2017-11' UNION ALL SELECT '2017-12' UNION ALL SELECT '2018-01' UNION ALL SELECT '2018-02' UNION ALL SELECT '2018-03' UNION ALL SELECT '2018-04' UNION ALL SELECT '2018-05' UNION ALL SELECT '2018-06' UNION ALL SELECT '2018-07' UNION ALL SELECT '2018-08' UNION ALL SELECT '2018-08' UNION ALL SELECT '2018-09' UNION ALL SELECT '2018-10' UNION ALL SELECT '2018-11' UNION ALL SELECT '2018-12' UNION ALL SELECT '2019-01' UNION ALL SELECT '2019-02' UNION ALL SELECT '2019-03' UNION ALL SELECT '2019-04' UNION ALL SELECT '2019-05' UNION ALL SELECT '2019-06' UNION ALL SELECT '2019-07' UNION ALL SELECT '2019-08' UNION ALL SELECT '2019-08' UNION ALL SELECT '2019-09' UNION ALL SELECT '2019-10' UNION ALL SELECT '2019-11' UNION ALL SELECT '2019-12' ), p AS ( SELECT 'Apple' AS ProductBrand UNION ALL SELECT 'Samsung' UNION ALL SELECT 'Sony' UNION ALL SELECT 'LG' ), yourTable AS ( SELECT 'Apple' AS ProductBrand, '2015-03' AS PurchasedDate, '2015-05' AS SoldDate UNION ALL SELECT 'Samsung', '2014-01', '2015-03' UNION ALL SELECT 'Sony', '2016-02', '2016-05' UNION ALL SELECT 'Sony', '2013-01', '2013-08' UNION ALL SELECT 'Apple', '2015-05', '2015-10' UNION ALL SELECT 'LG', '2011-02', '2014-06' UNION ALL SELECT 'Samsung', '2017-02', '2017-04' UNION ALL SELECT 'LG', '2016-01', '2016-06' UNION ALL SELECT 'LG', '2018-06', '2019-01' ) SELECT t.ProductBrand, d.YearMonthDate, COUNT(t.ProductBrand) AS NitemsInventory FROM p CROSS JOIN d LEFT JOIN yourTable t ON d.YearMonthDate BETWEEN t.PurchasedDate AND t.SoldDate AND p.ProductBrand = t.ProductBrand WHERE t.ProductBrand IS NOT NULL GROUP BY t.ProductBrand, d.YearMonthDate;
ProductBrand YearMonthDate NitemsInventory
LG 2011-02 1
LG 2011-03 1
LG 2011-04 1
LG 2011-05 1
LG 2011-06 1
LG 2011-07 1
LG 2011-08 2
LG 2011-09 1
LG 2011-10 1
LG 2011-11 1
LG 2011-12 1
LG 2012-01 1
LG 2012-02 1
LG 2012-03 1
LG 2012-04 1
LG 2012-05 1
LG 2012-06 1
LG 2012-07 1
LG 2012-08 2
LG 2012-09 1
LG 2012-10 1
LG 2012-11 1
LG 2012-12 1
LG 2013-01 1
Sony 2013-01 1
LG 2013-02 1
Sony 2013-02 1
LG 2013-03 1
Sony 2013-03 1
LG 2013-04 1
Sony 2013-04 1
LG 2013-05 1
Sony 2013-05 1
LG 2013-06 1
Sony 2013-06 1
LG 2013-07 1
Sony 2013-07 1
LG 2013-08 2
Sony 2013-08 2
LG 2013-09 1
LG 2013-10 1
LG 2013-11 1
LG 2013-12 1
LG 2014-01 1
Samsung 2014-01 1
LG 2014-02 1
Samsung 2014-02 1
LG 2014-03 1
Samsung 2014-03 1
LG 2014-04 1
Samsung 2014-04 1
LG 2014-05 1
Samsung 2014-05 1
LG 2014-06 1
Samsung 2014-06 1
Samsung 2014-07 1
Samsung 2014-08 2
Samsung 2014-09 1
Samsung 2014-10 1
Samsung 2014-11 1
Samsung 2014-12 1
Samsung 2015-01 1
Samsung 2015-02 1
Samsung 2015-03 1
Apple 2015-03 1
Apple 2015-04 1
Apple 2015-05 2
Apple 2015-06 1
Apple 2015-07 1
Apple 2015-08 2
Apple 2015-09 1
Apple 2015-10 1
LG 2016-01 1
LG 2016-02 1
Sony 2016-02 1
LG 2016-03 1
Sony 2016-03 1
LG 2016-04 1
Sony 2016-04 1
LG 2016-05 1
Sony 2016-05 1
LG 2016-06 1
Samsung 2017-02 1
Samsung 2017-03 1
Samsung 2017-04 1
LG 2018-06 1
LG 2018-07 1
LG 2018-08 2
LG 2018-09 1
LG 2018-10 1
LG 2018-11 1
LG 2018-12 1
LG 2019-01 1
 hidden batch(es)