我们无法从当前时间列中减去时间列,因为它们具有不同的格式: Y-d-m HMS和Y-m-d HMS 。我们无法更改时间列中的读取顺序。因此,我们必须更改当前列的时间格式,并将其设置为日期时间对象。
我们尝试了SET statement_timeout TO 9000000;
SELECT
sp.sku
,soi.product_id
,sc.name
,sp.name
,sp.active
,sp.stock AS total_stock
,sp.actual_stock
,sp.virtual_stock
,sp.price AS current_proce
,date(sp.created::timestamp AT time zone 'utc' AT time zone 'asia/kolkata')
,CURRENT_DATE - date(sp.created::TIMESTAMP AT TIME ZONE 'utc' AT TIME ZONE 'asia/kolkata') AS days_since_created
,DATE_PART('week', date(sp.created::timestamp AT time zone 'utc' AT time zone 'asia/kolkata')) AS week
,sum(soi.quanity) AS units_sold
,((soi.price* sum(soi.quanity)) - sum(soi.discount)) AS Revenue_Before_Tax
,case
WHEN sc.name = 'Accessories' AND so.currency = 'USD' THEN ((soi.price * sum(soi.quanity) * 70) - sum(soi.discount)) - round((((soi.price*sum(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Apparel' AND so.currency = 'USD' THEN ((soi.price*sum(soi.quanity) * 70) - sum(soi.discount)) - round((((soi.price* sum(soi.quanity)* 70) - SUM(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Wellness' AND so.currency ='USD' THEN ((soi.price*sum(soi.quanity))*70 - sum(soi.discount)) - round((((soi.price*sum(soi.quanity) * 70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Footwear' AND so.currency = 'USD' THEN ((soi.price*sum(soi.quanity)*70) - sum(soi.discount)) - round((((soi.price* sum(soi.quanity)*70) - SUM(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Bags' AND so.currency = 'USD' THEN ((soi.price*sum(soi.quanity)*70) - SUM(soi.discount)) - round((((soi.price* sum(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Jewelry'AND so.currency = 'USD' THEN ((soi.price*sum(soi.quanity*70)) - sum(soi.discount))- round((((soi.price* sum(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Home & Decor' AND so.currency = 'USD' THEN ((soi.price*sum(soi.quanity)*70) - sum(soi.discount)) - round((((soi.price* sum(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Mother & Child' AND so.currency = 'USD' THEN ((soi.price*sum(soi.quanity)*70) - sum(soi.discount)) - round((((soi.price* sum(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
ELSE ((soi.price*sum(soi.quanity)) - sum(soi.discount)) - round((((soi.price*sum(soi.quanity)) - sum(soi.discount))*hsn.tax::integer)/100)
END AS revenue_after_tax
,round(ng.revenue_after_tax / ng.units_sold) AS average_selling_price
FROM
order_order AS so
LEFT JOIN
order_orderproduct AS soi ON so.id = soi.order_id
LEFT JOIN
store_product AS sp ON soi.product_id = sp.id
LEFT JOIN
store_category AS sc ON sp.category_id = sc.id
LEFT JOIN
tms_hsncode AS hsn ON sp.hsncode_id = hsn.id
LEFT JOIN
(SELECT
sp.sku AS gt
,sp.id
,sc.name
,sp.name
,sp.active
,sp.stock AS total_stock
,sp.actual_stock
,sp.virtual_stock
,sp.price AS current_proce
,date(sp.created::timestamp AT time zone 'utc' AT time zone 'asia/kolkata')
,CURRENT_DATE - date(sp.created::TIMESTAMP AT TIME ZONE 'utc' AT TIME ZONE 'asia/kolkata') AS days_since_created
,DATE_PART('week', date(sp.created::timestamp AT time zone 'utc' AT time zone 'asia/kolkata')) AS week
,count(soi.quanity) AS units_sold
,((soi.price* count(soi.quanity)) - sum(soi.discount)) AS Revenue_Before_Tax
,case
WHEN sc.name = 'Accessories' AND so.currency = 'USD' THEN ((soi.price * count(soi.quanity) * 70) - sum(soi.discount)) - round((((soi.price*count(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Apparel' AND so.currency = 'USD' THEN ((soi.price*count(soi.quanity) * 70) - sum(soi.discount)) - round((((soi.price* count(soi.quanity)* 70) - SUM(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Wellness' AND so.currency ='USD' THEN ((soi.price*count(soi.quanity))*70 - sum(soi.discount)) - round((((soi.price*count(soi.quanity) * 70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Footwear' AND so.currency = 'USD' THEN ((soi.price*count(soi.quanity)*70) - sum(soi.discount)) - round((((soi.price* count(soi.quanity)*70) - SUM(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Bags' AND so.currency = 'USD' THEN ((soi.price*count(soi.quanity)*70) - SUM(soi.discount)) - round((((soi.price* count(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Jewelry'AND so.currency = 'USD' THEN ((soi.price*count(soi.quanity*70)) - sum(soi.discount))- round((((soi.price*count(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Home & Decor' AND so.currency = 'USD' THEN ((soi.price*count(soi.quanity)*70) - sum(soi.discount)) - round((((soi.price*count(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
WHEN sc.name = 'Mother & Child' AND so.currency = 'USD' THEN ((soi.price*count(soi.quanity)*70) - sum(soi.discount)) - round((((soi.price*count(soi.quanity)*70) - sum(soi.discount))*hsn.tax::integer)/100)
ELSE ((soi.price*count(soi.quanity)) - sum(soi.discount)) - round((((soi.price*count(soi.quanity)) - sum(soi.discount))*hsn.tax::integer)/100)
END AS revenue_after_tax
FROM
order_order AS so
LEFT JOIN
order_orderproduct AS soi ON so.id = soi.order_id
LEFT JOIN
store_product AS sp ON soi.product_id = sp.id
LEFT JOIN
store_category AS sc ON sp.category_id = sc.id
LEFT JOIN
tms_hsncode AS hsn ON sp.hsncode_id = hsn.id
WHERE
sp.created::TIMESTAMP AT TIME ZONE 'utc' AT TIME ZONE 'asia/kolkata' >= CURRENT_DATE - 100
GROUP BY
sp.sku
,sp.id
,soi.price
,so.currency
,hsn.tax
,sc.name
,sp.name
,sp.active
,sp.stock
,sp.actual_stock
,sp.virtual_stock
,sp.price
,sp.created
,days_since_created
,week ) AS ng ON sp.sku = ng.gt
WHERE
sp.created::TIMESTAMP AT TIME ZONE 'utc' AT TIME ZONE 'asia/kolkata' >= CURRENT_DATE - 100
GROUP BY
sp.sku
,soi.product_id
,soi.price
,so.currency
,hsn.tax
,sc.name
,sp.name
,sp.active
,sp.stock
,sp.actual_stock
,sp.virtual_stock
,sp.price
,sp.created
,days_since_created
,week
,ng.revenue_after_tax
,ng.units_sold
;
,但是这将当前列置于相同的顺序:pd.to_datetime(df['Our time column'])
ex: 2019-05-08 05:58:00
错误的答案:
df['lenghtofstay']= (df['Current time']-df['Our time column'])
df['lenghtofstay'].head()
当前时间:2019-08-14 13:54:40.303740
我们的时间栏:05-08-2019 05:58
当前时间-我们的时间列:新列:9天xx:xx:xx
答案 0 :(得分:1)
如果列Our time column
中的所有值的格式均为Y-d-m H:M:S
,则可以通过format
参数进行指定:
df = pd.DataFrame({'Current time':['2019-08-14 13:54:40.303740','2019-05-10 05:58:00'],
'Our time column':['2019-05-08 05:58:00','2019-06-04 05:58:00']})
df['Current time'] = pd.to_datetime(df['Current time'],)
df['Our time column'] = pd.to_datetime(df['Our time column'], format='%Y-%d-%m %H:%M:%S')
df['lenghtofstay']= (df['Current time']-df['Our time column'])
print (df)
Current time Our time column lenghtofstay
0 2019-08-14 13:54:40.303740 2019-08-05 05:58:00 9 days 07:56:40.303740
1 2019-05-10 05:58:00.000000 2019-04-06 05:58:00 34 days 00:00:00
答案 1 :(得分:0)
重复两次pd.to_datetime()
,以使两列的格式相同
df['Current time']= pd.to_datetime(df['Current time'],format='%Y-%m-%d %H:%M:%S').dt.strftime('%Y-%d-%m %H:%M:%S')
df['Current time']= pd.to_datetime(df['Current time'],format='%Y-%d-%m %H:%M:%S')
然后您可以进行差值计算