group by的问题!
SELECT CustomerID, ItemNumber, ShippedQuantity
FROM dbo.FS_HistoryShipment
WHERE (OrderType = 'C') AND (IssueType = 'I') AND (UserID = 'C14') AND (Stockroom = 'FG') AND (CONVERT(NVARCHAR(10), TransactionDate, 120)
= CONVERT(NVARCHAR(10), GETDATE() - 1, 120))
SELECT CustomerID, ItemNumber, sum(ShippedQuantity) as qty
FROM dbo.FS_HistoryShipment
WHERE (OrderType = 'C') AND (IssueType = 'I') AND (UserID = 'C14') AND (Stockroom = 'FG') AND (CONVERT(NVARCHAR(10), TransactionDate, 120)
= CONVERT(NVARCHAR(10), GETDATE() - 1, 120))
GROUP BY CustomerID, ItemNumber
聚合时候,ItemNumber和CustomerID一样了。
但是其他的可以;搞不懂!SQL2005 group?by的问题!
[解决办法]
SELECT rtrim(CustomerID), rtrim(ItemNumber), sum(ShippedQuantity) as qty
FROM dbo.FS_HistoryShipment
WHERE (OrderType = 'C') AND (IssueType = 'I') AND (UserID = 'C14') AND (Stockroom = 'FG') AND (CONVERT(NVARCHAR(10), TransactionDate, 120)
= CONVERT(NVARCHAR(10), GETDATE() - 1, 120))
GROUP BY rtrim(CustomerID), rtrim(ItemNumber)
可能前后有空格,你这样试一下
------解决方案--------------------
显然是你自己SELECT 的列填成和CUSTOMERID一样了啊 SQL SERVER不至于这样。
[解决办法]
不至于吧!! SQL Server不可能犯这种错误的。你好好检查你的代码
[解决办法]
是不是你机器上开的东西太多了。我有的时候因为开的东西太多,好像内存不够了,就会出一些莫名其妙的东西,比如一个select不显示结果,只给一句"Command(s) completed successfully.",之类的,一般多关些程序,重启sms就好了
[解决办法]
我似乎记得,群组之前是不是要先排序啊,你order下啊
[解决办法]
如果dbo.FS_HistoryShipment是视图,就有可能会这样了。
将这个视图打开,随便修改一下(如加个空格),再保存。再试上面的语句。
[解决办法]
去除前后空格,试试
SELECT ltrim(rtrim(CustomerID)), ltrim(rtrim(ItemNumber)), sum(ShippedQuantity) as qty
FROM dbo.FS_HistoryShipment
WHERE (OrderType = 'C') AND (IssueType = 'I') AND (UserID = 'C14') AND (Stockroom = 'FG') AND (CONVERT(NVARCHAR(10), TransactionDate, 120)
= CONVERT(NVARCHAR(10), GETDATE() - 1, 120))
GROUP BY ltrim(rtrim(CustomerID)), ltrim(rtrim(ItemNumber))