1. 程式人生 > >比較好的SQL語句

比較好的SQL語句

ima eight rap ttr product hone AD sql語句 unit

批次導數據表頭

SELECT [運單號] ,
[運單號] AS [訂單號] ,
[運單號] AS [訂單號] ,
[運單號] ,
SUM([price] * [ProductNum]) AS [訂單總價] ,
SUM(CAST(( temp.[Weight] * ( CAST (temp.ProductNum AS DECIMAL(18, 2))
/ temp.ProductSumNum ) ) AS NUMERIC(18, 2))) AS [毛重] ,
SUM(CAST(( temp.[Weight] * ( CAST (temp.ProductNum AS DECIMAL(18, 2))
/ temp.ProductSumNum ) ) AS NUMERIC(18, 2))
- 0.01) AS [凈重] ,
temp2.TrueName AS [收貨人] ,
temp2.Adrress AS [收貨人地址] ,
CASE temp2.Mobile
WHEN ‘‘ THEN temp2.Phone
ELSE temp2.Mobile
END AS [電話] ,
temp.幣制 ,
‘‘ AS [orderType] ,
‘‘ AS [trafMode] ,
‘‘ AS [portCode] ,
‘‘ AS [licenseNo] ,
‘‘ AS [wrapType] ,
‘‘ AS [freight] ,
‘‘ AS [insuredFee] ,
‘‘ AS [freightMark] ,
‘‘ AS [insuredFeeMark] ,
‘‘ AS [goodsInfo] ,
‘‘ AS [consigneeCountry] ,
‘‘ AS [shipper] ,
‘‘ AS [shipperAddress] ,
‘‘ AS [shipperTelephone] ,
‘‘ AS [shipperCountry] ,
‘‘ AS [orderNote] ,
‘‘ AS [logisticsNote] ,
‘‘ AS [InventoryNote] ,
‘‘ AS [trafName] ,
‘‘ AS [voyageNo] ,
‘‘ AS [billNo] ,
‘1‘ AS packNo
FROM ( SELECT a.TransportOrderCode AS [運單號] ,
MAX(ISNULL(d.CatalogName,
SUBSTRING(ProductCatalogName,
CHARINDEX(‘-‘, ProductCatalogName)
+ 1, LEN(ProductCatalogName)))) CatalogName ,
d.itemName ,
d.itemNo ,
d.gcode AS Hscode ,
CASE WHEN a.Weight < 0.02 THEN 0.02
ELSE a.Weight
END AS [Weight] ,
SUM(c.ProductNum) AS ProductNum ,
CAST(CAST(SUM(c.TotalItemActualPrice) * 0.1498 AS DECIMAL(11,
2))
/ SUM(c.ProductNum) AS DECIMAL(11, 2)) [price] ,
‘美元‘ AS [幣制] ,
dbo.Fun_GetTransportProductNum(a.TransportOrderID) AS ProductSumNum ,
d.unit ,
d.unit1
FROM dbo.TWX_TransportOrder a WITH ( NOLOCK )
LEFT JOIN dbo.TWX_TransportFromMst b WITH ( NOLOCK ) ON a.TransportOrderID = b.TransportOrderID
LEFT JOIN dbo.TWX_TransportFromDtl c WITH ( NOLOCK ) ON b.TransportFormMstID = c.TransportFormMstID
LEFT JOIN dbo.TBD_ProductCatalog_MAP_KJT d WITH ( NOLOCK ) ON c.ProductCatalogID = d.CatalogID
AND c.DeleteUser = 0
WHERE a.StatusFlag >= 20
AND a.LogisticsTransferDtlID IN (
SELECT LogisticsTransferDtlID
FROM dbo.TWX_LogisticsTransferDtl WITH ( NOLOCK )
WHERE LogisticsTransferDtlCode IN (
SELECT [LogisticsTransferDtlCode]
FROM TWX_ScanLogisticsTransfer WITH ( NOLOCK )
WHERE DeleteUser = 0
AND BatchNo = ‘113020170828‘ ) )
GROUP BY a.TransportOrderCode ,
itemName ,
itemNo ,
d.gcode ,
a.Weight ,
d.unit ,
a.TransportOrderID ,
d.unit1
) temp
LEFT JOIN dbo.TWX_TransportOrder temp2 ON temp.[運單號] = temp2.TransportOrderCode
GROUP BY temp.[運單號] ,
temp.[幣制] ,
temp2.TrueName ,
temp2.Adrress ,
temp2.Mobile ,
temp2.Phone;

-------------------------------------------------------------------.

(SELECT TOP 1 C.CreateTime FROM TWX_LogisticsTransferDtl A
LEFT JOIN TWX_ScanLogisticsTransfer C ON A.LogisticsTransferDtlCode=C.LogisticsTransferDtlCode
WHERE A.LogisticsTransferDtlID=a.LogisticsTransferDtlID
) AS ‘裝車時間‘,
(
SELECT TOP 1 N.TrackTime FROM
[dbo].[Log_TransportOrderTrack] N WHERE N.TransportOrderCode=a.TransportOrderCode AND N.TrackCode IN (‘CAI_CUSTOMS_CLR_SUC_00‘,‘CAI_CUSTOMS_CLR_SUC_HK‘)
) AS ‘到港時間‘,
(
SELECT TOP 1 N.TrackTime FROM
[dbo].[Log_TransportOrderTrack] N WHERE N.TransportOrderCode=a.TransportOrderCode AND N.TrackCode=‘CAI_SIGN_IN‘
)
AS ‘簽收時間‘

比較好的SQL語句