Domanda

Ho bisogno di aiuto con questo esercizio in cui devo tradurre oggetti di inglese in SQL.

Domande:

  1. lunghezza ha almeno un 0

    Answer: MIN(ABS(length)) = 0
    
  2. lunghezza ha più che un valore

    Answer: MIN(length) < MAX(length)
    
  3. non ci sono nulli per la lunghezza

    Answer: COUNT(*) = COUNT(length)
    
  4. valori di lunghezza sono o positivi o negativi

    Answer: MIN(length) * MAX(length) > 0
    
  5. differisce lunghezza sopra e sotto lo zero della stessa quantità

    Answer: MIN(length) = -max(length)
    
  6. tutti i valori di lunghezza sono diversi

    Answer: COUNT(DISTINCT length) = COUNT(length)
    
  7. solo uno di min (lunghezza) o massima (lunghezza) è positiva

    Answer: MIN(length) * MAX(length) < 0 
    
  8. lunghezza ha un valore o valore nullo

    Answer: MIN(length) = MAX(length)
    

Sono questi corretta? Io non sono sicuro di # 3, 5, 6 e 8 in particolare.

È stato utile?

Soluzione

  1. length has at least one 0

    Answer: MIN(ABS(length)) = 0
    
  2. length has more that one value

    Answer: MIN(length) < MAX(length)
    or
    Answer: COUNT(distinct length) > 1
    
  3. there are no nulls for length

    Answer: COUNT(*) = COUNT(length)
    
  4. values of length are either positive or negative

    Answer: MIN(length) * MAX(length) > 0
    or
    Better Answer: Sign(MIN(length)) * Sign(MAX(length)) > 0
    
    -- Because, with large values of length, multiplying two of them
    -- together can give you an overflow.
    
  5. length differs above and below zero by the same amount

    Answer: MIN(length) = -max(length)
    
  6. all values of length are different

    Answer: COUNT(DISTINCT length) = COUNT(length)
    
  7. only one of min(length) or max(length) is positive

    Maybe Wrong Answer: MIN(length) * MAX(length) < 0 (0 is not positive)
    or
    Answer: MIN(length) <= 0 and MAX(length) > 0
    
    -- IF: one must be positive and the other must be negative
    -- change <= 0 to < 0
    
  8. length has one value or null value

    Wrong Answer: MIN(length) = MAX(length) -- null=null does not return true
    or
    Answer: COUNT(distinct length) <= 1
    

Altri suggerimenti

I would question #2. You may have two identical length values.

For #3, I would not use the HAVING clause to check for non-nulls. Same thing for #4. HAVING is not the right place to be checking equivalency for each row against a scalar value.

#5 might have some problems worth wording. Are you certain they mean min and max and not distribution?

#8 is oddly worded as well. Null could be the value of length, or it could be absence of value. It depends on how the query is formated. Also, is there a requirement that you not use operators like AND & OR?

Also, NULL is treated as "unknown" with most database platforms. Since it is unknown, it cannot be checked for equivalency against another value. If you don't know what Value1 is, you can't possibly know if it's the same as what's in Value2...

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