首页 \ 问答 \ SQL - 总结每月销售数据(SQL - Summarizing Monthly Sales Data)

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.

enter image description here

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
更新时间:2024-01-30 22:01

最满意答案

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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。