如何在使用 2 个表和 GROUP BY 语法的同时从 1 个表中选择数据?
我在 SQL Server 中有两个表连接在一起。当我想应用 GROUP BY
语法并从一个表中进行 SELECT 时,它会影响到第二个表。
我如何SELECTGROUP BY
语法时,来自 1 个表的 code> 数据?
SELECT
tblProduct.fTechnicalNo, tblProduct.fName,
tblProduct.fDesc, tblProduct.fRegisterDate,
SUM(tblOrders.fCount) AS Expr1, tblOrders.fSaleDate
FROM tblProduct
INNER JOIN tblOrders ON tblProduct.fId = tblOrders.fxProductId
WHERE
(tblProduct.fRegisterDate >= @Since)
AND (tblProduct.fRegisterDate <= @To)
GROUP BY
tblProduct.fTechnicalNo, tblProduct.fName,
tblProduct.fDesc, tblProduct.fRegisterDate,
tblOrders.fSaleDate, tblOrders.fCount
我只想SELECT
tblProduct
data 。但 GROUP BY 语法会更改结果并在 result 中显示 tblOrders 数据。我只想要 tblProduct
结果和 tblOrders
中的 1 个字段
I have two tables in SQL Server that are joined together .. When I want to apply GROUP BY
syntax and SELECT from one table, it affected into second table ..
How can I SELECT
data from 1 table while I'm using 2 tables and GROUP BY
syntax ?
SELECT
tblProduct.fTechnicalNo, tblProduct.fName,
tblProduct.fDesc, tblProduct.fRegisterDate,
SUM(tblOrders.fCount) AS Expr1, tblOrders.fSaleDate
FROM tblProduct
INNER JOIN tblOrders ON tblProduct.fId = tblOrders.fxProductId
WHERE
(tblProduct.fRegisterDate >= @Since)
AND (tblProduct.fRegisterDate <= @To)
GROUP BY
tblProduct.fTechnicalNo, tblProduct.fName,
tblProduct.fDesc, tblProduct.fRegisterDate,
tblOrders.fSaleDate, tblOrders.fCount
I want to SELECT
just tblProduct
data . but GROUP BY
syntax change the result and shows the tblOrders
data in result . I want just tblProduct
result and just 1 field from tblOrders
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在内联视图中使用
使用公用表表达式
Using in inline view
Using a Common table expression
您不能按聚合进行分组...否则您的查询很好..
You can't do a group by an aggregate... Your query was otherwise fine..