0

How I can implement it? I am confused in syntax also?

CREATE OR REPLACE Function dailyyyy_volation_routine(_ispeed_count integer,_rlvd_count integer,_stopline_count integer,_wrongdir_count integer
                                                     ,_wronglane_count integer,_zebracross_count integer,_total_count integer)
RETURNS void AS $$
BEGIN
INSERT INTO temp_daily_stats(ispeed_count,rlvd_count,stopline_count,wrongdir_count,wronglane_count,zebracross_count,total_count)
with t1 as (SELECT SUM(ispeed_count) as ispeed_count from temp_hourly_stats),
t3 as (SELECT SUM(rlvd_count) as rlvd_count from temp_hourly_stats),
t4 as (SELECT SUM(stop_line_count) as stopline_count from temp_hourly_stats),
t5 as (SELECT SUM(wrong_dir_count) as wrong_dir_count from temp_hourly_stats),
t6 as (SELECT SUM(wrong_lane_count) as wrong_lane_count from temp_hourly_stats),
t7 as (SELECT SUM(zebra_cross_count) as zebra_cross_count from temp_hourly_stats),
t8 as (SELECT @tc := SUM(total_count) as total_day_count from temp_hourly_stats),
SELECT DISTINCT t1.ispeed_count,t3.rlvd_count,t4.stopline_count,t5.wrong_dir_count,t6.wrong_lane_count,t7.zebra_cross_count,t8.total_day_count
FROM t1,t3,t4,t5,t6,t7,t8 limit 1;
END
$$ 
LANGUAGE SQL;


I want to put data from one table to another using postgresql function?*

8
  • SQL functions don't use BEGIN and END. Remove those two lines. Commented Feb 25, 2020 at 10:26
  • Sir, now its showing this error Commented Feb 25, 2020 at 10:38
  • ERROR: syntax error at or near ":=" LINE 11: t8 as (SELECT @tc := SUM(total_count) as total_day_count fro... Commented Feb 25, 2020 at 10:38
  • Is my syntax wrong?? Commented Feb 25, 2020 at 10:38
  • SELECT @tc := SUM(total_count) is invalid standard SQL and invalid in Postgres - what is that supposed to do? Commented Feb 25, 2020 at 11:26

1 Answer 1

1

Here is an attempt to translate your function to correct code:

CREATE OR REPLACE Function dailyyyy_volation_routine(
   _ispeed_count integer,
   _rlvd_count integer,
   _stopline_count integer,
   _wrongdir_count integer,
   _wronglane_count integer,
   _zebracross_count integer,
_total_count integer) RETURNS void AS
$$INSERT INTO temp_daily_stats(
   ispeed_count,
   rlvd_count,
   stopline_count,
   wrongdir_count,
   wronglane_count,
   zebracross_count,
   total_count
)
SELECT SUM(ispeed_count),
       SUM(rlvd_count),
       SUM(stop_line_count),
       SUM(wrong_dir_count),
       SUM(wrong_lane_count),
       SUM(zebra_cross_count),
       SUM(total_count)
FROM temp_hourly_stats$$ 
LANGUAGE SQL;
Sign up to request clarification or add additional context in comments.

14 Comments

CREATE OR REPLACE Function dailyyyy_volation_routine(ispeed_count integer) RETURNS void AS $$ BEGIN INSERT INTO temp_daily_stats(ispeed_count) SELECT SUM(ispeed_count) as ispeed_count from temp_hourly_stats; END $$ LANGUAGE SQL;
I did the same but its saying syntax error near Insert or at insert
I am using latest version of postgre
Ah-hah, but you never said in your answer that the function has a parameter of the same name (which is bad practice). In that case, you have to qualify the column. I'll update the answer.
I doubt that that is valid SQL anywhere. Anyway, I have attempted to translate your code into valid SQL in my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.