1

如何GROUP通过日期相同但时间不同的日期?

我使用此代码但收到错误:

SELECT TO_CHAR(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS'),'YYYY-MM-DD') DAY, DE_NO
FROM (
 SELECT TO_CHAR(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS'),'YYYY-MM-DD') DAY, DE_NO
 FROM PACKINGAPPS_FQA
WHERE (TO_CHAR(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS'),'YYYY-MM-DD') BETWEEN '$start_date' AND '$end_date')
)
GROUP BY DAY, DE_NO

错误是:

Warning: oci_execute() [function.oci-execute]: ORA-00904: "FQA_START_DATE": invalid identifier in...

请帮忙指教。谢谢

4

6 回答 6

1

FQA_START_DATE您的内部查询在字段列表中没有,因此您的第一个 SELECT 不正确。

尝试这个

SELECT DAY, DE_NO
FROM (
 SELECT TO_CHAR(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS'),'YYYY-MM-DD') 
   DAY, 
   DE_NO
 FROM PACKINGAPPS_FQA
WHERE (TO_CHAR(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS'),'YYYY-MM-DD') 
         BETWEEN '$start_date' AND '$end_date')
)
GROUP BY DAY, DE_NO
于 2013-10-22T10:39:26.943 回答
1

尝试使用

GROUP BY CAST(datefield AS DATE)
于 2013-10-22T10:36:19.687 回答
0

You can get the date part of a date (without time), by using the TRUNC function. TRUNC will remove the time part.

The resulting value is still a date and you can use it to group by and format it as will.

I must admit that it's not exactly clear to me what type of value the field FQA_START_DATE is and what type the input variables are. It looks like they are all strings, but I would advise you to store dates as dates and also use date input parameters if possible.

So ideally, your query would look like this:

SELECT 
  FQA_START_DATE_WITHOUT_TIME, 
  DE_NO
FROM (
 SELECT 
   trunc(FQA_START_DATE) as FQA_START_DATE_WITHOUT_TIME, 
   DE_NO
 FROM P
   ACKINGAPPS_FQA
 WHERE 
   trunc(FQA_START_DATE) BETWEEN $start_date AND $end_date
)
GROUP BY 
  FQA_START_DATE_WITHOUT_TIME, 
  DE_NO

And if you are not going to need any other fields, extra joins or aggregations, you can simplify it to this:

SELECT DISTINCT /* Distinct will remove duplicate rows */
  trunc(FQA_START_DATE) as FQA_START_DATE_WITHOUT_TIME, 
  DE_NO
FROM P
  ACKINGAPPS_FQA
WHERE 
  trunc(FQA_START_DATE) BETWEEN $start_date AND $end_date

Note that this assumes FQA_START_DATE to be a date/datetime field, assumes start_date and end_date to be dates/datetimes and will return a field FQA_START_DATE_WITHOUT_TIME, which is also a date field.

于 2013-10-22T11:34:13.507 回答
0
SELECT * FROM TABLE_NAME 
  WHERE DATE(coloumn_name)='SPECIFY DATE' 
  GROUP BY coloumn_name
于 2013-10-22T10:41:18.303 回答
0

您可以使用

SELECT field_name(s) FROM table_name GROUP BY DATE(date_field);

这里的 Date() 函数将日期时间转换为日期。

日期和时间函数

于 2013-10-22T10:41:25.683 回答
0

FQA_START_DATE首先,您的内联视图(子句SELECT中的语句)中没有任何带有别名的列FROM。您的基表中可能有一个具有该名称的列,但这不计算在内,因为您是SELECT从内联视图而不是基表中获取的。

其次,WHERE您的内联视图中的条件已经完全失控,当您将一个字符值比较为两个字符值之间时,它将返回所有错误的结果。

我建议您使用以下查询:

  SELECT TRUNC(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS')) AS dt, de_no
    FROM packingapps_fqa
   WHERE TRUNC(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS'))
         BETWEEN TRUNC (TO_DATE('$start_date')) AND TRUNC(TO_DATE('$end_date'))
GROUP BY TRUNC(TO_DATE(FQA_START_DATE,'DD-Mon-YYYY HH24:MI:SS')), de_no;
于 2013-10-22T14:43:29.927 回答