我不是一个真正的程序员,我正在尝试查询给定年份的月份报价,采购订单和发票。我遇到的一些问题(尝试专门使用查询向导)已链接上面列出的3个类别的日期。例如
InvoiceCost|InvoiceDate|QuoteCost|QuoteDate|POCost|PODate
$$$ | Date | $$$ | Date | $$$ | Date
$$$ | Date | $$$ | Date | $$$ | Date
(我希望有效) 我想从这样的表中获得的是一个查询,它对类别进行求和并按月对它们进行排序。我得到的是行列出所有独特的日期组合,其类别总和为Via -
SELECT DISTINCTROW Format$([Table].[invoiceDate],'mmmm yyyy') AS [invoiceDate By Month],
Format$([Table].[PODate],'mmmm yyyy') AS [PODate By Month],
Format$([Table].[quoteDate],'mmmm yyyy') AS [quoteDate By Month],
Sum(Table.invoiceCost) AS [Sum Of invoiceCost],
Sum(Table.POCost) AS [Sum Of POCost],
Sum(Table.quoteCost) AS [Sum Of quoteCost]
FROM Table
GROUP BY Format$([Table].[invoiceDate],'mmmm yyyy'),
Format$([Table].[PODate],'mmmm yyyy'),
Format$([Table].[quoteDate],'mmmm yyyy'),
Year([Table].[invoiceDate])*12+DatePart('m',[Table].[invoiceDate])-1,
Year([Table].[PODate])*12+DatePart('m',[Table].[PODate])-1,
Year([Table].[quoteDate])*12+DatePart('m',[Table].[quoteDate])-1;
我想知道一些SQL“魔法”,它会链接表格中的日期列以避免唯一的日期组合。
修改
更具体地说,
<style type="text/css">
table.tableizer-table {
border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif
font-size: 12px;}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #ccc;}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;}
</style><table class="tableizer-table">
<tr class="tableizer-firstrow"><th>invoiceDate</th><th>QuoteDate</th><th>PODate</th><th>sumInvoice</th><th>sumQuote</th><th>sumPO</th></tr>
<tr><td> </td><td>17-Jul-13</td><td>01-Jul-13</td><td>$0.00</td><td>$0.00</td><td>$0.00</td></tr>
<tr><td>30-Jul-13</td><td>01-Jan-14</td><td>31-Mar-14</td><td>$100.00</td><td>$10.00</td><td>$48.00</td></tr>
<tr><td>30-Dec-13</td><td>01-Feb-14</td><td>10-Jan-14</td><td>$615.00</td><td>$50.00</td><td>$23.00</td></tr>
<tr><td>03-Jan-14</td><td>01-Jan-14</td><td>16-Mar-14</td><td>$3.00</td><td>$100.00</td><td>$615.00</td></tr>
<tr><td>16-Jan-14</td><td>01-Jan-14</td><td>20-Feb-14</td><td>$65.00</td><td>$50.00</td><td>$156.00</td></tr>
<tr><td>03-Mar-14</td><td>24-Jan-14</td><td>24-Feb-14</td><td>$0.00</td><td>$200.00</td><td>$0.00</td></tr>
<tr><td>11-Mar-14</td><td>18-Dec-13</td><td>21-Feb-14</td><td>$3,500.00</td><td>$5,000.00</td><td>$4,000.00</td></tr>
<tr><td>13-Mar-14</td><td>01-Jan-14</td><td>11-Dec-13</td><td>$15.00</td><td>$1,000.00</td><td>$231.00</td></tr>
<tr><td>15-Mar-14</td><td>30-Jan-14</td><td>19-Feb-14</td><td>$10.00</td><td>$8,000.00</td><td>$20.00</td></tr>
<tr><td>20-Mar-14</td><td>09-Jan-14</td><td>14-Feb-14</td><td>$1,000.00</td><td>$200.00</td><td>$20,000.00</td></tr>
<tr><td>21-Mar-14</td><td>01-Mar-14</td><td>20-Feb-14</td><td>$15.00</td><td>$10,000.00</td><td>$45.00</td></tr>
</table>
请注意,行中的日期会产生组合。我想看的是这样的
invoiceD|POD |quoteD |SumOfinvoiceCost|SumOfPOCost|SumOfquoteCost
1-2014 |1-2014 |1-2014 |$68.00 |$23.00 |$9,560.00
12-2013 |12-2013|12-2013|$615.00 |$231.00 |$5,000.00
3-2014 |3-2014 |3-2014 |$4,540.00 |$663.00 |$10,000.00
7-2013 |7-2013 |7-2013 |$100.00 |$0.00 |$0.00
我已经能够通过总共4个查询来实现这一点(3个用于按月分别生成类别总和,第4个用于将日期链接在一起。)SQL是否可以在一个查询中完成所有这些操作或者我应该在访问中创建3个单独的表来存储彼此远离的类别?
如果需要更多信息,请与我们联系。 感谢
答案 0 :(得分:0)
我想知道一些链接日期列的SQL“魔法” 在表格内,以避免独特的日期组合。
要避免使用唯一日期组合来获取您无法按所有列分组的总和,您必须仅按日期进行分组:
SELECT invoiceDate, QuoteDate, PODate, month(invoiceDate) as month,
sum(invoiceCost) as sumInvoice,
sum(QuoteCost) as sumQuote,
sum(SOCost) as sumSO
FROM Table
GROUP BY invoiceDate, QuoteDate, PODate, month(invoiceDate) as month
DISTINCT
上不需要SELECT
条款。
要按月分组,您必须在select
和group by
子句中加入该字段,但如果您没有字段month
,请尝试使用month()
在其中一个日期字段上运行,它具有可能出现的所有月份值。
答案 1 :(得分:0)
我认为你想要的是:
SELECT Format$(thedate,'mmmm yyyy') as MMMYYYY,
Sum(invoiceCost) AS [Sum Of invoiceCost],
Sum(POCost) AS [Sum Of POCost],
Sum(quoteCost) AS [Sum Of quoteCost]
FROM ((select invoiceDate as thedate, invoiceCost, NULL as POCost, NULL as quoteCost
from Table
) union all
(select PODate as thedate, NULL as InvoiceCost, POCost, NULL as quoteCost
from Table
) union all
(select QuoteDate as thedate, NULL as InvoiceCost, Null as POCost, NULL as quoteCost
from Table
)
) as t
GROUP BY Format$(thedate, 'mmmm yyyy')
ORDER BY min(thedate);
编辑:
Access可能不允许子查询中允许的子查询。您也可以使用左外连接执行此操作,如果您有月份列表。所以,如果我假设所有月份都有发票,那么你可以这样做:
SELECT i.MMMYYYY,
i.invoiceCost AS [Sum Of invoiceCost],
p.POCost AS [Sum Of POCost],
q.quoteCost AS [Sum Of quoteCost]
FROM ((select Format$(invoiceDate, 'mmm yyyy') as mmmyyyy, sum(InvoiceCost) as InvoiceCost
from Table t
group by Format$(invoiceDate, 'mmm yyyy')
) i left join
(select Format$(PODate, 'mmm yyyy') as mmmyyyy, sum(POCost) as POCost
from Table t
group by Format$(PODate, 'mmm yyyy')
) p
on i.mmmyyyy = p.mmmyyyy
) left join
(select Format$(QuoteDate, 'mmm yyyy') as mmmyyyy, sum(QuoteCost) as quoteCost
from Table t
group by Format$(QuoteDate, 'mmm yyyy')
) q
on i.mmmyyyy = q.mmmyyyy