Question

Hi I'm fairly new to SQL and have been learning by trial and error but I have walked into a wall with this issue. I hope someone could give me an advice.

1) Check if table already exist in database, if it does drops it [works]

IF OBJECT_ID (N'PWC_L6_Daily',N'U') IS NOT NULL
DROP TABLE PWC_L6_Daily

2) Create table with SELECT INTO statement from original table in same database, ORDER BY clause in this statement works without issues DESC or ASC [works]

SELECT Time_Stamp,Plate_Number,OP_Tgt_Wgt_Difference AS Sample_Tgt_Wgt_Difference,Plate_Weight_Target,Plate_Geometry,OP_NOP_Wgt_Difference
INTO PWC_L6_Daily
FROM PWC_L6 
WHERE ((Time_Stamp BETWEEN '05/09/2014 07:00:00' And '05/10/2014 06:59:59') AND (OP_Tgt_Wgt_Difference BETWEEN -6 And 6) AND (NOP_Tgt_Wgt_Difference BETWEEN -6 And 6) AND (Plate_Number <> 0) AND (Plate_Geometry <> 'Error'))
UNION ALL
SELECT Time_Stamp,Plate_Number,NOP_Tgt_Wgt_Difference AS Sample_Tgt_Wgt_Difference,Plate_Weight_Target,Plate_Geometry,OP_NOP_Wgt_Difference
FROM PWC_L6
WHERE ((Time_Stamp BETWEEN '05/09/2014 07:00:00' And '05/10/2014 06:59:59') AND (OP_Tgt_Wgt_Difference BETWEEN -6 And 6) AND (NOP_Tgt_Wgt_Difference BETWEEN -6 And 6) AND (Plate_Number <> 0) AND (Plate_Geometry <> 'Error'))
ORDER BY Time_Stamp ASC

3) Check if table already exist in database, if it does drops it [works]

IF OBJECT_ID (N'PWC_L6_Report',N'U') IS NOT NULL
DROP TABLE PWC_L6_Report

4) Create table with SELECT INTO statement from table previously created with SELECT INTO statement in same database, GROUP BY clause will work but it will give a random order, ORDER BY clause will do nothing. [does not ORDER BY]

SELECT MAX(Time_Stamp) AS Last_Produced,Plate_Number,COUNT(Plate_Number) AS Sample_Count,AVG(Sample_Tgt_Wgt_Difference) AS Avg_StT_Wgt_Difference,AVG(OP_NOP_Wgt_Difference) AS Avg_StS_Wgt_Difference,STDEV(Sample_Tgt_Wgt_Difference) AS Std_Dev_StT_Wgt_Difference
INTO PWC_L6_Report
FROM PWC_L6_Daily
GROUP BY Plate_Number
ORDER BY Last_Produced DESC

5) I did the following for troubleshooting and found the first ORDER BY clause (in 2nd step) affects the PWC_L6_Report table if I change to ASC or DESC but the second ORDER BY clause in this SELECT INTO statement won't have any effect at all. [Temporary statement]

SELECT Time_Stamp
INTO PWC_L6_Report
FROM PWC_L6_Daily
ORDER BY Time_Stamp DESC

I would really appreciate any suggestions. Thank you.

Was it helpful?

Solution

How can you tell if data is in the order you want? The statement:

select *
from table t

Returns the data as an unordered result set. The only way you can impose an ordering is with order by. You do not get data in insert order in SQL Server. This is ANSI-compliant behavior and consistent with how almost all databases work. If you want results in a particular order, then use order by.

order by can be used for an insert with good reason. If you do:

insert into t2(col1 . . .)
    select col1 . . . )
    from t
    order by col1;

Then an identity column will be incremented in the proper order. (This is a nice feature, but probably slows down the insert.) You can get a similar effect using row_number() in your query:

SELECT row_number() over (order by Time_Stamp desc) as id, Time_Stamp
INTO PWC_L6_Report
FROM PWC_L6_Daily;

Now you can order by id instead of time_stamp, if you wanted.

OTHER TIPS

Order by is only for presenting purpose. When used to insert data, it takes no effect. THINK TABLE AS SET.

The difference I see is that in step 2, the variable you are using in the order by statement (Time_Stamp) is an existing column of the table. While in step 4 the variable you are using (Last_Produced) is not an existing column in the table - you are still in the process of creating that column.

In query 4, if you use order by max(Time_Stamp), then that should work I think.

Also, I have noticed that using variable name in order by while it is still being created, works if you are using 'proc sql and create table query' in a SAS environment. It could be something to do with the way SAS/SQL processes/saves data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top