Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对sql的跨选项卡隐蔽访问_Sql_Sql Server_Ms Access - Fatal编程技术网

对sql的跨选项卡隐蔽访问

对sql的跨选项卡隐蔽访问,sql,sql-server,ms-access,Sql,Sql Server,Ms Access,我需要将其转换为SQL视图。这是MS Access的代码: TRANSFORM Count(Products.Id) AS AntalOfId SELECT Products.[id] FROM Sold INNER JOIN (Products INNER JOIN Cat ON Products.BelsCategoryId = Cat.[Id]) ON Sold.BelsProductId = Products.[id] GROUP BY Products.[id] PIVOT Sold.

我需要将其转换为SQL视图。这是MS Access的代码:

TRANSFORM Count(Products.Id) AS AntalOfId
SELECT Products.[id]
FROM Sold INNER JOIN (Products INNER JOIN Cat ON Products.BelsCategoryId = Cat.[Id]) ON Sold.BelsProductId = Products.[id]
GROUP BY Products.[id]
PIVOT Sold.Year;
你知道如何将其重新写入SQL吗


在产品中我有id,BelsCategoryID。 在分类中,我有ID。 在销售中,我有BelsProductID和年份。-此表显示了产品在给定年份的销售情况


如果产品在给定年份销售,则我希望结果以年份作为标题,然后是0或1,因为TRANSFORM在其他数据库中没有精确匹配,因为它可以生成数量可变的列。对于给定的一组年份,可以使用条件聚合:

SELECT SUM(CASE WHEN Sold.Year = 2020 THEN 1 ELSE 0 END) as year_2020,
       SUM(CASE WHEN Sold.Year = 2019 THEN 1 ELSE 0 END) as year_2019,
       SUM(CASE WHEN Sold.Year = 2018 THEN 1 ELSE 0 END) as year_2018       
FROM Products INNER JOIN
     Cat 
     ON Products.BelsCategoryId = Cat.[Id] INNER JOIN
     Sold 
     ON Sold.BelsProductId = Products.[id]
GROUP BY Products.[id]

如果您想要灵活的年限,您需要使用动态SQL。

您使用的是哪种dbms?使用Access 2019和SQL server manager v。18.3.1请提供样本数据和预期结果。另外,请指定目标数据库。在产品中,我有id BelsCategoryID。在分类中,我有销售中的ID,我有BelsProductID和年份。-此表显示了产品在给定年份的销售情况。我希望结果如下ProductID 2017 2018 2019 2020 1 1 1 0 1 2 0 1 1 1 1 3 1 0 10@Hoeberg . . . 如果这回答了你的问题,你可以接受答案。