当我运行下面的查询时,它不返回任何数据!有人知道为什么会这样吗?
select
es.SubCategoryName,
es.ClientProductID,
es.ProductSID,
CAST (ps.Date as date) as Date,
Price as Price,
Cost as Cost
from
ProductStatus as ps
join
v_EnterpriseStructure as es on es.ProductSID = ps.ProductSID
join
PriceZone as pz on pz.PriceZoneID = ps.PriceZoneID
where
es.ProductSID = '9577C393-833B-4ACB-9A8E-8B748C65A488'
and ps.Date = '2014-12-17'
group by
es.SubCategoryName,
es.ClientProductID, es.ProductSID,
ps.Date, ps.Price, ps.Cost
我知道,如果您忘记在日期前后加上逗号,则会得到空白的结果。
但是我不确定为什么会在这里发生?
我正在使用SQL Server Management Studio2014。
TIA。
答案 0 :(得分:1)
您应该尝试将日期列强制转换为datetime
语句中的日期。如果将其存储为 SELECT
es.SubCategoryName AS SubCategoryName
,es.ClientProductID AS ClientProductID
,es.ProductSID AS ProductSID
,CAST(ps.DATE AS DATE) AS DATE
,Price AS Price
,Cost AS Cost
FROM
ProductStatus AS ps
INNER JOIN v_EnterpriseStructure AS es ON es.ProductSID = ps.ProductSID
INNER JOIN PriceZone AS pz ON pz.PriceZoneID = ps.PriceZoneID
WHERE
es.ProductSID = '9577C393-833B-4ACB-9A8E-8B748C65A488'
AND CAST(ps.DATE AS DATE) = CAST('2014-12-17' AS date)
GROUP BY es.SubCategoryName
,es.ClientProductID
,es.ProductSID
,ps.DATE
,ps.Price
,ps.Cost;
,则不会匹配。
{{1}}
我会尝试一下。我猜想这与sql将数据存储为的列类型有关。
答案 1 :(得分:0)
之所以会这样,是因为2014-12-17
的值为1985。这是一个整数,而不是日期。
大概没有日期与1985相匹配。SQLServer将尝试将该值转换为日期,例如“ 1905-09-06”。
答案 2 :(得分:0)
您在您的Select语句中添加了CAST (ps.Date as date)
,在where“ date”子句中使用类似的格式可能是明智的选择,但您实际上应该发布一些数据。
答案 3 :(得分:0)
由于您未提供有关表的任何信息,因此我将假定ProductStatus.Date列的数据类型为'datetime'。因此,我相信您的问题出在您的WHERE子句中。尝试更改为此:
WHERE
es.ProductSID = '9577C393-833B-4ACB-9A8E-8B748C65A488'
and year(ps.Date) = '2014' and month(ps.Date) = '12' and day(ps.Date) = '17'
希望这会有所帮助