将奇数和偶数分离到单独的列表中-更简洁的方法?

时间:2018-12-02 07:00:24

标签: python python-3.x list

如何优化以下代码。谁能帮助我:

SELECT
    QTABLE.[Model], 
    SUM(ISNULL(QTABLE.[Total Test Rides], 0)),
    SUM(ISNULL(QTABLE.[Total Month Test Rides], 0)),
    SUM(ISNULL(QTABLE.[Total Today Test Rides], 0)), 
    SUM(ISNULL(QTABLE.[Total Reciepts], 0)),
    SUM(ISNULL(QTABLE.[Total Month Reciepts], 0)),
    SUM(ISNULL(QTABLE.[Total TodayReciepts], 0)),
    SUM(ISNULL(QTABLE.[Retail Total], 0)),
    SUM(ISNULL(QTABLE.[Total Retail Month], 0)),
    SUM(ISNULL(QTABLE.[Total Retail Today], 0)),
    SUM(ISNULL(QTABLE.[Total WholeSale], 0)),
    SUM(ISNULL(QTABLE.[Total Month WholeSale], 0)),
    SUM(ISNULL(QTABLE.[Total Today WholeSale], 0)),
    SUM(ISNULL(QTABLE.[Total Cancled Bookings], 0)),
    SUM(ISNULL(QTABLE.[Cancled Bookings Month Count], 0)),
    SUM(ISNULL(QTABLE.[Cancled Bookings Today Count], 0)),
    SUM(ISNULL(QTABLE.[Total Finance Cases], 0)),
    SUM(ISNULL(QTABLE.[ Month Finance Cases], 0)),
    SUM(ISNULL(QTABLE.[ Today Finance Cases], 0)),
    SUM(ISNULL(QTABLE.[Stock in Hand], 0))  
FROM
    (SELECT 
         vProMod.ProdName 'Model',
         COUNT(vTR.iMasterId) 'Total Test Rides',
         CASE
            WHEN FORMAT(CONVERT(DATE, dbo.FCORE_INTTODATE(vTR.DateOfTestDrive), 103), 'yyyyMM') = FORMAT(Getdate(), 'yyyyMM') 
               THEN COUNT(vTR.iMasterId)
               ELSE 0
         END 'Total Month Test Rides',
         CASE
            WHEN CONVERT(DATE, dbo.FCORE_INTTODATE(vTR.DateOfTestDrive), 103) = CONVERT(DATE, Getdate(), 103) 
               THEN COUNT(vTR.iMasterId)
               ELSE 0
         END 'Total Today Test Rides',
         COUNT(vR.iMasterId) 'Total Reciepts', 
         CASE
            WHEN FORMAT(CONVERT(DATE, dbo.FCORE_INTTODATE(vR.ReceiptDate), 103), 'yyyyMM') = FORMAT(Getdate(), 'yyyyMM') 
               THEN COUNT(vR.iMasterId)
               ELSE 0
         END 'Total Month Reciepts',
         CASE
            WHEN CONVERT(DATE, dbo.FCORE_INTTODATE(vR.ReceiptDate), 103) = CONVERT(DATE, Getdate(), 103) 
               THEN COUNT(vR.iMasterId)
               ELSE 0
         END 'Total TodayReciepts'
         /*Case when DATEPART(MM,CONVERT(DATE,dbo.fCore_IntToDate(vR.ReceiptDate),103)) =DATEPART(MM,DATEADD(MM,-1,(CONVERT(DATE,getdate(),103)))) AND vR.Amount IN (0,NULL) then count(vR.iMasterId) else 0 end 'Rec Count Last Month2'*/
,
Count(vCIV.Mid)            'Retail Total'
, CASE
  WHEN FORMAT(CONVERT(DATE, dbo.FCORE_INTTODATE(vCIV.CID), 103), 'yyyyMM') =
       FORMAT(
       Getdate(), 'yyyyMM') THEN Count (vCIV.Mid)
  ELSE 0
END                        'Total Retail Month',
CASE
  WHEN CONVERT(DATE, dbo.FCORE_INTTODATE(vCIV.CID), 103) =
              CONVERT(DATE, Getdate(), 103)
       AND vCIV.ChsNo NOT IN ( 0, NULL ) THEN Count(vCIV.Mid)
  ELSE 0
END                        'Total Retail Today',
Sum(vMin.ReceivedQuantity) 'Total WholeSale',
CASE
  WHEN
FORMAT(CONVERT(DATE, dbo.FCORE_INTTODATETIME(vMin.iCreatedDate), 103), 'yyyyMM')
= FORMAT(Getdate(), 'yyyyMM') THEN Sum(vMin.ReceivedQuantity)
  ELSE 0
END                        'Total Month WholeSale',
CASE
  WHEN CONVERT(DATE, dbo.FCORE_INTTODATETIME(vMin.iCreatedDate), 103) =
       CONVERT(DATE, Getdate(), 103) THEN Sum(vMin.ReceivedQuantity)
  ELSE 0
END                        'Total Today WholeSale',
CASE
  WHEN vEnq.EnquiryStatus = '13' THEN Count(vEnq.iMasterId)
  ELSE 0
END                        'Total Cancled Bookings',
CASE
  WHEN
FORMAT(CONVERT(DATE, dbo.FCORE_INTTODATETIME(vEnq.EnquiryDate), 103), 'yyyyMM')
= FORMAT(Getdate(), 'yyyyMM')
AND vEnq.EnquiryStatus = '13' THEN Count(vEnq.iMasterId)
  ELSE 0
END                        'Cancled Bookings Month Count',
CASE
  WHEN CONVERT(DATE, dbo.FCORE_INTTODATETIME(vEnq.EnquiryDate), 103) =
              CONVERT(DATE, Getdate(), 103)
       AND vEnq.EnquiryStatus = 13 THEN Count(vEnq.iMasterId)
  ELSE 0
END                        'Cancled Bookings Today Count',
CASE
  WHEN vSo.Finance = 1 THEN Count(vSO.iMasterId)
  ELSE 0
END                        'Total Finance Cases',
CASE
  WHEN
FORMAT(CONVERT(DATE, dbo.FCORE_INTTODATETIME(vSo.iCreatedDate), 103), 'yyyyMM')
= FORMAT(Getdate(), 'yyyyMM')
AND vSo.Finance = 1 THEN Count(vSO.iMasterId)
  ELSE 0
END                        ' Month Finance Cases',
CASE
  WHEN CONVERT(DATE, dbo.FCORE_INTTODATETIME(vSo.iCreatedDate), 103) =
              CONVERT(DATE, Getdate(), 103)
       AND vSo.Finance = 1 THEN Count(vSO.iMasterId)
  ELSE 0
END                        ' Today Finance Cases',
Sum(vMin.ReceivedQuantity) 'Stock in Hand',
CASE
  WHEN ( vMin.iCreatedDate BETWEEN 17334860906496 AND 17334861004539 ) THEN Sum(
  vMin.ReceivedQuantity)
  ELSE 0
END                        'Wholesale Year',
CASE
  WHEN ( vCIV.CID BETWEEN 17334860906496 AND 17334861004539 )THEN Count (vCIV.Mid)
  ELSE 0
END                        'Retail Year'
FROM   (SELECT a.iMasterId    'Model',
                         b.iMasterId    'Prod',
                         a.sName        'ModName',
                         b.sName        'ProdName',
                         b.iProductGroup'pg'
                  FROM   vCore_Model a
                         INNER JOIN vCore_Product b
                                 ON b.sName = a.sName
                  WHERE  b.iProductGroup = 134309
                         AND a.sName IS NOT NULL) vProMod
        Left join vCore_TestDriveVS vTR 
            on vProMod.Model=vTR.Model
       LEFT JOIN vCore_ReceiptsVS vR
              ON vR.Veh =  vProMod.Model
       LEFT JOIN (SELECT a.iMasterId       'Mid',
                         b.VehMod          'ModID',
                         b.VehicleChassisNO'ChsNo',
                         a.CashInvoiceDate 'CID'
                  FROM   vCore_CashInvoiceVS a
                         INNER JOIN vuCore_CashInvoiceVS_Product_Details b
                                 ON a.iMasterId = b.iMasterId
                  WHERE  a.iMasterId > 0
                         ) vCIV
              ON vCIV.ModID =  vProMod.Model
       LEFT JOIN vuCore_MaterialIn_Material_In_Details_Details vMin
              ON vMin.Product = vProMod.Prod
       LEFT JOIN vCore_VSEnquiry vEnq
              ON vEnq.VehicleModel = vProMod.Prod
       LEFT JOIN vuCore_SalesOrderVS_Product_Details vSO
              ON vSO.ProductModel = vProMod.Model
WHERE  vTR.iMasterId > 0
GROUP  BY vProMod.ProdName,
          vTR.DateOfTestDrive,
          vR.ReceiptDate,
          vCIV.CID,
          vCIV.ChsNo,
          vMin.iCreatedDate,
          vEnq.EnquiryDate,
          vEnq.EnquiryStatus,
          vSo.Finance,
          vSo.iCreatedDate ) QTABLE   group by QTABLE.[Model]  order by  1 asc

1 个答案:

答案 0 :(得分:2)

您要创建两个列表,所以为什么不从其中一个列表中删除呢?

even = [1, 2, 3, 4]
odd = []
for i in reversed(range(len(even))):
    if even[i] % 2 != 0:
        odd.insert(0, even.pop(i))

pop从第一个开始删除,insert(0, ...)将其添加到开头(在正确的位置)。此外,与reverse-delete idiom一样,您可以反向遍历b

您将像以前一样以bc结尾。然而,在复杂度方面,它还是一样。


如果顺序不重要,请将odd.insert(0, even.pop(i))更改为odd.append(even.pop(i)),这样更易​​于阅读。