如何在MS Access中使用多对多关系进行计算

发布于 2025-01-03 20:39:06 字数 887 浏览 1 评论 0原文

我有一个具有以下结构的数据库。每本书可以有多个作者,每个作者可以写多本书。

[book:book_id, book_name, book_price]
[author: author_id, author_name]
[link:book_id, author_id]

{book_id 和author_id 已链接。完整的结构如下所示:https://i.sstatic.net/vIFNU.png}

现在每本书都有一个价格(货币)。价格的30%应平均分配给对本书做出贡献的每位作者。 我的问题是如何找到特定年份每个作者的总付款。

[我自己想到了一个解决方案。我只能做第 1 步。如果您可以向我提供一些提示或材料,让我可以找到如何进行此类操作,这将非常有帮助]

我的解决方案的算法是:

  1. 因此对于每个 book_id ,我需要找到中间表中具有相同book_id的author_id的数量。 (可以通过查询来完成)

  2. 如果我将 book_price 除以 number_of_author_in_book 并乘以 30/100,我会得到该书的钱,该钱将转到该书的每个作者的帐户(例如 payment_of_one_author_in_book)

  3. 对于中间表中的每个author_id,我查找相应的 book_id 并将该author_id 的 payment_of_one_author_in_book 添加到与作者 ID 对应的新变量(author_ payment_this_year),如果年份与查询年份匹配。

提前致谢

I have a database having the following structure. Each book can have multiple authors, and each author can write multiple books.

[book:book_id, book_name, book_price]
[author: author_id, author_name]
[link:book_id, author_id]

{book_id's and author_id's are linked. the complete structure is shown here: https://i.sstatic.net/vIFNU.png}

Now each book has a price (currency). 30% of the price should be equally distributed to each author who have contributed to the book.
My question is how to find the total payment for each author for a particular year.

[I thought of a solution my self. I could do only up to step 1. If you can provide me some hints or materials where I can find how do such manipulations, it would be very helpful]

Algorithm of my solution is:

  1. So for each book_id, I need to find the number of author_id's in the middle table who has the same book_id. (could do it by query)

  2. If I divide the book_price by number_of_author_in_book and multiply it with 30/100, I get the money for that book that will go to the each author's account for that book (say payment_of_one_author_in_book)

  3. For each author_id in the middle table, I look up for the corresponding book_id and add the payment_of_one_author_in_book for that author_id to a new variable (author_payment_this_year) corresponding to the author ID, if the year matches to the query year.

Thanks in advance

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

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

发布评论

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

评论(2

天煞孤星 2025-01-10 20:39:07
SELECT author.AuthorId, author.Author_name, book.Book_Name, book.Book_Price, [Book_Price]/DCount("[Author_id]","[Link]","[Book_id]=" & [Book_Id]) AS Share
FROM (author INNER JOIN link AS link_1 ON author.AuthorId = link_1.Author_id) INNER JOIN book ON link_1.Book_id = book.Book_Id;

基本上,它只是三个表的联接。唯一棘手的一点是使用 DCount 函数来计算 Link 中有多少作者共享这本书的 ID

SELECT author.AuthorId, author.Author_name, book.Book_Name, book.Book_Price, [Book_Price]/DCount("[Author_id]","[Link]","[Book_id]=" & [Book_Id]) AS Share
FROM (author INNER JOIN link AS link_1 ON author.AuthorId = link_1.Author_id) INNER JOIN book ON link_1.Book_id = book.Book_Id;

Basically, it is just a join of the three tables. The only tricky bit is using DCount function to add up how many authors in Link share this book_ID

ぃ双果 2025-01-10 20:39:06

此示例包括别名和子查询。

SELECT 
    a.author_id,
    a.author_name,
    SUM(share.auth_share) AS author_total
FROM (link l
INNER JOIN (
    SELECT 
        b.book_id,
        ( [b.book_price] * 0.3 ) / [no_auth] AS auth_share
    FROM book b
    INNER JOIN (
        SELECT 
            l.book_id,
            COUNT(l.author_id) AS no_auth
        FROM   link l
        GROUP  BY l.book_id) AS ac
    ON b.book_id = ac.book_id) AS share
ON l.book_id = share.book_id)
INNER JOIN author a
ON l.author_id = a.author_id
GROUP BY a.author_id,a.author_name

This example includes aliases and subqueries.

SELECT 
    a.author_id,
    a.author_name,
    SUM(share.auth_share) AS author_total
FROM (link l
INNER JOIN (
    SELECT 
        b.book_id,
        ( [b.book_price] * 0.3 ) / [no_auth] AS auth_share
    FROM book b
    INNER JOIN (
        SELECT 
            l.book_id,
            COUNT(l.author_id) AS no_auth
        FROM   link l
        GROUP  BY l.book_id) AS ac
    ON b.book_id = ac.book_id) AS share
ON l.book_id = share.book_id)
INNER JOIN author a
ON l.author_id = a.author_id
GROUP BY a.author_id,a.author_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文