SQL - 总结每月销售数据(SQL - Summarizing Monthly Sales Data)
我有两个表需要合并,但我无法看到联接起作用。 图片有三张桌子
_date_array2:有一个DateMonthYr字段,其中包含所有可能的日期/年份组合
_sales_summ_tbl__ =>有5个字段。 只有销售月份出现。 例如,您将看到只有三条记录显示在第二个表格中。 例如,没有5/2016,因为那个月没有销售。
我的目标是“填充”第二个表,使TotalDemand为0,持续几个月没有销售。 我非常接近(请参阅第三张表格),但我无法让PartNumber显示在没有销售的日期。
我的猜测是这是由于RIGHT JOIN。 但我不知道如何处理这个问题。 我希望的输出是表3,但所有条目都填入了部件号。
这里是我的代码(运行此代码的结果是图片中的第三个/最后一个表):
SELECT TmpSalesTbl.PartNumber as PartNumber, tmp_date_array.CreateDateMonth as CreateDateMonth, tmp_date_array.CreateDateYear as CreateDateYear, CASE WHEN TmpSalesTbl.TotalDemand is NULL THEN 0 ELSE TmpSalesTbl.TotalDemand END as TotalDemand FROM #_sales_summ_tbl__ TmpSalesTbl RIGHT JOIN #_date_array2 tmp_date_array on tmp_date_array.CreateDateMonthYr = TmpSalesTbl.CreateDateMonthYr ORDER BY tmp_date_array.CreateDateYear, tmp_date_array.CreateDateMonth
I have two tables that I need to combine, but I can't get to seem the joins to work. The picture has three tables
_date_array2: Has a field DateMonthYr that contains all possible date/yr combinations
_sales_summ_tbl__ => Has 5 fields. Only months with sales show up. For example, you see only three records showing up for the second table. There is no 5/2016, for example, because there were no sales for that month.
My goal is to "pad" the second table to have TotalDemand of 0's for months with no sales. I am very close (see the third table), except I cannot get the PartNumber to show up for dates with no sales.
My guess is that it's due to the RIGHT JOIN. But I'm not sure how to handle this. The output I am hoping for is table 3 but with the part number populated for all entries.
And here is my code (the results from running this code are the third/last table in the picture):
SELECT TmpSalesTbl.PartNumber as PartNumber, tmp_date_array.CreateDateMonth as CreateDateMonth, tmp_date_array.CreateDateYear as CreateDateYear, CASE WHEN TmpSalesTbl.TotalDemand is NULL THEN 0 ELSE TmpSalesTbl.TotalDemand END as TotalDemand FROM #_sales_summ_tbl__ TmpSalesTbl RIGHT JOIN #_date_array2 tmp_date_array on tmp_date_array.CreateDateMonthYr = TmpSalesTbl.CreateDateMonthYr ORDER BY tmp_date_array.CreateDateYear, tmp_date_array.CreateDateMonth
原文:https://stackoverflow.com/questions/47564078
最满意答案
ASE Job Scheduler将是一种运行清除的更稳定的方法,而不是waitfor 。
不建议生产系统使用truncate_only *转储事务的原因是它对可恢复性的影响。 您可以通过将事务日志作为进程的一部分转储到文件来解决此问题。 这样您就可以处理日志填充问题,并在数据库发生故障时保持恢复系统的能力。
有关转储事务日志的更多信息,请参见“ Sybase ASE 15.7参考手册:命令”
此外,可以将sp_thresholdaction设置为在达到特定阈值时自动转储事务日志。 “ Sybase ASE 15.7参考手册:过程”就是这个例子。
ASE Job Scheduler would be a more stable method of running your purge, instead of waitfor.
The reason *dump transaction with truncate_only* is not recommended for production systems is it's impact on recoverability. You can work around this issue by dumping the transaction logs to file as part of your process. That way you can deal with both the log filling issue, as well as maintaining the ability to recover your system in case of database failure.
More information on dumping the transaction log can be found in the Sybase ASE 15.7 Reference Manual: Commands
Also, sp_thresholdaction can be setup to automatically dump the transaction log when it reaches a certain threshold. The Sybase ASE 15.7 Reference Manual: Procedures has that example.
相关问答
更多-
将Sybase ASE 12.5.4与带有JRuby的jTDS驱动程序一起使用(Using Sybase ASE 12.5.4 with jTDS drivers with JRuby)[2022-07-01]
我一直在使用Sybase驱动程序,正如@lollipopman所建议的那样,这有助于开始,但是当我构建更复杂的查询时,我一直遇到问题所以我试图重新审视原始问题,并且花了一个小时左右我开始工作。 在这里找到开源的jTDS驱动程序 require java require jtds-1.2.5.jar require rubygems require dbi dbh = DBI.connect('dbi:Jdbc:jtds:sybase://: / ', , < ... -
好的,我在这里得到了答案。 首先,你需要在我认为你做的任何地方安装SQL。 然后: "SQL anywhere install folder\Bin32\dbtran.exe" logName.log logName.log.txt 最终结果如下: --CONNECT-1045-0016035758-DBA-2011-11-04 01:50 --BEGIN TRANSACTION-1045-0016035769 BEGIN TRANSACTION go --UPDATE-1045-0016036065 U ...
-
Sybase ASE数据分组(Sybase ASE data grouping)[2022-07-20]
尝试: SELECT t1.* FROM Table1 t1 WHERE t1.lastModificationDate = (SELECT MAX(t2.lastModificationDate) FROM Table1 t2 WHERE t2.effectiveDate = t1.effectiveDate ... -
您可以使用此工具: SqlDbx 这很棒。 You can use this tool: SqlDbx It's great.
-
没有足够的过程高速缓存来运行此过程,Sybase ASE,C#(There is not enough Procedure cache to run this procedure, Sybase ASE, C#)[2022-01-18]
100 MB的过程缓存非常小。 特别是如果你有一个程序,你知道将超过50 MB。 这是整个数据库实例的总过程高速缓存,而不是每个会话或每个过程。 我建议你让你的程序缓存更大。 100 MB of procedure cache is pretty small. Especially if you have a single procedure taht you know will take over 50 MB. That is the total procedure cache for the whole ... -
在触发器中执行的操作被视为修改父表的同一事务的一部分(触发器定义在其上)。 如果您将记录插入测试表,并且测试表的insert触发器执行对test_copy表的插入,那么就无法将两组插入分开,即它们被视为单个工作单元: 提交插入测试并将插入提交到test_copy 回滚imserts进行测试,然后将插入回滚到test_copy 如果要将两组插入(1组设置为test; 1设置为test_copy)视为单独的工作单元,则需要将test_copy的插入作为单独的独立插入语句执行(例如,存储过程将必须insert t ...
-
添加要返回的TOP numberofrecords DECLARE @intStartRow int; DECLARE @intEndRow int; SET @intStartRow = (@intPage -1) * @intPageSize + 1; SET @intEndRow = @intPage * @intPageSize; WITH wl_eval AS (SELECT field, ROW_NUMBER() OVER(ORDER BY intID DESC) as int ...
-
Sybase ASE数据清除批处理 - 等待和事务日志管理(Sybase ASE data purge batch - waitfor and transaction log management)[2023-12-09]
ASE Job Scheduler将是一种运行清除的更稳定的方法,而不是waitfor 。 不建议生产系统使用truncate_only *转储事务的原因是它对可恢复性的影响。 您可以通过将事务日志作为进程的一部分转储到文件来解决此问题。 这样您就可以处理日志填充问题,并在数据库发生故障时保持恢复系统的能力。 有关转储事务日志的更多信息,请参见“ Sybase ASE 15.7参考手册:命令” 此外,可以将sp_thresholdaction设置为在达到特定阈值时自动转储事务日志。 “ Sybase ASE ... -
有些要点现在是空白的,我会在有机会的时候填写它们。 1 - 查看有关在Sybase ASE中复制此功能的帖子 。 2 - 我在dba堆栈上的帖子涵盖了确定何时运行reorg的许多关键点 3 - 由于更新统计数据可以比重组(也更新统计数据)更快地完成,因此它有时用于帮助提高重组之间的性能。 决定何时运行它们取决于清洗时性能下降的速度。 sp_sysmon是一个有价值的工具,可以捕获指标以帮助您做出决定。 4 - 分区表不应该真正影响您的清除。 这是另一种情况,它可以提高删除的性能,因为可以比其他配置更快地访问 ...
-
我重新安装了我的ASE12.5驱动程序,并使用以下字符串工作。 conexion = "Driver={SYBASE ASE ODBC Driver};NA=HOSTNAME,PORTNUMBER;Uid=USER;Pwd=PASSWORD;" 谢谢! I re-installed my ASE12.5 drivers and worked using the following string. conexion = "Driver={SYBASE ASE ODBC Driver};NA=HOSTNAME, ...