2017-03-13 7 views
-1

誰かがPostgresで動作するように次のクエリを変更してもらえますか?SQL DATEFORMATをPostgresに変換する

SELECT 
user2.id AS ID, 
ul.timeaccess, 
user2.firstname AS Firstname, 
user2.lastname AS Lastname, 
user2.email AS Email, 
user2.username AS IDNumber, 
user2.institution AS Institution, 

IF (user2.lastaccess = 0,'never', 
DATE_FORMAT(FROM_UNIXTIME(user2.lastaccess),'%Y-%m-%d')) AS dLastAccess 

,(SELECT DATE_FORMAT(FROM_UNIXTIME(timeaccess),'%Y-%m-%d') FROM  mdl_user_lastaccess WHERE userid=user2.id AND courseid=c.id) AS CourseLastAccess 

,(SELECT r.name 
FROM mdl_user_enrolments AS uenrol 
JOIN mdl_enrol AS e ON e.id = uenrol.enrolid 
JOIN mdl_role AS r ON e.id = r.id 
WHERE uenrol.userid=user2.id AND e.courseid = c.id) AS RoleName 

FROM mdl_user_enrolments AS ue 
JOIN mdl_enrol AS e ON e.id = ue.enrolid 
JOIN mdl_course AS c ON c.id = e.courseid 
JOIN mdl_user AS user2 ON user2 .id = ue.userid 
LEFT JOIN mdl_user_lastaccess AS ul ON ul.userid = user2.id 
WHERE c.id=14 AND ue.userid NOT IN (SELECT qa.userid FROM mdl_quiz_attempts  AS qa 
JOIN mdl_quiz AS q ON qa.quiz = q.id 
JOIN mdl_course AS c ON q.course = c.id 
WHERE c.id = 14 AND q.name LIKE '%quiz name goes here%') 

それは私はそれがto_char関数などに行きますが、事前

答えて

0
SELECT 
user2.id AS "ID", 
ul.timeaccess, 
user2.firstname AS "Firstname", 
user2.lastname AS "Lastname", 
user2.email AS "Email", 
user2.username AS "IDNumber", 
user2.institution AS "Institution", 

(CASE WHEN user2.lastaccess = 0 THEN'never' END || 
to_char(to_timestamp(user2.lastaccess)::date,'YYYY-MM-DD')) AS "dLastAccess" 

,(SELECT to_char(to_timestamp(timeaccess)::date,'YYYY-MM-DD') FROM  mdl_user_lastaccess WHERE userid=user2.id AND courseid=c.id) AS "CourseLastAccess" 

,(SELECT r.name 
FROM mdl_user_enrolments AS uenrol 
JOIN mdl_enrol AS e ON e.id = uenrol.enrolid 
JOIN mdl_role AS r ON e.id = r.id 
WHERE uenrol.userid=user2.id AND e.courseid = c.id) AS "RoleName" 

FROM mdl_user_enrolments AS ue 
JOIN mdl_enrol AS e ON e.id = ue.enrolid 
JOIN mdl_course AS c ON c.id = e.courseid 
JOIN mdl_user AS user2 ON user2 .id = ue.userid 
LEFT JOIN mdl_user_lastaccess AS ul ON ul.userid = user2.id 
WHERE c.id=14 AND ue.userid NOT IN (SELECT qa.userid FROM mdl_quiz_attempts  AS qa 
JOIN mdl_quiz AS q ON qa.quiz = q.id 
JOIN mdl_course AS c ON q.course = c.id 
WHERE c.id = 14 AND q.name LIKE '%quiz name goes here%') 

ねえ、私は試しました。

FROM_UNIXTIMEは、postgresのto_timestampと同等です。 IF ELSEケースの場合がポストグルです。 to_char - 日付を書式設定する場合は、これを使用します。

0

おかげで見持って、それを変更する方法がわからない持っていることを理解するのMoodle ためのものです:Postgresのドキュメント上Data Type Formatting Functionsを。たとえば、TO_CHAR(日時、 'YYYY-MM-DD')

用途:

select to_char('20170501'::date, 'YYYY-MM-DD') 

戻り値:

2017-05-01 

ここでそれを確認してください:http://rextester.com/JDZA16474

関連する問題