需要sql查询这种格式

时间:2016-07-21 08:10:45

标签: sql sql-server sql-server-2008 inner-join where

我有一个像这样的SQL查询:

select  T2.ItemCode,
        T2.ItemName,
        T1.Quantity,
        T2.InvntryUom,
        T1.Price  
from opor T0 
inner join por1 T1 on T1.DocEntry = T0.DocEntry 
inner join OITM T2 on T2.ItemCode = T1.ItemCode  
where T1.ItemCode = 'V0724-0158' 
and month(T0.DocDueDate) in ( 4,5, 6) and year(T0.DocDueDate) = '2016'

输出是这样的:

ItemCode             ItemName                                                                                             Quantity                                InvntryUom                                                                                           Price
-------------------- ---------------------------------------------------------------------------------------------------- --------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------
V0724-0158           SIS'88 80326 Brown Sugar 1kg Bag (Arabic) SBRADK124C - PET12/LLDPE60, 410mm x 1000m x 8-9C           29.000000                               Roll                                                                                                 232.390000
V0724-0158           SIS'88 80326 Brown Sugar 1kg Bag (Arabic) SBRADK124C - PET12/LLDPE60, 410mm x 1000m x 8-9C           29.000000                               Roll                                                                                                 232.390000
V0724-0158           SIS'88 80326 Brown Sugar 1kg Bag (Arabic) SBRADK124C - PET12/LLDPE60, 410mm x 1000m x 8-9C           10.000000                               Roll                                                                                                 234.740000

我有另一个比较查询:

SELECT '2015' as year, 
       T0.ItemCode as 'Item Code', 
       T1.Price As 'Base Price', 
       T2.Amount As 'Box qty', 
       T2.Price As 'Box Price'
FROM OITM T0 
Inner Join SPP1 T1 on T0.ItemCode=T1.ItemCode
Inner Join SPP2 T2 on T0.ItemCode=T2.ItemCode And T1.LINENUM=T2.SPP1LNum
Where T0.ItemCode ='V0724-0158' 
and FromDate ='20150101' 
and ToDate ='20160315'

输出是这样的:

year Item Code            Base Price                              Box qty                                 Box Price
---- -------------------- --------------------------------------- --------------------------------------- ---------------------------------------
2015 V0724-0158           242.000000                              16.000000                               242.000000
2015 V0724-0158           242.000000                              25.000000                               239.580000
2015 V0724-0158           242.000000                              50.000000                               235.000000
2015 V0724-0158           242.000000                              100.000000                              230.000000

在第一个查询中,quanity列应与第二个查询的框数量进行比较,下面是用第二个查询检查的条件

for eg: if quantity is 10 the box price is 242 
        if quantity is 29 the box price is 239.58 

示例输出结果将是这样的,带有名为box price的tan extra列:

ItemCode             ItemName                                                                                             Quantity                                InvntryUom                                                                                           Price                                   Box Price
-------------------- ---------------------------------------------------------------------------------------------------- --------------------------------------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------
V0724-0158           SIS'88 80326 Brown Sugar 1kg Bag (Arabic) SBRADK124C - PET12/LLDPE60, 410mm x 1000m x 8-9C           29.000000                               Roll                                                                                                 232.390000                             239.58
V0724-0158           SIS'88 80326 Brown Sugar 1kg Bag (Arabic) SBRADK124C - PET12/LLDPE60, 410mm x 1000m x 8-9C           29.000000                               Roll                                                                                                 232.390000                             239.58
V0724-0158           SIS'88 80326 Brown Sugar 1kg Bag (Arabic) SBRADK124C - PET12/LLDPE60, 410mm x 1000m x 8-9C           10.000000                               Roll                                                                                                 234.740000                             242

请帮助我是否可以在sql查询中实现,如果是,请帮助我如何做。

1 个答案:

答案 0 :(得分:0)

我不完全确定数量和价格之间存在什么逻辑关系,但您可以通过以下方式添加条件:

SituationSummaryType