首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql到postgresql的查询转换

问mysql到postgresql的查询转换
EN

Stack Overflow用户
提问于 2014-06-16 01:28:02
回答 1查看 81关注 0票数 0

我在MySql中有这个查询,我想知道,它在postgresql中可以吗?我想知道这是否应该是我需要改变的东西:)

我最关心的是group by函数,它不应该在post中执行。

我愿意考虑迁移到postgres的可能性,我想知道我使用的当前查询是否可以完成这项工作!

谢谢你提前帮我,vinc

代码语言:javascript
复制
    SELECT
     #GROUP BY
   dc.region as Region
 ,(DATEDIFF(CURRENT_TIMESTAMP,cc.activated_at)) AS 'Listing Age'
 ,cc.sku AS SKU
 ,CONCAT(dc.venture_url,cc.urlkey_details) as URL
 , cc.status 'Listing Status'
# REGION
, region.name_en Region
 ,(CASE WHEN is_agent=1 THEN supplier.name ELSE concat(supplier.name, ' (private)') END) AS Agent
 # Car type
 , ct.label_en as 'Vehicle Type'
,(Case 
    WHEN cc.fk_catalog_attribute_option_global_condition=1 THEN 'New'
    ELSE 'Other' END) AS 'Condition'
, ca.name_en as Brand

FROM 
 catalog_product_visible cpv

join catalog_config cc on cc.country_id = cpv.country_id and cc.id_catalog_config = cpv.fk_catalog_config

    left join
    dwh_country dc on dc.country_id = cc.country_id

  JOIN supplier AS supplier ON supplier.country_id = cc.country_id AND supplier.id_supplier = cc.product_owner

 LEFT JOIN
 (SELECT 
 profile_isocode,
 date_format(report_date,'%x-%v') as date,
 sum(coalesce((case when event_action IN ('Email Lead Success', 'Contact Submit Sucess') OR event_category = 'Email Lead Success' then total_events end),0)) as EmailLead,
 sum(coalesce((case when event_action IN ('Phone Lead Success', 'Show number Top', 'Show number Bottom', 'Show number Agency') OR event_category IN ('Phone Lead Success') then total_events end),0)) as PhoneLead,
 sum(coalesce((case when event_category IN ('SMS Lead Success') then total_events end),0)) as SMSLead
 FROM dwh_eventlistings c
 WHERE (event_action in ('Contact Submit Sucess')
 GROUP BY 1,2,3,4,5,6)  AS leads ON leads.profile_isocode = pv.profile_isocode AND leads.listing_id = pv.listing_id AND leads.date = date_format(pv.report_date,'%x-%v')

 LEFT JOIN catalog_simple AS cs ON cs.fk_catalog_config = cc.id_catalog_config AND cs.country_id = cc.country_id

WHERE supplier.name not like '%test%' and URL not like '%test%'

GROUP BY 
country, sku, report_week, source, medium, campaign
EN

回答 1

Stack Overflow用户

发布于 2014-06-16 01:58:44

是的,您正在使用一些在PostgreSQL上不起作用的MySQL功能。

  • 在给定别名Region的情况下,您的选择列表中有两个不同的列,然后您尝试GROUP BY该列,我认为这将抛出错误或导致未定义的行为。

  • 在子查询中,即使只有五列,也要使用GROUP BY 1,2,3,4,5,6。

在您的子查询中,对于像SUM().这样的聚合表达式的

  • 列是没有意义的

  • 在您的子查询中,您没有关闭WHERE子句中的括号,因此此查询也不能在MySQL中工作。

  • 您的查询违反了the single-value rule。在选择列表中使用了几个列,这些列既没有在GROUP BY子句中列出,也没有在聚合函数中列出。实际上,很难理解为什么要在GROUP BY子句中列出您拥有的列。

MySQL对GROUP BY语义更为宽松。请参阅我对explanation.的MySQL GROUP BY behavior的回答

我认为您对GROUP BY的功能有一些基本的误解。要了解更多信息,我建议您阅读这篇由SQL expert和author Roland Bouman撰写的关于Debunking GROUP BY myths的优秀博客文章。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24232231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档