Domanda

Sto utilizzando MS SQL 2005 e ho creato una query CTE per restituire valori dagli ultimi due record. Quindi lo uso per trovare il delta di due figure restituite. Ho una specie di query funzionante ma Ho problemi a ottenere qualcosa di diverso dalla figura delta.

ecco la mia domanda:

;with data as(
    SELECT 
        NetObjectID,
        RawStatus,
        RowID,
        rn 
    from(   
        SELECT 
            CustomPollerAssignmentID AS NetObjectID,
            RawStatus,
            RowID,
            row_number() over(order by DateTime desc)as rn 
        FROM CustomPollerStatistics_Detail 
        WHERE
            (CustomPollerAssignmentID='a87f531d-4842-4bb3-9d68-7fd118004356')
    ) x where rn<=2
)
SELECT 
    case when 
        max(case rn when 1 then RawStatus end) > max(case rn when 2 then RawStatus end) 
    then 
        max(case rn when 1 then RawStatus end) - max(case rn when 2 then RawStatus end) 
    else 
        max(case rn when 2 then RawStatus end) - max(case rn when 1 then RawStatus end) 
    end as Delta
from data having 
(SELECT 
    case when 
        max(case rn when 1 then RawStatus end) > max(case rn when 2 then RawStatus end) 
    then 
        max(case rn when 1 then RawStatus end) - max(case rn when 2 then RawStatus end) 
    else 
        max(case rn when 2 then RawStatus end) - max(case rn when 1 then RawStatus end) 
    end
from data) >= 1

Quello che sto cercando è ottenere Delta & amp; NetObjectID restituito. Ogni volta che provo, ricevo errori. data.NetObjectID non è valido nell'elenco di selezione perché non è contenuto né in una funzione aggregata né nella clausola group by.

Se provo ad aggiungere un gruppo per ecc. alla fine della query ricevo un ulteriore errore lamentandomi della parola "gruppo".

Sono relativamente nuovo a SQL e sto raccogliendo le cose mentre vado. Qualsiasi aiuto sarebbe ricevuto con gratitudine.

È stato utile?

Soluzione

vedi se qualcosa del genere funzionerà.

;with data as
(    
    SELECT         
        NetObjectID,        
        RawStatus,        
        RowID,        
        rn     
    from
    (               
        SELECT                 
            CustomPollerAssignmentID AS NetObjectID,                
            RawStatus,                
            RowID,                
            row_number() over(order by DateTime desc)as rn         
        FROM CustomPollerStatistics_Detail         
        WHERE                
        (
            CustomPollerAssignmentID='a87f531d-4842-4bb3-9d68-7fd118004356'
        )    
    ) x 
    where rn<=2
)
select
    NetObjectID,
    max(RawStatus)-min(RawStatus) as Delta
from data
group by NetObjectID

Altri suggerimenti

Siamo spiacenti, sono molto nuovo qui e non sono sicuro delle query CTE, tuttavia sembra che dopo aver definito i dati, selezioni il caso ... come Delta DA .... Significa che hai solo Delta nel tuo selezionare dichiarazione. Ancora una volta, scusami se sono lontano dalla base.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top