如何查找列之间的天数(A6-联系日期和A7-响应日期)

时间:2018-12-20 20:24:28

标签: sql-server

在SQL中执行时收到错误。

+----------+-------------+
| Field    | Type        |
+----------+-------------+
| id       | varchar(17) |
| log_date | date        |
+----------+-------------+

SELECT last_day.log_date, COUNT(distinct last_day.id) as users_num
FROM (SELECT DISTINCT log_date, id
      FROM `userActivity`) this_day
JOIN (SELECT DISTINCT log_date, id
      FROM `userActivity`) last_day
ON this_day.id = last_day.id
AND this_day.log_date = "2018-10-01"
AND last_day.log_date BETWEEN "2018-10-01" AND "2018-10-30"
GROUP BY log_date;

有人可以告诉我我的查询出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

如果您将按照Dale的格式进行格式化,则可以看到您有一个嵌套的select语句。这部分:

SELECT datediff(dd, A6, A7) AS WAITTIME           
FROM viewz_INTAKE_STACK 
INNER JOIN RAI_RAI ON viewz_INTAKE_STACK.rai_uno = RAI_RAI.rai_uno      
WHERE (Where A6 BETWEEN $P{FromDate} and $P{ToDate})ISNULL(A6, '1900-01-01')!='1900-01-01'

如果这应该是子查询,则需要用括号括起来。

此外,对于您的外部查询,没有FROM子句。

因此,作为猜测,您可能只想删除第二个SELECT字并为该列添加一个逗号,删除一个只调用了基表而没有列的实例,并删除了重复的实例where。这是一个刺。

SELECT 
    viewz_INTAKE_STACK.A3 AS STNum
  , viewz_INTAKE_STACK.A6 AS DateofRef
  , viewz_INTAKE_STACK.A6b AS TimeofRef
  , viewz_INTAKE_STACK.A7 AS InitRespDate
  , viewz_INTAKE_STACK.A7b AS InitTime
  , viewz_INTAKE_STACK.A8a
  , viewz_INTAKE_STACK.A8c
  , viewz_INTAKE_STACK.A8d
  , viewz_INTAKE_STACK.A8e
  , viewz_INTAKE_STACK.A8f
  , viewz_INTAKE_STACK.A8g
  , viewz_INTAKE_STACK.A8h
  , viewz_INTAKE_STACK.A8i
  , viewz_INTAKE_STACK.A8j
  , viewz_INTAKE_STACK.A8k
  , viewz_INTAKE_STACK.A8l
  , viewz_INTAKE_STACK.L2a AS Gen             
  , datediff(dd, A6, A7) AS WAITTIME           
FROM viewz_INTAKE_STACK 
INNER JOIN RAI_RAI ON viewz_INTAKE_STACK.rai_uno = RAI_RAI.rai_uno      
WHERE A6 BETWEEN $P{FromDate} and $P{ToDate} and ISNULL(A6, '1900-01-01')!='1900-01-01'