SELECT * INTO基于特定订单

时间:2015-09-23 06:58:56

标签: sql sql-server

我想询问是否有可能SELECT * INTO特定订单中的新表格?

这是我的SQL语句:

SELECT * INTO TEMP FROM (

SELECT a.Name, b.Balance, -c.AmountOut as amount, c.Date
FROM Item a, Stock b, StockOut c
WHERE a.ID = b.ItemID AND b.ID = c.StockID

UNION

SELECT a.Name, b.Balance, -c.AmountIn as amount, c.Date
FROM Item a, Stock b, StockIn c
WHERE a.ID = b.ItemID AND b.ID = c.StockID

//PLACE 1: ORDER BY DATE STATEMENT

) As Temp //PLACE 2: ORDER BY DATE STATEMENT

基本上我希望在输入数据库时​​以升序日期顺序输入库存中的所有库存,因为我想根据要在图表中绘制的日期来执行运行总计。但似乎两个地方都不正确。

我的脚本如下:

ALTER TABLE TEMP ADD SIndex INT identity(1,1)
ALTER TABLE TEMP ADD RunningTotal INT

SELECT ... , (SELECT SUM(amount) FROM TEMP b WHERE b.SIndex >= a.SIndex AND b.Name = a.Name)
FROM TEMP a

您可以看到日期的顺序很重要,否则运行总计会出错。所以我想知道是否还有确保SELECT * INTO声明可以有一定的顺序?

非常感谢!

3 个答案:

答案 0 :(得分:4)

如果您的标识列的顺序相关,请使用ROW_NUMBER()例如

创建您自己的标识
SELECT  SIndex = ROW_NUMBER() OVER(ORDER BY Date), *
INTO    Temp
FROM    (   
            SELECT a.Name, b.Balance, -c.AmountOut as amount, c.Date
            FROM Item a, Stock b, StockOut c
            WHERE a.ID = b.ItemID AND b.ID = c.StockID

            UNION

            SELECT a.Name, b.Balance, -c.AmountIn as amount, c.Date
            FROM Item a, Stock b, StockIn c
            WHERE a.ID = b.ItemID AND b.ID = c.StockID
        ) AS temp;

或先创建临时表,然后使用INSERT INTO .... SELECT维护您的订单。

有一个IDENTITY()函数,允许您在插入时生成标识列,e.generate

SELECT  SIndex = IDENTITY(), *
INTO    Temp
FROM ....

然而。这样做 NOT 保证身份的顺序与插入的顺序相符。

除了唯一标识行之外,标识列不应该具有任何其他含义,如果它们具有任何含义,则它们未被正确使用。插入新行后,您的“标识”列将不再显示日期顺序。我倾向于不打扰临时表:

WITH Temp AS
(   SELECT  SIndex = ROW_NUMBER() OVER(ORDER BY Date), *
    FROM    (   
                SELECT a.Name, b.Balance, -c.AmountOut as amount, c.Date
                FROM Item a, Stock b, StockOut c
                WHERE a.ID = b.ItemID AND b.ID = c.StockID

                UNION

                SELECT a.Name, b.Balance, -c.AmountIn as amount, c.Date
                FROM Item a, Stock b, StockIn c
                WHERE a.ID = b.ItemID AND b.ID = c.StockID
            ) AS temp
)
SELECT  RunningTotal = (    SELECT SUM(amount) 
                            FROM    TEMP AS b 
                            WHERE   b.SIndex >= a.SIndex 
                            AND     b.Name = a.Name)
FROM    TEMP AS a;

或者,如果您使用的是SQL Server 2012或更高版本,只需:

WITH Temp AS
(   SELECT  SIndex = ROW_NUMBER() OVER(ORDER BY Date), *
    FROM    (   
                SELECT a.Name, b.Balance, -c.AmountOut as amount, c.Date
                FROM Item a, Stock b, StockOut c
                WHERE a.ID = b.ItemID AND b.ID = c.StockID

                UNION

                SELECT a.Name, b.Balance, -c.AmountIn as amount, c.Date
                FROM Item a, Stock b, StockIn c
                WHERE a.ID = b.ItemID AND b.ID = c.StockID
            ) AS temp
)
SELECT  RunningTotal = SUM(amount) OVER(PARTITION BY Name ORDER BY SIndex)
FROM    Temp;

值得注意的是,如果您有两行具有相同的日期和相同的名称,则无法保证如果您运行查询两次它们将以相同的顺序结束,您可能希望考虑添加进一步订购标准。我假设会有重复,否则查询可能只是:

WITH Temp AS
(   
    SELECT a.Name, b.Balance, -c.AmountOut as amount, c.Date
    FROM Item a, Stock b, StockOut c
    WHERE a.ID = b.ItemID AND b.ID = c.StockID

    UNION

    SELECT a.Name, b.Balance, -c.AmountIn as amount, c.Date
    FROM Item a, Stock b, StockIn c
    WHERE a.ID = b.ItemID AND b.ID = c.StockID
)
SELECT  RunningTotal = (    SELECT SUM(amount) 
                            FROM    TEMP AS b 
                            WHERE   b.Date >= a.Date 
                            AND     b.Name = a.Name)
FROM    TEMP AS a;

答案 1 :(得分:0)

希望这有效

创建表格并插入

Create table TEMPv1 
(
SIndex INT identity(1,1),
Name varchar(max),
AmountOut int,
[Date] datetime,
)

insert into TEMPv1 ( Name,AmountOut,[Date])

SELECT a.Name, b.Balance, -c.AmountOut as amount, c.Date
FROM Item a, Stock b, StockOut c
WHERE a.ID = b.ItemID AND b.ID = c.StockID

UNION

SELECT a.Name, b.Balance, -c.AmountIn as amount, c.Date
FROM Item a, Stock b, StockIn c
WHERE a.ID = b.ItemID AND b.ID = c.StockID
ORDER BY c.Date

ALTER TABLE TEMPv1 ADD RunningTotal INT

SELECT ... , (SELECT SUM(amount) FROM TEMP b WHERE b.SIndex >= a.SIndex AND     b.Name = a.Name)
FROM TEMPv1 MP a

答案 2 :(得分:0)

Select into

无法在cmds/screencap/screencap.cpp

中使用

Please Check the Posted enquiry