有条件地基于辅助列进行汇总

时间:2020-02-20 19:43:12

标签: sql sum case sybase sqlanywhere

我很难找到信息来有条件地求和,如果不满足条件就不求和。在以下查询中,我尝试对具有LineItemID且与ID匹配且前导零的项目进行求和。因此,在下面的查询中,我尝试将LineItemID 3404和03404求和成单个行,而不求和其他选定的ID。我采用了这种双柱解决方案,但似乎无法正确触发:

With LeadingZeroCombinedTransactions AS
(
    SELECT TransactionJournalDetail."TLI_ScanCode" As LineItemID, 
    StockInventory."INV_ScanCode" As ID, 
    StockInventory."INV_Name" As Name, 
    TransactionJournalDetail."TLI_ReceiptAlias" As Alias,
    TransactionJournalDetail."ASC_PK" AS Alt,
    SUM(TransactionJournalDetail."TLI_Quantity") As Quantity

    FROM ( ( ( "ecrs"."TJ_StockInventory" StockInventory 
        LEFT OUTER JOIN "ecrs"."TJ_Brands" Brands ON StockInventory."INV_BRD_FK" = Brands."BRD_PK" ) 
        LEFT OUTER JOIN "ecrs"."TJ_Departments" Departments ON StockInventory."INV_DPT_FK" = Departments."DPT_PK" ) 
        LEFT OUTER JOIN "ecrs"."TJ_TransactionJournalDetail" TransactionJournalDetail ON StockInventory."INV_PK" = TransactionJournalDetail."INV_PK" )
        LEFT OUTER JOIN "ecrs"."TJ_TransactionJournal" TransactionJournal ON "TransactionJournalDetail"."TJD_TRN_FK" = TransactionJournal."TRJ_TRN_FK"

    GROUP BY ID, Name, Alias, Alt,LineItemID
    ORDER BY ID DESC
)

SELECT LineItemID AS LineItemID, ID AS ID, Name, Alias,

      SUM( CASE WHEN LineItemID = STRING('0', ID)
         THEN LeadingZeroCombinedTransactions.Quantity else 0 end ) as FirstQuantity,
      SUM( CASE WHEN LineItemID = STRING('0', ID)
         THEN 0 else LeadingZeroCombinedTransactions.Quantity end ) as SecondQuantity

FROM LeadingZeroCombinedTransactions
WHERE ((ID='03404         ')
OR (ID='894773001018  '))
GROUP BY LineItemID,Name, ID, Alias

导致此结果的原因:

+----------------+------------------+----------------------------------+------------------------------------+---------------+----------------+
|   LineItemID   |        ID        |               Name               |               Alias                | FirstQuantity | SecondQuantity |
+----------------+------------------+----------------------------------+------------------------------------+---------------+----------------+
| '894773001018' | '894773001018  ' | 'Natural Diet Cola             ' | 'ZEVIA-SODA DIET COLA 12OZ'        |        0.0000 |         3.0000 |
| '894773001018' | '894773001018  ' | 'Natural Diet Cola             ' | 'ZEVIA-COLA SF 6PK'                |        0.0000 |         1.0000 |
| '3404'         | '03404         ' | 'Lemon Thyme Whole Chicken     ' | 'DELI-CHCKN WHLE THYME LEMON NGMO' |        0.0000 |         3.0000 |
| '03404'        | '03404         ' | 'Lemon Thyme Whole Chicken     ' | 'DELI-CHCKN LEMON THYME WHLE NGMO' |        0.0000 |        91.0000 |
+----------------+------------------+----------------------------------+------------------------------------+---------------+----------------+

但是,这是我很难达到的理想结果(求和有或缺少前导零的4/5位IDS):

+----------------+------------------+----------------------------------+------------------------------------+----------------+
|   LineItemID   |        ID        |               Name               |               Alias                |    Quantity    |
+----------------+------------------+----------------------------------+------------------------------------+----------------+
| '894773001018' | '894773001018  ' | 'Natural Diet Cola             ' | 'ZEVIA-SODA DIET COLA 12OZ'        |         3.0000 |
| '894773001018' | '894773001018  ' | 'Natural Diet Cola             ' | 'ZEVIA-COLA SF 6PK'                |         1.0000 |
| '03404'        | '03404         ' | 'Lemon Thyme Whole Chicken     ' | 'DELI-CHCKN LEMON THYME WHLE NGMO' |        94.0000 |
+----------------+------------------+----------------------------------+------------------------------------+----------------+

无论我尝试了哪种求和方法,我要么最终求和我不希望求和的项(“ 894773001018”),要么不求和我不想求和的项(“ 03404”)。

在这里真的很难找到解决方案。我宁愿不需要再添加两列,而仅根据项目是否具有前导零来有条件地求和,但是每次都会被混淆。

1 个答案:

答案 0 :(得分:0)

已编辑 您的LineItemID和ID似乎没有相同的格式,但是遵循相同的模式。

字段是字母数字,因此请尝试修剪空格,然后再次替换左侧的0并修剪。

...


SELECT TRIM(substring(TRIM(TransactionJournalDetail."TLI_ScanCode"), patindex('%[^0]%',TRIM(TransactionJournalDetail."TLI_ScanCode")),14)) As LineItemID, 
    TRIM(substring(TRIM(StockInventory."INV_ScanCode"), patindex('%[^0]%',TRIM(StockInventory."INV_ScanCode")),14)) As ID, 
...

请注意,如果您更改此设置,则必须在WHERE子句中引入的值将是

WHERE ((ID='3404')
OR (ID='894773001018'))