Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 0.86 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Merge data from multiple source tables into a single destination table
  2. INSERT INTO Destination
  3.     (Col1, Col2, Col3, Col4, Col5)
  4.     SELECT s.Col1,
  5.            s.type,
  6.            /* The next two columns illustrate choosing a  */
  7.            /* subset from either Type_A or Type_B         */
  8.            COALESCE(a.Col3, b.Col3),
  9.            COALESCE(a.Col4, b.Col4),
  10.            /* The next column illustrates choosing a      */
  11.            /* static value based on Source.type           */
  12.            CASE WHEN s.type = 0 THEN 'Static Value 1'
  13.                 WHEN s.type = 1 THEN 'Static Value 2'
  14.                 ELSE NULL
  15.            END /* CASE */
  16.         FROM Source s
  17.             LEFT JOIN Type_A a
  18.                 ON s.Col1 = a.Col1
  19.                     AND s.type = 0
  20.             LEFT JOIN Type_B b
  21.                 ON s.Col1 = b.Col1
  22.                     AND s.type = 1