通过检查每个行属性查询将发票添加到表

时间:2018-08-16 07:29:13

标签: sql sql-server tsql

Invoice
========
InvoiceID
LineID
ItemID
AmountInc
MenuPrice

#TempTable
========
InvoiceID
AmountInc (will have Sum(AmountInc))

我想构建一个表,按照上述各列从“发票”表中获取数据。

要添加到临时表中,发票将需要至少包含一行菜单价格=是的项目,例如:

> InvoiceTable
> InvoiceID | LineID | ItemID | AmountInc | MenuPrice
  0001      | 1      | A001   | 10        | Yes
  0001      | 2      | A002   | 20        | No
  0002      | 1      | A001   | 10        | No
  0002      | 2      | A003   | 15        | No
  0002      | 3      | A001   | 10        | No
  0003      | 1      | A003   | 15        | Yes
  0003      | 2      | A004   | 25        | Yes

结果将是:

#TempTable
> InvoiceID | AmountInc
  0001      | 30        
  0003      | 15        

或者如果可能的话:

#TempTable
> InvoiceID | AmountInc | MenuPrice
  0001      | 30        | Mixed
  0002      | 35        | No
  0003      | 40        | Yes

写这个的最好方法是什么?

2 个答案:

答案 0 :(得分:1)

像这样尝试:

while self.running:
  self.logger.debug('Pipe loop')
  ChannelNo = ctypes.c_long (int(self.comm_channel))
  BufferLength = ctypes.c_long (0)
  CmsApStamp = ctypes.c_long (0)
  result=True
  data_buff=[]
  result=self.hllReadPipe(   
    ChannelNo=ChannelNo,
    ReadBuffer=ctypes.byref(self.ReadBuffer),
    BufferLength=ctypes.byref(BufferLength),
    CmsApStamp=ctypes.byref(CmsApStamp))
  if result:
    length=BufferLength.value
    self.logger.info('Data ['+str(self.ReadBuffer.raw[:length])+']. Length ['+str(length)+'] bytes.')
  time.sleep(loop_sleep)

-查询将首先使用cte查找具有至少一个<00074> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00075> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00076> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00077> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00078> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00079> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00080> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00081> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00082> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00083> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00084> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00085> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00086> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00087> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 <00088> 000B049A P WM_COMMAND wNotifyCode:0000 wID:4 hwndCtl:001D02E4 的{​​{1}}的所有值,然后此有效ID列表将用于DECLARE @tbl TABLE(InvoiceID INT, LineID INT,ItemID VARCHAR(10),AmountInc INT,MenuPrice VARCHAR(10)); INSERT INTO @tbl VALUES (0001,1,'A001',10,'Yes') ,(0001,2,'A002',20,'No') ,(0002,1,'A001',10,'No') ,(0002,2,'A003',15,'No') ,(0002,3,'A001',10,'No') ,(0003,1,'A003',15,'Yes') ,(0003,2,'A004',25,'Yes'); 列表。

InvoiceID

结果

Yes

此查询将带回您的替代预期结果:

INNER JOIN

答案 1 :(得分:0)

您可以按照以下步骤进行操作

With TempTable As
(
    Select InvoiceID, Sum(AmountInc) AmountInc, MenuPrice From InvoiceTable 
    Group By InvoiceID, MenuPrice
)
Select InvoiceID, Sum(AmountInc) AmountInc, 
Case When Count(*) > 1 then 'Mixed' Else Max(MenuPrice) End MenuPrice
From TempTable Group By InvoiceID