需要帮助设计正确的 SQL 查询

发布于 2024-10-15 23:44:29 字数 1086 浏览 3 评论 0原文

我有这张表:

DebitDate | DebitTypeID | DebitPrice | DebitQuantity
----------------------------------------------------
40577       1             50           3
40577       1             100          1
40577       2             75           2
40578       1             50           2
40578       2             150          2

我想通过单个查询获得(如果可能的话),这些详细信息: date、debit_id、total_sum_of_same_debit、how_many_debits_per_day

因此从上面的示例中我会得到:

40577, 1, (50*3)+(100*1), 2 (because 40577 has 1 and 2 so total of 2 debits per this day)
40577, 2, (75*2), 2 (because 40577 has 1 and 2 so total of 2 debits per this day)
40578, 1, (50*2), 2 (because 40578 has 1 and 2 so total of 2 debits per this day)
40578, 2, (150*2), 2 (because 40578 has 1 and 2 so total of 2 debits per this day)

所以我有这个 SQL 查询:

SELECT      DebitDate, DebitTypeID, SUM(DebitPrice*DebitQuantity) AS TotalSum
FROM        DebitsList
GROUP BY    DebitDate, DebitTypeID, DebitPrice, DebitQuantity

现在我遇到了麻烦,我不确定在哪里放置我需要的最后一个信息的计数。

I have this table:

DebitDate | DebitTypeID | DebitPrice | DebitQuantity
----------------------------------------------------
40577       1             50           3
40577       1             100          1
40577       2             75           2
40578       1             50           2
40578       2             150          2

I would like to get with a single query (if that's possible), these details:
date, debit_id, total_sum_of_same_debit, how_many_debits_per_day

so from the example above i would get:

40577, 1, (50*3)+(100*1), 2 (because 40577 has 1 and 2 so total of 2 debits per this day)
40577, 2, (75*2), 2 (because 40577 has 1 and 2 so total of 2 debits per this day)
40578, 1, (50*2), 2 (because 40578 has 1 and 2 so total of 2 debits per this day)
40578, 2, (150*2), 2 (because 40578 has 1 and 2 so total of 2 debits per this day)

So i have this sql query:

SELECT      DebitDate, DebitTypeID, SUM(DebitPrice*DebitQuantity) AS TotalSum
FROM        DebitsList
GROUP BY    DebitDate, DebitTypeID, DebitPrice, DebitQuantity

And now i'm having trouble and i'm not sure where to put the count for the last info i need.

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

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

发布评论

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

评论(2

空心空情空意 2024-10-22 23:44:29

您将需要一个相关子查询来获取这个新列。您还需要从 GROUP BY 子句中删除 DebitPrice 和 DebitQuantity 才能使其正常工作。

SELECT   DebitDate,
         DebitTypeID,
         SUM(DebitPrice*DebitQuantity) AS TotalSum,
         (   select Count(distinct E.DebitTypeID)
             from DebitsList E
             where E.DebitDate=D.DebitDate) as CountDebits
FROM     DebitsList D
GROUP BY DebitDate, DebitTypeID

You would need a correlated subquery to get this new column. You also need to drop DebitPrice and DebitQuantity from the GROUP BY clause for it to work.

SELECT   DebitDate,
         DebitTypeID,
         SUM(DebitPrice*DebitQuantity) AS TotalSum,
         (   select Count(distinct E.DebitTypeID)
             from DebitsList E
             where E.DebitDate=D.DebitDate) as CountDebits
FROM     DebitsList D
GROUP BY DebitDate, DebitTypeID
感悟人生的甜 2024-10-22 23:44:29

我想这可以帮助你。

SELECT      DebitDate,  SUM(DebitPrice*DebitQuantity) AS TotalSum, Count(DebitDate) as DebitDateCount
FROM        DebitsList where DebitTypeID = 1
GROUP BY    DebitDate

I think this can help you.

SELECT      DebitDate,  SUM(DebitPrice*DebitQuantity) AS TotalSum, Count(DebitDate) as DebitDateCount
FROM        DebitsList where DebitTypeID = 1
GROUP BY    DebitDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文