使用 Excel 中的单元格引用的动态 SQL 查询
Dynamic SQL query with cell reference in Excel
我有一个连接到数据透视表 table 的查询,我希望它根据单元格中的日期显示结果(这样用户可以在不编辑查询的情况下更改它)。我读过一种方法是创建与单元格的连接
并向下钻取该查询,以便其数据可以被另一个查询引用
我读过我必须通过
引用连接 table(只是一个日期)
SQL1 = Text.Combine({SQL,Date}),
SQL2 = Text.Combine({SQL1, "‘" }),
Source = Sql.Database("servername", "db", [Query= SQL2])
所以我认为它应该是这样的:
let
SQL =
"select trim(codart) AS REF,
descart AS 'DESCRIPTION',
codalm AS WAREHOUSE,
descalm AS WAREHOUSEDESCRIP,
unidades AS UNITS,
entran AS 'IN',
salen AS 'OUT',
m.entran*1 + m.salen*-1 as MOVEMENT,
(select sum(m1.entran*1 + m1.salen*-1)
from MOVSTOCKS m1
where m1.codart = m.codart and m1.codalm =
m.codalm and m.fecdoc >= m1.fecdoc) as 'CUMULATIVE',
PRCMEDIO as 'VALUE',
FECDOC as 'DATE',
REFERENCIA as 'REF',
tipdoc as 'DOCUMENT'
from (select m.*,
sum(m.entran*1 - m.salen*-1) over (partition by m.codart, m.codalm order by fecdoc) as cumulative,
max(fecdoc) over (partition by m.codart, m.codalm) as max_fecdoc
from MOVSTOCKS m
where fecdoc <= ‘ ), m
where fecdoc = max_fecdoc
and (m.entran <> 0 or m.salen <> 0)
order by fecdoc",
SQL1 = Text.Combine({SQL,Date}),
SQL2 = Text.Combine({SQL1, "‘" }),
Source = Sql.Database("servername", "db", [Query= SQL2])
in
Source
问题是...如果该动态单元格的格式为日期,则电源查询会显示此错误:
Expression.Error: We cannot convert the value #date (2020, 9, 30) to type Text.
Details:
Value= 30/09/2020
Type=Type
如果我将日期格式化为文本,则会显示此错误(更改隐私选项后):
DataSource.Error: Microsoft SQL: Incorrect syntax near '‘'.
Details:
DataSourceKind=SQL
DataSourcePath=servername;db
Message=Incorrect syntax near '‘'.
Number=102
Class=15
我想SQL读这个
where fecdoc <= ‘ ), m
作为
where fecdoc <= 30/09/2020 ), m
请记住,日期来自 excel 单元格,用户可以更改该单元格。
我是 SQL 和 M 的新手,我什至不知道我的尝试是否有意义。
任何反馈都会有所帮助。非常感谢
而不是你试图用 SQL1
和 SQL1
做的任何事情,在你的查询中,替换
where fecdoc <= ‘ ), m
和
where fecdoc <= '" & Date & "' ), m
其中 Date
是以字符串格式引用您的日期。
我有一个连接到数据透视表 table 的查询,我希望它根据单元格中的日期显示结果(这样用户可以在不编辑查询的情况下更改它)。我读过一种方法是创建与单元格的连接
并向下钻取该查询,以便其数据可以被另一个查询引用
我读过我必须通过
引用连接 table(只是一个日期) SQL1 = Text.Combine({SQL,Date}),
SQL2 = Text.Combine({SQL1, "‘" }),
Source = Sql.Database("servername", "db", [Query= SQL2])
所以我认为它应该是这样的:
let
SQL =
"select trim(codart) AS REF,
descart AS 'DESCRIPTION',
codalm AS WAREHOUSE,
descalm AS WAREHOUSEDESCRIP,
unidades AS UNITS,
entran AS 'IN',
salen AS 'OUT',
m.entran*1 + m.salen*-1 as MOVEMENT,
(select sum(m1.entran*1 + m1.salen*-1)
from MOVSTOCKS m1
where m1.codart = m.codart and m1.codalm =
m.codalm and m.fecdoc >= m1.fecdoc) as 'CUMULATIVE',
PRCMEDIO as 'VALUE',
FECDOC as 'DATE',
REFERENCIA as 'REF',
tipdoc as 'DOCUMENT'
from (select m.*,
sum(m.entran*1 - m.salen*-1) over (partition by m.codart, m.codalm order by fecdoc) as cumulative,
max(fecdoc) over (partition by m.codart, m.codalm) as max_fecdoc
from MOVSTOCKS m
where fecdoc <= ‘ ), m
where fecdoc = max_fecdoc
and (m.entran <> 0 or m.salen <> 0)
order by fecdoc",
SQL1 = Text.Combine({SQL,Date}),
SQL2 = Text.Combine({SQL1, "‘" }),
Source = Sql.Database("servername", "db", [Query= SQL2])
in
Source
问题是...如果该动态单元格的格式为日期,则电源查询会显示此错误:
Expression.Error: We cannot convert the value #date (2020, 9, 30) to type Text.
Details:
Value= 30/09/2020
Type=Type
如果我将日期格式化为文本,则会显示此错误(更改隐私选项后):
DataSource.Error: Microsoft SQL: Incorrect syntax near '‘'.
Details:
DataSourceKind=SQL
DataSourcePath=servername;db
Message=Incorrect syntax near '‘'.
Number=102
Class=15
我想SQL读这个
where fecdoc <= ‘ ), m
作为
where fecdoc <= 30/09/2020 ), m
请记住,日期来自 excel 单元格,用户可以更改该单元格。
我是 SQL 和 M 的新手,我什至不知道我的尝试是否有意义。
任何反馈都会有所帮助。非常感谢
而不是你试图用 SQL1
和 SQL1
做的任何事情,在你的查询中,替换
where fecdoc <= ‘ ), m
和
where fecdoc <= '" & Date & "' ), m
其中 Date
是以字符串格式引用您的日期。