如何在使用 2 个表和 GROUP BY 语法的同时从 1 个表中选择数据?

发布于 2024-11-09 09:21:41 字数 870 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2024-11-16 09:21:41

在内联视图中使用

SELECT tblproduct.ftechnicalno, 
       tblproduct.fname, 
       tblproduct.fdesc, 
       tblproduct.fregisterdate 
FROM   tblproduct 
       INNER JOIN (SELECT SUM(tblorders.fcount) AS sumoffcount, 
                          tblorders.fsaledate, 
                          tblorders.fxproductid tblorders 
                   GROUP  BY tblorders.fsaledate, 
                             tblorders.fxproductid) orderdata 
         ON tblproduct.fid = orderdata.fxproductid 
WHERE  ( tblproduct.fregisterdate >= @Since ) 
       AND ( tblproduct.fregisterdate <= @To ) 

使用公用表表达式

With orderdata  as 
(SELECT SUM(tblorders.fcount) AS sumoffcount, 
                              tblorders.fsaledate, 
                              tblorders.fxproductid tblorders 
                       GROUP  BY tblorders.fsaledate, 
                                 tblorders.fxproductid) 
   SELECT tblproduct.ftechnicalno, 
           tblproduct.fname, 
           tblproduct.fdesc, 
           tblproduct.fregisterdate 
    FROM   tblproduct 
           INNER JOIN orderdata 
             ON tblproduct.fid = orderdata.fxproductid 
    WHERE  ( tblproduct.fregisterdate >= @Since ) 
           AND ( tblproduct.fregisterdate <= @To ) 

Using in inline view

SELECT tblproduct.ftechnicalno, 
       tblproduct.fname, 
       tblproduct.fdesc, 
       tblproduct.fregisterdate 
FROM   tblproduct 
       INNER JOIN (SELECT SUM(tblorders.fcount) AS sumoffcount, 
                          tblorders.fsaledate, 
                          tblorders.fxproductid tblorders 
                   GROUP  BY tblorders.fsaledate, 
                             tblorders.fxproductid) orderdata 
         ON tblproduct.fid = orderdata.fxproductid 
WHERE  ( tblproduct.fregisterdate >= @Since ) 
       AND ( tblproduct.fregisterdate <= @To ) 

Using a Common table expression

With orderdata  as 
(SELECT SUM(tblorders.fcount) AS sumoffcount, 
                              tblorders.fsaledate, 
                              tblorders.fxproductid tblorders 
                       GROUP  BY tblorders.fsaledate, 
                                 tblorders.fxproductid) 
   SELECT tblproduct.ftechnicalno, 
           tblproduct.fname, 
           tblproduct.fdesc, 
           tblproduct.fregisterdate 
    FROM   tblproduct 
           INNER JOIN orderdata 
             ON tblproduct.fid = orderdata.fxproductid 
    WHERE  ( tblproduct.fregisterdate >= @Since ) 
           AND ( tblproduct.fregisterdate <= @To ) 
两个我 2024-11-16 09:21:41

您不能按聚合进行分组...否则您的查询很好..

SELECT     
      tblProduct.fTechnicalNo, 
      tblProduct.fName, 
      tblProduct.fDesc, 
      tblProduct.fRegisterDate, 
      tblOrders.fSaleDate,
      SUM(tblOrders.fCount) AS Expr1
   FROM
      tblProduct 
         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

You can't do a group by an aggregate... Your query was otherwise fine..

SELECT     
      tblProduct.fTechnicalNo, 
      tblProduct.fName, 
      tblProduct.fDesc, 
      tblProduct.fRegisterDate, 
      tblOrders.fSaleDate,
      SUM(tblOrders.fCount) AS Expr1
   FROM
      tblProduct 
         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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文