sql-server - 用户为访问 SQL 代码选择变量
问题描述
创建一个查询,分析整个工厂需要生产的成品预测。每月需求被拉入,但试图弄清楚如何轻松选择要查看的月份,因为它们是单独的列。
下面的 SQL 是我目前拥有的,我想从用户输入中对变量进行 DIM 处理,以选择加粗的数字(月份)。前任。用户输入“4”,它变为[MPS 客户预测]![4]。提前感谢您的帮助。
SELECT [MPS Customer Forecast]![Part] AS [Finished Good]
, [MPSCustomer Forecast].Part
, ***[MPS Customer Forecast]![3]*** AS [MonthDemand]
, BOMs.[Component No]
, BOMs.[Component Name]
, BOMs.[BOMQuantity]
, ([BOMs]![BOM Quantity][MPS Customer Forecast]![3]***) AS [Comp Month Dem]
, [Active Routings - Primary].[Approved WorkcenterRates]
,[MPS Customer Forecast]![3][BOMs]![BOM Quantity]/[Active
Routings - Primary]![Approved Workcenter Rates] AS [Needed Hours]
,[Active Routings - Primary].[Workcenter Code]
解决方案
这是不可能的。您不能将参数传递给查询来决定要返回的列。
如果您无法访问用于计算 12 个月值的基础记录,我看到有两个机会可以实现您想要的:
1.使用UNION查询
不是查询表[MPS Customer Forecast]
,而是查询定义为 UNION 查询 ( [MPS Customer Forecast UNION]
) 的查询,如下所示(如果需要,添加更多字段):
SELECT 1 AS [Month], [1] AS [MonthDemand], [Part] FROM [MPS Customer Forecast]
UNION ALL
SELECT 2 AS [Month], [2] AS [MonthDemand], [Part] FROM [MPS Customer Forecast]
UNION ALL
...
UNION ALL
SELECT 12 AS [Month], [12] AS [MonthDemand], [Part] FROM [MPS Customer Forecast]
主查询可能如下所示(您忘记了MonthDemand
and之间的算术运算符BOMQuantity
,我也是):
PARAMETERS MonthParameter Short;
SELECT [MPS Customer Forecast UNION]![Part] AS [Finished Good]
, [MPS Customer Forecast UNION].Part
, [MPS Customer Forecast UNION].[MonthDemand]
, BOMs.[Component No]
, BOMs.[Component Name]
, BOMs.[BOMQuantity]
, ([BOMs]![BOM Quantity][MPS Customer Forecast UNION]![MonthDemand]) AS [Comp Month Dem]
, [Active Routings - Primary].[Approved WorkcenterRates]
,[MPS Customer Forecast UNION].[MonthDemand][BOMs]![BOM Quantity]/[Active
Routings - Primary]![Approved Workcenter Rates] AS [Needed Hours]
,[Active Routings - Primary].[Workcenter Code]
...
WHERE ((([MPS Customer Forecast UNION].[Month]) = [MonthParameter]))
2. 使用Choose
函数选择 12 个值之一:
不要访问与月份相关的列,而是根据查询的参数选择值:
PARAMETERS MonthParameter Short;
...
Choose([MonthParameter], [MPS Customer Forecast]![1], [MPS Customer Forecast]![2], [MPS Customer Forecast]![3], [MPS Customer Forecast]![4], [MPS Customer Forecast]![5], [MPS Customer Forecast]![6], [MPS Customer Forecast]![7], [MPS Customer Forecast]![8], [MPS Customer Forecast]![9], [MPS Customer Forecast]![10], [MPS Customer Forecast]![11], [MPS Customer Forecast]![12])
...
推荐阅读
- python - 将系列对象转换为 json 并将其写入本地驱动器
- java - 当 Selenium 驱动程序在 Java 中通过 VPN 运行时出现 W3CHttpResponseCodec 错误
- python - 具有分层折叠的嵌套交叉验证
- laravel - 根据 Laravel 7 中的下拉值重新绘制视图
- visual-studio - 如何在 Visual Studio 安装程序 2015 中使用 now 安装程序覆盖服务器
- ios - 如何为动态键、json解析、swift5创建模型
- python - 在数据框中绘制数据时需要帮助
- excel - Excel 无法访问 ssas 多维数据集
- java - 用 spark 实现散列
- oauth-2.0 - 没有发现文档的 angular-oidc 库代码流