将DATE_DIFF与MAX(DATE)一起使用时的问题

时间:2017-11-23 08:14:23

标签: sql google-bigquery

我正在使用BigQuery中的Google Analytics数据,我正在尝试计算IF(COUNT(DISTINCT hits.transaction.transactionId) > 0,(MAX(DATE))CURRENT_DATE()之间的DATE_DIFF,但我收到错误:

  

错误:参数类型的函数IF没有匹配的签名:BOOL,STRUCT。支持的签名:IF(BOOL,ANY,ANY)在[12:11]

这是什么意思? IF的两个部分必须是同一类型吗?我似乎也出现了类似的错误,表明MAX(DATE)是一个STRING,所以我试过了:

DATE_DIFF(IF(COUNT(DISTINCT hits.transaction.transactionId) > 0, PARSE_DATE('%Y%m%d',(MAX(DATE)),CURRENT_DATE())),CURRENT_DATE(),DAY) AS days_since_lst_ord

但后来我得到了:

  

错误:参数类型的函数PARSE_DATE没有匹配的签名:STRING,STRING,DATE。支持的签名:[12:66]

的PARSE_DATE(STRING,STRING)

以下是我的完整查询:

SELECT customDimension.value AS UserID, COUNT(DISTINCT VisitId) AS visits, COUNT(DISTINCT hits.transaction.transactionId) AS orders, IFNULL(SUM(totals.bounces),0) AS bounces, SUM(totals.pageviews) AS pageviews, IFNULL(SUM(hits.transaction.transactionRevenue)/1000000,0) AS revenue,
ROUND(IFNULL(COUNT(DISTINCT hits.transaction.transactionId)/COUNT(DISTINCT VisitId),0),5) AS conversion_rate,
ROUND(IFNULL(SUM(totals.bounces)/COUNT(DISTINCT VisitId),0),5) AS bounce_rate,   ROUND(IFNULL(SUM(hits.transaction.transactionRevenue)/1000000,0)/COUNT(DISTINCT VisitId),2) AS rev_per_visit,
IF(COUNT(DISTINCT hits.transaction.transactionId) > 0,(MAX(DATE)),"unknown") AS last_ord_date,
DATE_DIFF(IF(COUNT(DISTINCT hits.transaction.transactionId) > 0, PARSE_DATE('%Y%m%d',(MAX(DATE)),CURRENT_DATE())),CURRENT_DATE(),DAY) AS days_since_lst_ord
FROM `PROJECTNAME.ga_sessions_20*` AS t
  CROSS JOIN UNNEST (hits) AS hits
  CROSS JOIN UNNEST(t.customdimensions) AS customDimension
WHERE parse_date('%y%m%d', _table_suffix) between
DATE_sub(current_date(), interval 1 day) and
DATE_sub(current_date(), interval 1 day)
AND customDimension.index = 2
AND customDimension.value NOT LIKE "true"
AND customDimension.value NOT LIKE "false"
AND customDimension.value NOT LIKE "undefined"
AND customDimension.value IS NOT NULL
GROUP BY UserID, hits.eventInfo.eventCategory
Order by orders DESC

2 个答案:

答案 0 :(得分:1)

SELECT IF(true, 1, '1.1')
     

这是什么意思? IF的两个部分必须是同一类型吗?

true_result和else_result表达式都必须能够强制转换为常见的超类型。

例如,下面不起作用

SELECT IF(true, 1, 1.1)   

而这一个 - 将完美地运作

SELECT IF(true, 1, 2)   

请注意,最后一个陈述的结果将是1.0(FLOAT)

同时,如果你在下面运行 - 结果将是1(INTEGER)

struct MyState: RaspState {
    var price: Float = 0.0

    mutating func apply(event: RaspEvent) {
        switch event {
        case let priceEvent as PriceEvent:
            self.price = priceEvent.price
        default:
        break
        }
    }
}

struct PriceEvent: RaspEvent {
    let price: Float
}

let myAggregator = RaspAggregator<MyState>(initial: MyState())

let priceSelector = RaspSelector<MyState, Float> { state -> Float in
    let value: Float = state.price
    return value
}

let price = myAggregator.select(selector: priceSelector)

price.bind(to: self.tableViewCal.rx.items(cellIdentifier: "edittingCell")){ (row, product: Product, cell: CaloriesEdittingCell) in
            cell.lblContentName.text = self.products$.value[row].name ?? ""
            cell.textValue.text = "\(self.products$.value[row].price ?? 0)"
            cell.textValue.rx.text
                .map({ value -> Float in
                    return (value! as NSString).floatValue
                })
                .subscribe({value in
                    myAggregator.manual(PriceEvent(price: value) as RaspEvent)
                    print(product)
                }).disposed(by: self.bag)
        }.disposed(by: bag)

详细了解Coercion

答案 1 :(得分:0)

可以用这个替换last_ord_datedays_since_lst_ord吗?

IF(COUNT(DISTINCT hits.transaction.transactionId) > 0, (MAX(DATE)) ,"unknown") AS last_ord_date,

DATE_DIFF(IF(COUNT(DISTINCT hits.transaction.transactionId) > 0, PARSE_DATE('%Y%m%d',(MAX(DATE))) , CURRENT_DATE())
    ,CURRENT_DATE()
    ,DAY) AS days_since_lst_ord