SQL Server 中多个表的多个聚合函数

发布于 2024-10-31 12:56:58 字数 1198 浏览 3 评论 0原文

我正在尝试获取输入人员的数据,我想提取特定人员的发票数量和行项目数量等数据。

输出是

Entered_by  No of line items
CD               9
CD               136084
deepa             7
deepa             18
dolly             757
dolly             22350
kroshni         666
kroshni         16161
lokesh           4
lokesh           999
MHeera           639
MHeera             20427
nandini            7
nandini            5318

这里的行项目数中的数据是“行项目数”计数和“发票数”计数的混合,我想显示类似

Entered_by  No of line items    No of invoices
CD               136084              9
deepa               18                7
dolly               22350               757

请帮我解决这个问题...... 

这是T- SQL查询

select ENTERED_BY, count(entered_by) 'NO OF LINE ITEMS'
from im_invoice, im_invoice_line_item, im_invoice_inventory 
where invoice_rid = invoice_fk
and invoice_inventory_rid = invoice_inv_fk
and enter_date between dateadd(mm, -3, getdate()) and dateadd(mm,0,getdate())
group by entered_by

union 

select entered_by, count(invoice_num) 'NO OF INVOICES' from im_invoice
where enter_date between dateadd(mm, -3, getdate()) and dateadd(mm,0,getdate())
group by entered_by

I am trying to get data for entered person, I want to pull out data as No of invoices and No of line items for particular person.

The output is

Entered_by  No of line items
CD               9
CD               136084
deepa             7
deepa             18
dolly             757
dolly             22350
kroshni         666
kroshni         16161
lokesh           4
lokesh           999
MHeera           639
MHeera             20427
nandini            7
nandini            5318

Here the data in No of line items is mixing of both ’ No of line items’ count and ‘No of invoices’ count, I want to show like

Entered_by  No of line items    No of invoices
CD               136084              9
deepa               18                7
dolly               22350               757

Please help me with this somebody ….. 

Here is the T-SQL query

select ENTERED_BY, count(entered_by) 'NO OF LINE ITEMS'
from im_invoice, im_invoice_line_item, im_invoice_inventory 
where invoice_rid = invoice_fk
and invoice_inventory_rid = invoice_inv_fk
and enter_date between dateadd(mm, -3, getdate()) and dateadd(mm,0,getdate())
group by entered_by

union 

select entered_by, count(invoice_num) 'NO OF INVOICES' from im_invoice
where enter_date between dateadd(mm, -3, getdate()) and dateadd(mm,0,getdate())
group by entered_by

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

z祗昰~ 2024-11-07 12:56:58

正如乔所说,如果您给我们更详细的描述,我们可以给您更好的答案,但在那之前,完成此操作的快速而肮脏的方法如下:

  1. 摆脱联合
  2. 将 2 个查询转换为派生表
  3. 从它们中选择连接在 Entered_by 上。

例如。

SELECT LineItems.ENTERED_BY, [NO OF LINE ITEMS], [NO OF INVOICES] 
FROM
(SELECT ENTERED_BY,COUNT(entered_by) 'NO OF LINE ITEMS' 
FROM im_invoice, im_invoice_line_item,im_invoice_inventory   
WHERE invoice_rid = invoice_fk  
AND invoice_inventory_rid = invoice_inv_fk
AND enter_date BETWEEN dateadd(mm, -3, getdate()) AND dateadd(mm,0,getdate())
GROUP BY entered_by) AS LineItems 
INNER JOIN 
(SELECT entered_by, count(invoice_num) 'NO OF INVOICES' 
FROM im_invoice  
WHERE enter_date BETWEEN dateadd(mm, -3, getdate()) AND dateadd(mm,0,getdate())  
GROUP BY entered_by ) AS invoices 
ON invoices.entered_by = LineItems.ENTERED_BY

As Joe said, if you give us a more detailed description we can give you better answers, but until then, quick and dirty way to accomplish this is as follows:

  1. Get rid of the union
  2. Turn the 2 queries into derived tables
  3. Select from them joining on entered_by.

Eg.

SELECT LineItems.ENTERED_BY, [NO OF LINE ITEMS], [NO OF INVOICES] 
FROM
(SELECT ENTERED_BY,COUNT(entered_by) 'NO OF LINE ITEMS' 
FROM im_invoice, im_invoice_line_item,im_invoice_inventory   
WHERE invoice_rid = invoice_fk  
AND invoice_inventory_rid = invoice_inv_fk
AND enter_date BETWEEN dateadd(mm, -3, getdate()) AND dateadd(mm,0,getdate())
GROUP BY entered_by) AS LineItems 
INNER JOIN 
(SELECT entered_by, count(invoice_num) 'NO OF INVOICES' 
FROM im_invoice  
WHERE enter_date BETWEEN dateadd(mm, -3, getdate()) AND dateadd(mm,0,getdate())  
GROUP BY entered_by ) AS invoices 
ON invoices.entered_by = LineItems.ENTERED_BY
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文