简体   繁体   English

SQL - 在月份有WHERE时获得所有月份的总和

[英]SQL - Getting Sum of all month payable while having a WHERE on months

Edited : Thank you every one for your response but it seems it all went out of context. 编辑:谢谢大家的回复,但似乎一切都脱离了背景。 My fault I should have gone into more details. 我的错,我应该了解更多细节。

The query is for Master-Child reportviewer (SQL Server 2008 R2 at backend); 该查询适用于Master-Child reportviewer(后端的SQL Server 2008 R2); where delvoucher has a 1:m relation with dvdetails table. 其中delvoucher与dvdetails表的关系为1:m。 The report is actually an invoice, that display cient_title and deliverydate etc. on top (list) and details of delivery in the inner tablix (Master-Child report scenario). 该报告实际上是一张发票,在顶部(列表)上显示cient_title和deliverydate等,并在内部Tablix中显示交付详情(Master-Child报告方案)。 At the bottom, I show sum of all the amount payable for selected month. 在底部,我显示所选月份的所有应付金额的总和。 ( the month is selected through a date time picker placed in same form that also contains my reportviewer control and refreshes report on selected value change). (通过日期时间选择器选择月份,该日期时间选择器以相同的形式放置,同时包含我的reportviewer控件并刷新有关所选值更改的报告)。 However, I want to get the total payable since beginning of customer minus this month amount ie balance payable). 但是,我希望从客户开始减去本月金额(即应付余额)后得到应付总额。 Shall I use UNION? 我要用UNION吗? and run a separate query to get SUM(amount) without applying WHERE month is blah blah. 并运行一个单独的查询来获得SUM(金额)而不应用WHERE月是等等等等。

the overall problem is i have to display arrear (sum(payable) - sum(paid)) where paid amount is to be got from another table : Edit Close 整体问题是我必须显示欠款(总和(应付款) - 总和(已付款)),其中付款金额将从另一张表中获得:编辑关闭

My Query 我的查询

SELECT        
    delvoucher.dvdate, 
    delvoucher.dvno, 
    delvoucher.vehicleno, 
    delvoucher.salesman, 
    dvdetails.[desc], 
    dvdetails.supply, 
    dvdetails.empty, 
    dvdetails.amount, 
    dvdetails.rate, 
    delvoucher.title, 
    customer.currentaddress, 
    customer.phone, 
    customer.code
FROM         
    delvoucher 
    INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
    INNER JOIN customer ON delvoucher.title = customer.title
WHERE        
    YEAR(delvoucher.dvdate) = YEAR(@i) 
    AND MONTH(delvoucher.dvdate) = MONTH(@i)

All I want is retrieve SUM(dvdetails.amount) as well, eg 我想要的只是检索SUM(dvdetails.amount),例如

SELECT        
    SUM(dvdetails.amount) , 
    delvoucher.dvdate, 
    delvoucher.dvno, ............

It may be simple but its giving me error. 它可能很简单,但它给我错误。 where am i going wrong 我哪里错了

You can use GROUP BY statement with SUM() to make a monthly summary like this: 您可以将GROUP BY语句与SUM()来制作如下的月度摘要:

SELECT 
    SUM(dvdetails.amount), 
    YEAR(delvoucher.dvdate), 
    MONTH(delvoucher.dvdate) 
FROM
    delvoucher 
    INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
    INNER JOIN customer ON delvoucher.title = customer.title
WHERE
    YEAR(delvoucher.dvdate) = YEAR(@i) 
    AND MONTH(delvoucher.dvdate) = MONTH(@i)
GROUP BY 
    YEAR(delvoucher.dvdate), 
    MONTH(delvoucher.dvdate)

You can read a related question here: SQL query to retrieve SUM in various DATE ranges 您可以在此处阅读相关问题: SQL查询以在各种DATE范围内检索SUM

And a document of GOUP BY is here: http://www.w3schools.com/sql/sql_groupby.asp GOUP BY的文档在这里: http//www.w3schools.com/sql/sql_groupby.asp

Hope this helps. 希望这可以帮助。

@naota has the right answer but in case your looking for the select like this ie to return all rows of delvoucher: @naota有正确的答案但是如果你正在寻找像这样的选择,即返回所有的delvoucher行:

SELECT        
    SUM(dvdetails.amount) , 
    delvoucher.dvdate, 
    delvoucher.dvno, ............

Rather than 而不是

SELECT 
    AmountSum = SUM(dvdetails.amount),
    Year = YEAR(delvoucher.dvdate),
    Month = MONTH(delvoucher.dvdate)

Then your query could look like this (untested) 然后你的查询看起来像这样(未经测试)

SELECT        
    totals.AmountSum,
    delvoucher.dvdate, 
    delvoucher.dvno, 
    delvoucher.vehicleno, 
    delvoucher.salesman, 
    dvdetails.[desc], 
    dvdetails.supply, 
    dvdetails.empty, 
    dvdetails.amount, 
    dvdetails.rate, 
    delvoucher.title, 
    customer.currentaddress, 
    customer.phone, 
    customer.code
FROM         
    delvoucher 
    INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
    INNER JOIN customer ON delvoucher.title = customer.title
    INNER JOIN (   
        SELECT 
            AmountSum = SUM(dvdetails.amount),
            Year = YEAR(delvoucher.dvdate),
            Month = MONTH(delvoucher.dvdate) 
        FROM
            delvoucher 
            INNER JOIN dvdetails ON delvoucher.id = dvdetails.pid 
            INNER JOIN customer ON delvoucher.title = customer.title
        WHERE
            YEAR(delvoucher.dvdate) = YEAR(@i) 
            AND MONTH(delvoucher.dvdate) = MONTH(@i)
        GROUP BY 
            YEAR(delvoucher.dvdate), 
            MONTH(delvoucher.dvdate)
    ) totals ON totals.Year = YEAR(delvoucher.dvdate) AND totals.Month = MONTH(delvoucher.dvdate)
WHERE        
    YEAR(delvoucher.dvdate) = YEAR(@i) 
    AND MONTH(delvoucher.dvdate) = MONTH(@i)

Basically your query inner joins @naota's query joining on year and month 基本上你的查询内部加入@ naota的查询加入年和月

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 sql 中获取一年中几个月的每个月的总和 - getting sum for each month for several months in a year in sql 过去 12 个月的数据总和,其中每个月有 2 行数据 — Spark SQL - sum of last 12 months of data where each month has 2 rows of data — Spark SQL 按月、年和帐户对直到今天月份的所有月份的值求和,并在特定行不存在数据的情况下显示 0 的值 - Sum values by month, year, and by account, for all months leading up to todays month and show 0's values where data does not exist for specific row SQL两个具有不同月份值的表。 在所有月份中获得一个结果集 - SQL two tables with different month values. Getting one result set for all the months 在SQL或SSRS中将月份总和显示为年 - Getting sum of months to show as years in SQL or SSRS 每月获取所有交易的总和,包括没有交易的月份 - Get Sum Of All Transactions Per Month Including Months With No Transaction 每月前 12 个月的滚动总和(SQL-雪花) - Rolling sum previous 12 months per month (SQL- Snowflake) Oracle SQL表达式可根据选定的月份计算六个月的总和 - Oracle SQL Expression to Sum Payments for Six Months based on Selected Month sql为每月选择一条记录,并记录该月记录的总和 - sql to select one record for every month with a sum of that months records T-SQL查询以选择最近3个月的数据,去年的月份,本月的平均值和所有月份的平均值 - T-SQL query to select last 3 months of data, last years month, averages of this month and average of all months
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM