SQL查询多表连接计数和求和问题

发布于 2024-12-10 23:40:45 字数 1600 浏览 0 评论 0原文

有2张桌子。 1 个 tblBidder 和 1 个 tblItem。它们共享 1 个字段,用于在每个表中的字段之间建立关联。它是 bidder_number(对于 tblBidder)和 item_bidder_number(对于 tblItem)。然后,它们有一个对应的字段 bidder_sale_id (对于 tblBidder)和 item_sale_id (对于 tblItem)。此字段适用于一次销售期间全部出售的一组商品。因此,您可以单独出售 20 种不同的商品,但它们都具有相同的销售 ID,因为它们是在同一天出售的。

我正在尝试获取注册参加拍卖 235 的每种投标人类型的投标人总数的报告,并找出该特定组的拍卖总销售额是多少。以下是我的 SQL 查询:

SELECT     COUNT(dbo.tblBidder.bidder_type) AS TotalRegistrants, SUM(dbo.tblItem.item_premium + dbo.tblItem.item_pr) AS SumTotal, 
                  dbo.tblBidder.bidder_type
FROM         dbo.tblItem INNER JOIN
                  dbo.tblBidder ON dbo.tblItem.item_bidder_number = dbo.tblBidder.bidder_number     AND dbo.tblItem.item_sale_id = dbo.tblBidder.bidder_sale_id
WHERE     (dbo.tblItem.item_sale_id IN (235))
GROUP BY dbo.tblBidder.bidder_type

该查询返回了错误的注册者数量,但返回了正确的总和:

TotalRegistrants        SumTotal            bidder_type

10          3760.0000       Absentee Bidder
133         173870.6250     Floor Bidder
77          81300.0000      Internet Bidder
29          34862.2500      Mail Bidder
100         242790.2500     Phone Bidder

这只是为了获取注册者总数:

SELECT     COUNT(bidder_type) AS TotalBidders, bidder_type
FROM         dbo.tblBidder
WHERE     (bidder_sale_id = 235)
GROUP BY bidder_type

正确返回:

TotalBidders    bidder_type

41      Absentee Bidder
79      Floor Bidder
60      Internet Bidder
35      Mail Bidder
82      Phone Bidder

如果我尝试将 items 表连接到此最后一个查询,即使我不选择任何新列,TotalBiders 也会返回数千个数字。任何帮助将不胜感激。

There are 2 tables. 1 tblBidder and 1 tblItem. They share 1 field that makes the association between the fields in each table. It is the bidder_number (for tblBidder) and item_bidder_number (for tblItem). Then, they have a corresponding field of bidder_sale_id (for tblBidder) and item_sale_id (for tblItem). This field is for a group of items being sold all during 1 sitting. So you could have 20 different items being sold individually, but they all have the same sale ID because they are being sold on the same day.

I am trying to pull a report of the total number of bidders per bidder type who were registered for auction 235, and also find out how much the total sale was that auction for that particular group. Here is what I have for an SQL query:

SELECT     COUNT(dbo.tblBidder.bidder_type) AS TotalRegistrants, SUM(dbo.tblItem.item_premium + dbo.tblItem.item_pr) AS SumTotal, 
                  dbo.tblBidder.bidder_type
FROM         dbo.tblItem INNER JOIN
                  dbo.tblBidder ON dbo.tblItem.item_bidder_number = dbo.tblBidder.bidder_number     AND dbo.tblItem.item_sale_id = dbo.tblBidder.bidder_sale_id
WHERE     (dbo.tblItem.item_sale_id IN (235))
GROUP BY dbo.tblBidder.bidder_type

That query returned the incorrect number of registrants, but correct sum total:

TotalRegistrants        SumTotal            bidder_type

10          3760.0000       Absentee Bidder
133         173870.6250     Floor Bidder
77          81300.0000      Internet Bidder
29          34862.2500      Mail Bidder
100         242790.2500     Phone Bidder

This is just to get the total number of registrants:

SELECT     COUNT(bidder_type) AS TotalBidders, bidder_type
FROM         dbo.tblBidder
WHERE     (bidder_sale_id = 235)
GROUP BY bidder_type

Which returns correctly at:

TotalBidders    bidder_type

41      Absentee Bidder
79      Floor Bidder
60      Internet Bidder
35      Mail Bidder
82      Phone Bidder

If I try to join the items table to this last query, even if I don't select any new columns, the TotalBidders returns numbers in the thousands. Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

他是夢罘是命 2024-12-17 23:40:45

如果你尝试这个会怎么样?:

SELECT  COUNT(DISTINCT dbo.tblBidder.bidder_id) AS TotalRegistrants, 
        SUM(dbo.tblItem.item_premium + dbo.tblItem.item_pr) AS SumTotal,
        dbo.tblBidder.bidder_type 
FROM dbo.tblBidder 
LEFT JOIN dbo.tblItem 
ON dbo.tblItem.item_bidder_number = dbo.tblBidder.bidder_number
AND dbo.tblItem.item_sale_id = dbo.tblBidder.bidder_sale_id 
WHERE (dbo.tblBidder.bidder_sale_id  = 235) 
GROUP BY dbo.tblBidder.bidder_type 

What if you try this?:

SELECT  COUNT(DISTINCT dbo.tblBidder.bidder_id) AS TotalRegistrants, 
        SUM(dbo.tblItem.item_premium + dbo.tblItem.item_pr) AS SumTotal,
        dbo.tblBidder.bidder_type 
FROM dbo.tblBidder 
LEFT JOIN dbo.tblItem 
ON dbo.tblItem.item_bidder_number = dbo.tblBidder.bidder_number
AND dbo.tblItem.item_sale_id = dbo.tblBidder.bidder_sale_id 
WHERE (dbo.tblBidder.bidder_sale_id  = 235) 
GROUP BY dbo.tblBidder.bidder_type 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文