2

我正在从保存的视图中从 SQL Server 导出到 Excel。在该视图中,它按两列排序(按..排序)

但是当我导出它时,它在 Excel 中是随机排序的。

如何使用订单导出?

这是视图的查询:

SELECT     TOP (100) PERCENT GETDATE() AS Date, MixStatusID AS Partner, SUM(CAST(Weight AS 
float)) AS Weight, SUM(CAST(TotalCostPrice AS float)) AS TotalCost, ProductGroupID
FROM         dbo.D_ParcelLocation
WHERE     (ParcelStatusID IN (N'stock', N'memo', N'transfer', N'analyze_un')) AND (ParcelTypeID <> N'M') AND (ProductGroupID <> N'G_POL') AND (ProductGroupID <> N'G_ROU')
GROUP BY MixStatusID, ProductGroupID
HAVING      (SUM(CAST(Weight AS float)) > 0)
ORDER BY ProductGroupID, Partner
4

2 回答 2

0

正如@Pondlife 正确指出的那样,如果该查询实际上是视图定义,则需要ORDER BY 在从该视图中选择时指定一个,即如下所示:

SELECT *
FROM YourView
ORDER BY ProductGroupID, Partner
;

或您用于实际传输数据的任何查询。

ORDER BY 视图定义中的没有为您提供结果集的固定顺序。事实上,它只允许在那里,因为有一个TOP子句,在这种情况下,正式的1含义是为该子句ORDER BY设置正确的行顺序,而不是为将从该视图读取的查询结果设置.TOP


1对于使用+技术的实际含义,您可以查看此线程:为什么使用 Select Top 100 Percent?.SELECT TOP 100 PERCENTORDER BY

于 2013-05-31T09:01:17.707 回答
0

抱歉,您不能只创建一个具有相同结果集的存储过程吗?

create procedure sp_D_ParcelLocation_s as
begin 
    SELECT     TOP (100) PERCENT GETDATE() AS Date
                , MixStatusID AS Partner
                , SUM(CAST(Weight AS float)) AS Weight
                , SUM(CAST(TotalCostPrice AS float)) AS TotalCost
                , ProductGroupID
    FROM        dbo.D_ParcelLocation
    WHERE       (ParcelStatusID IN (N'stock', N'memo', N'transfer', N'analyze_un')) 
    AND         (ParcelTypeID <> N'M') 
    AND         (ProductGroupID <> N'G_POL') 
    AND         (ProductGroupID <> N'G_ROU')
    GROUP BY    MixStatusID, ProductGroupID
    HAVING      (SUM(CAST(Weight AS float)) > 0)
    ORDER BY    ProductGroupID, Partner
end

也许那行不通?我不经常像这样使用 Excel,但我认为您可以使用存储过程和视图。

于 2013-05-17T18:49:44.060 回答