Guest User

Union_Query

a guest
Mar 20th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.91 KB | None | 0 0
  1. insert overwrite TABLE test.test_tez_multiunion
  2. SELECT   aver,
  3.          asub ,
  4.          from_unixtime(unix_timestamp(snapshot_date_time, 'yyyy-MM-dd')) AS snapshot_date_time ,
  5.          count (DISTINCT l_id)                                     AS a_lists , (
  6.          CASE
  7.                   WHEN (
  8.                                     psla>=0
  9.                            AND      fsp>0
  10.                            AND      lis='1') THEN count (DISTINCT l_id)
  11.                   ELSE 0
  12.          END)                     AS active_instock_listings ,
  13.          count (DISTINCT iid) AS a_styles , (
  14.          CASE
  15.                   WHEN (
  16.                                     psla>=0
  17.                            AND      fsp>0
  18.                            AND      lis='1') THEN count (DISTINCT iid)
  19.                   ELSE 0
  20.          END)                   AS a_i_styles  
  21. FROM     (
  22.                 SELECT
  23.                        l_id,
  24.                        psla,
  25.                        fsp,
  26.                        lis,
  27.                        aver,
  28.                        asub ,
  29.                        iid,
  30.                        date_sub(to_date(from_unixtime(unix_timestamp())),120) AS snapshot_date_time  
  31.                 FROM   test.test_tez_input
  32.                 WHERE  to_date(start_date_time)<= date_sub(to_date(from_unixtime(unix_timestamp())),120)
  33.                 AND    (
  34.                               to_date(end_date_time)> date_sub(to_date(from_unixtime(unix_timestamp())),120)
  35.                        OR     end_date_time IS NULL)
  36.                 UNION ALL
  37.                 SELECT
  38.                        l_id,
  39.                        psla,
  40.                        fsp,
  41.                        lis,
  42.                        aver,
  43.                        asub ,                      
  44.                        iid,
  45.                        date_sub(to_date(from_unixtime(unix_timestamp())),120) AS snapshot_date_time  
  46.                 FROM   test.test_tez_input
  47.                 WHERE  to_date(start_date_time)<= date_sub(to_date(from_unixtime(unix_timestamp())),120)
  48.                 AND    (
  49.                               to_date(end_date_time)> date_sub(to_date(from_unixtime(unix_timestamp())),120)
  50.                        OR     end_date_time IS NULL)
  51.                 UNION ALL
  52.                               SELECT
  53.                        l_id,
  54.                        psla,
  55.                        fsp,
  56.                        lis,
  57.                        aver,
  58.                        asub ,                    
  59.                        iid,
  60.                        date_sub(to_date(from_unixtime(unix_timestamp())),120) AS snapshot_date_time  
  61.                 FROM   test.test_tez_input
  62.                 WHERE  to_date(start_date_time)<= date_sub(to_date(from_unixtime(unix_timestamp())),120)
  63.                 AND    (
  64.                               to_date(end_date_time)> date_sub(to_date(from_unixtime(unix_timestamp())),120)
  65.                        OR     end_date_time IS NULL)
  66.                 UNION ALL
  67.                                SELECT
  68.                        l_id,
  69.                        psla,
  70.                        fsp,
  71.                        lis,
  72.                        aver,
  73.                        asub ,                    
  74.                        iid,
  75.                        date_sub(to_date(from_unixtime(unix_timestamp())),120) AS snapshot_date_time  
  76.                 FROM   test.test_tez_input
  77.                 WHERE  to_date(start_date_time)<= date_sub(to_date(from_unixtime(unix_timestamp())),120)
  78.                 AND    (
  79.                               to_date(end_date_time)> date_sub(to_date(from_unixtime(unix_timestamp())),120)
  80.                        OR     end_date_time IS NULL)
  81.                         ) listing_snapshot
  82. GROUP BY aver,
  83.          asub,
  84.          snapshot_date_time,
  85.          lis,
  86.          psla,
  87.          fsp;
Add Comment
Please, Sign In to add comment