Share via

How can I make my query more efficient and order result

Krlos Stone 1 Reputation point
2022-01-29T18:30:18.263+00:00

Hello (:

I would like to know how I can order my queries because I cannot use "ORDER BY" and instead the "GROUP BY" fulfills its function but without order.

also

When of the 3 queries it is greater than 2000 rows, it takes a long time to do the "FULL JOIN" with respect to whether table1.time = table2.time.

ALTER PROCEDURE [dbo].[Datos_1] @linea int, @d1 varchar(30), @d2 varchar(30)
AS
WITH 

s as (
  select Datepart(HOUR, created_at) as hora,  
  CAST (isnull(avg(entrada_1),0)AS DECIMAL(10,2)) as 'entrada' 
  from entrada_temp where created_at between @d1 and @d2 and id_linea = @linea 
  group by ROLLUP(  datepart(HOUR, created_at))
),      
e as (
  select Datepart(HOUR, created_at) as hora, 
  CAST(isnull(avg(esmalte_1),0) AS DECIMAL(10,2))as 'esmalte' 
  from esmalte_temp where created_at between @d1 and @d2 and id_linea = @linea 
  group by ROLLUP ( datepart(HOUR, created_at))
),      
f as (
  select Datepart(HOUR, created_at) as hora, 
  CAST(isnull(avg(salida_1),0) AS DECIMAL(10,2)) as 'salida' 
  from salida_temp where created_at between @d1 and @d2 and id_linea = @linea 
  group by ROLLUP ( datepart(hour, created_at))
)

SELECT top 9
  COALESCE(s.hora,e.hora, f.hora) as hora,
 isnull(s.entrada,0) as 'entrada', 
isnull(e.esmalte,0) as 'esmalte',
isnull(f.salida,0) as 'salida'
FROM
  s
FULL JOIN e ON (e.hora = s.hora)
FULL JOIN f ON (f.hora = s.hora)
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


1 answer

Sort by: Most helpful
  1. YufeiShao-msft 7,156 Reputation points
    2022-01-31T05:54:21.19+00:00

    Hi @Krlos Stone ,

    I would like to know how I can order my queries because I cannot use "ORDER BY" and instead the "GROUP BY" fulfills its function but without order.

    Ordering is only guaranteed if you use ORDER BY
    Maybe this thread can help you, you can order by on a case, or use a table variable or temporary table with an identity column, feed in your values and join to that

    When of the 3 queries it is greater than 2000 rows, it takes a long time to do the "FULL JOIN" with respect to whether table1.time = table2.time.

    Consider indexing first, check out this thread, or create to tiny tables and join them together instead, the db engine has no choice but to scan each table looking for the joins

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.