简体   繁体   中英

Trying to Display Certain Columns and Sum from Table

I have my SQL Query that displays all the needed data, but I need help with getting a certain output. I need to display a column labeled Loop (which isn't in the mass query), LPT, and the SUM of PLAN_MOVES using the query below. I can do a select sum on Plan_Moves and label it Target_Daily_Moves but not sure how to display the other two columns. Please help me as I am still learning SQL.

I'm trying to display this expected outcome.

在此处输入图像描述

New Mass Query SQL W/o " reserved loop name":

    select
da.family,
sysdate update_dttm,
max(dds.sched_update_dttm) max_sched_update_dttm,
dds.lpt,
( case WHEN dds.lpt = '1400' THEN 'MOAT'
WHEN dds.lpt = '3300' THEN 'GATE'
  WHEN dds.lpt = '4800' THEN 'S/D'
    WHEN dds.lpt = '5130' THEN 'PMD'
      WHEN dds.lpt = '5800' THEN 'CONTACT'
        WHEN dds.lpt = '6400' THEN 'VIA 1'
          WHEN dds.lpt = '6900' THEN 'VIA 2'
            WHEN dds.lpt = '7900' THEN 'VIA 3-4'
              WHEN dds.lpt = '9200' THEN 'PO'
                WHEN dds.lpt = '9348' THEN 'PARAMETRICS'
                  WHEN dds.lpt = '9950' THEN 'OUTS'
else 'none'
  end) as LOOPS_INFO,

sum(dds.sched_cur_qty) plan_moves
from device_daily_sched@smsdwde2.itg.ti.com dds, dm_lpt_attributes@Smsdwde2.itg.ti.com la, dm_device_attributes@smsdwde2.itg.ti.com da
where 
dds.facility = 'DP1DM5'
and dds.facility = la.facility
and dds.lpt = la.lpt
and dds.device = da.device
and dds.lpt in('1400','3300','4800','5130','5800','6400','6900','7900','9200','9348','9950')
and (dds.device like 'SN/%'or dds.device like 'SS/%')
and (dds.sched_dttm = trunc(sysdate))
and dds.device not in('SN/BC3ZLTM','SN/DMD8SF','SN/DMD8','SN/DMDASSEM','SN/R035','SN/RFSIGNET','SN/SMIC','SS/BC3ZL','SS/BICOM3ZL','SS/BICOM3','SS/BICOMPBO','SS/R05S','SS/THERM')
group by
da.family,
dds.lpt

Original Mass Query SQL W/o " reserved loop name" OutPut:

在此处输入图像描述

UPDATED: SQL from Last comment to include "loops_info" instead of "loop":

     SELECT a.LOOPS_INFO AS 'LOOPS_INFO', 
          a.lpt AS 'LPT', 
          SUM(a.plan_moves) AS 'Target Daily Moves' 
    FROM (
  select
da.family,
sysdate update_dttm,
max(dds.sched_update_dttm) max_sched_update_dttm,
dds.lpt,
( case WHEN dds.lpt = '1400' THEN 'MOAT'
WHEN dds.lpt = '3300' THEN 'GATE'
  WHEN dds.lpt = '4800' THEN 'S/D'
    WHEN dds.lpt = '5130' THEN 'PMD'
      WHEN dds.lpt = '5800' THEN 'CONTACT'
        WHEN dds.lpt = '6400' THEN 'VIA 1'
          WHEN dds.lpt = '6900' THEN 'VIA 2'
            WHEN dds.lpt = '7900' THEN 'VIA 3-4'
              WHEN dds.lpt = '9200' THEN 'PO'
                WHEN dds.lpt = '9348' THEN 'PARAMETRICS'
                  WHEN dds.lpt = '9950' THEN 'OUTS'
else 'none'
  end) as LOOPS_INFO,

sum(dds.sched_cur_qty) plan_moves
from device_daily_sched@smsdwde2.itg.ti.com dds, dm_lpt_attributes@Smsdwde2.itg.ti.com la, dm_device_attributes@smsdwde2.itg.ti.com da
where 
dds.facility = 'DP1DM5'
and dds.facility = la.facility
and dds.lpt = la.lpt
and dds.device = da.device
and dds.lpt in('1400','3300','4800','5130','5800','6400','6900','7900','9200','9348','9950')
and (dds.device like 'SN/%'or dds.device like 'SS/%')
and (dds.sched_dttm = trunc(sysdate))
and dds.device not in('SN/BC3ZLTM','SN/DMD8SF','SN/DMD8','SN/DMDASSEM','SN/R035','SN/RFSIGNET','SN/SMIC','SS/BC3ZL','SS/BICOM3ZL','SS/BICOM3','SS/BICOMPBO','SS/R05S','SS/THERM')
group by
da.family,
dds.lpt ) AS a 
ORDER BY a.lpt ASC 
GROUP BY a.lpt 
   UNION 
  SELECT 'TOTAL' AS 'LOOPS_INFO', 
         '' AS 'LPT', 
         SUM(b.plan_moves) AS 'Target Daily Moves' 
   FROM (
    select
da.family,
sysdate update_dttm,
max(dds.sched_update_dttm) max_sched_update_dttm,
dds.lpt,
( case WHEN dds.lpt = '1400' THEN 'MOAT'
WHEN dds.lpt = '3300' THEN 'GATE'
  WHEN dds.lpt = '4800' THEN 'S/D'
    WHEN dds.lpt = '5130' THEN 'PMD'
      WHEN dds.lpt = '5800' THEN 'CONTACT'
        WHEN dds.lpt = '6400' THEN 'VIA 1'
          WHEN dds.lpt = '6900' THEN 'VIA 2'
            WHEN dds.lpt = '7900' THEN 'VIA 3-4'
              WHEN dds.lpt = '9200' THEN 'PO'
                WHEN dds.lpt = '9348' THEN 'PARAMETRICS'
                  WHEN dds.lpt = '9950' THEN 'OUTS'
else 'none'
  end) as LOOPS_INFO,

sum(dds.sched_cur_qty) plan_moves
from device_daily_sched@smsdwde2.itg.ti.com dds, dm_lpt_attributes@Smsdwde2.itg.ti.com la, dm_device_attributes@smsdwde2.itg.ti.com da
where 
dds.facility = 'DP1DM5'
and dds.facility = la.facility
and dds.lpt = la.lpt
and dds.device = da.device
and dds.lpt in('1400','3300','4800','5130','5800','6400','6900','7900','9200','9348','9950')
and (dds.device like 'SN/%'or dds.device like 'SS/%')
and (dds.sched_dttm = trunc(sysdate))
and dds.device not in('SN/BC3ZLTM','SN/DMD8SF','SN/DMD8','SN/DMDASSEM','SN/R035','SN/RFSIGNET','SN/SMIC','SS/BC3ZL','SS/BICOM3ZL','SS/BICOM3','SS/BICOMPBO','SS/R05S','SS/THERM')
group by
da.family,
dds.lpt ) AS b 

Same Output Error: ORA-00923: FROM keyword not found where expected在此处输入图像描述

You could nest several if commands together to test the value of lpt and return a string value for each as one of your result set parameters, based on lpt's value, if my understanding of what 'loop' means is correct. I'm assuming each distinct value of lpt determines what loop's value is...so when dds.lpt='1400', for example, Loop would always be the value 'MOAT', correct?

Or...use a case block (which would look cleaner).

see this post for more info on the syntax of both of those: How do I perform an IF...THEN in an SQL SELECT? .

The nested if's or case would be included as another result column, followed by 'AS loop' so the result set includes a column named loop with the corresponding value.

Something like this:

select
da.family,
sysdate update_dttm,
max(dds.sched_update_dttm) max_sched_update_dttm,
dds.lpt,
( case-or-if-testing-dds.lpt-values... ) as loop, 
sum(dds.sched_cur_qty) plan_moves
...etc.

Additional modified answer after first 5 comments were added to original answer:

Then try wrapping your whole query around 2 union'ed separate SELECT statements like this...not pretty but it should give you what you want if you can't do any post-processing in other non-SQL code:

   SELECT a.loop AS "Loop", 
          a.lpt AS "LPT", 
          SUM(a.plan_moves) "Target Daily Moves" 
    FROM (
  <your entire select SQL in here> 
         ) a 
ORDER BY a.lpt ASC 
GROUP BY a.lpt 
   UNION 
  SELECT "TOTAL" AS "Loop", 
         "" AS "LPT", 
         SUM(b.plan_moves) "Target Daily Moves" 
   FROM (
  <your entire select SQL in here> 
         ) b 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM