2016-10-20 2 views
0

Я набор запроса, как показано ниже, но результат не нравится, я хочуКак манипулировать SQL Data

SELECT * INTO #Temp FROM (SELECT Product,PeriodNum,OpenBal,Month Qty,0 as OnHandQty 
         FROM StockTransaction 
         WHERE Company = 1 AND 
          FiscalYear = 2016 AND 
          DeptCode = 'phst' and 
          Product = 'Product A' and 
          PeriodNum between 1 and 10) A 

SELECT * INTO #Temp2 FROM (SELECT Product,PeriodNum,OpenBal,Month Qty,OnHandQty 
         FROM StockTransaction 
         WHERE Company = 1 AND 
          FiscalYear = 2016 AND 
          DeptCode = 'phst' and 
          Product = 'Product A' and 
          PeriodNum between 1 and 10 and 
          PeriodNum = (SELECT max(PeriodNum) 
             from StockTransaction s 
             WHERE s.DeptCode = StockTransaction.DeptCode and 
               s.ItemCode = StockTransaction.ItemCode)) C 

SELECT * INTO #Temp3 FROM (C.Product,C.PeriodNum,C.OpenBal,C.Month Qty,C.OnHandQty        
         FROM #Temp2 C INNER JOIN 
          #Temp T ON C.DeptCode = T.DeptCode and 
             C.ItemCode = T.ItemCode and 
             C.PeriodNum = T.PeriodNum) as E 

SELECT * INTO #Result FROM (SELECT * FROM #Temp 
         UNION ALL SELECT * FROM #Temp3) As result 

SELECT * FROM #Result 

enter image description here

вы можете помочь мне, как манипулировать данными я получаю ??? я только данные дисплей как изображение. Если есть 5 строк данных из отборного даного, то значения OpenBal отображение только для минимального PeriodNum и значение OnHandQty отображения только максимум PeriodNum

+0

использованием выберите ** топ 3 ** продукта, PeriodNum, OpenBal, месяц ......... –

+0

почему выбор топ 3 @Denny Sutedja? Я не понимаю, вы можете объяснить больше? –

ответ

0

Я не вижу интуитивный способ сделать это на моей голове.

Итак, если вы хотите получить данные для каждого из продуктов, это может произойти следующим образом.

SELECT * INTO #Temp FROM (SELECT Product,PeriodNum,0 as OpenBal,Month Qty,OnHandQty 
         FROM StockTransaction 
         WHERE Company = 1 AND 
          FiscalYear = 2016 AND 
          DeptCode = 'phst' and 
          Product = 'Product A' and 
          PeriodNum between 1 and 10 and 
          (Product, PeriodNum) IN (SELECT Product, max(PeriodNum) 
             from StockTransaction s group by Product) 

SELECT * INTO #Temp2 FROM (SELECT Product,PeriodNum,OpenBal,Month Qty,0 as OnHandQty 
         FROM StockTransaction 
         WHERE Company = 1 AND 
          FiscalYear = 2016 AND 
          DeptCode = 'phst' and 
          Product = 'Product A' and 
          PeriodNum between 1 and 10 and 
          (Product, PeriodNum) IN (SELECT Product, min(PeriodNum) 
             from StockTransaction s group by Product) 

SELECT * INTO #Temp3 FROM (SELECT Product,PeriodNum,0 as OpenBal,Month Qty,0 as OnHandQty 
         FROM StockTransaction 
         WHERE Company = 1 AND 
          FiscalYear = 2016 AND 
          DeptCode = 'phst' and 
          Product = 'Product A' and 
          PeriodNum between 1 and 10 and 
          (Product, PeriodNum) NOT IN (SELECT Product, PeriodNum 
             from #temp1 UNION SELECT Product, PeriodNum from #temp2) 

SELECT * INTO #Result FROM (SELECT * FROM #Temp 
         UNION SELECT * FROM #Temp2 UNION SELECT * FROM Temp3) As result 

SELECT * FROM #Result