# 2023/02/17\_postgresSql-function

```sql
CREATE OR REPLACE FUNCTION query_result(IN inputDate text) RETURNS TABLE 
(aa varchar(64),ss varchar(64),n timestamp)AS $$

declare counter integer:=1;
declare startTime timestamp:=to_timestamp(inputDate, 'yyyy-MM-dd hh24:mi:ss');
declare days integer:=(select extract (day from date_trunc('month', startTime)+ interval '1 month'- interval '1 day'));

begin
	
	while counter<days loop
        counter := counter + 1;
       	return query
    	select  distinct xx,xx,xx from xxx   where  condition1 and
    	and xxx >=to_timestamp(to_char(startTime,'yyyy-mm-')||to_char(counter,'00') ||' 00:00:00','yyyy-MM-dd hh24:mi:ss' )
    	and xxx <=to_timestamp(to_char(startTime,'yyyy-mm-')||to_char(counter,'00') ||' 23:59:59','yyyy-MM-dd hh24:mi:ss' );
    end loop;
END;
$$ LANGUAGE plpgsql STABLE;

SELECT * FROM query_result('2023-02');              

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://note.lonelylty.com/coding/postgressql-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
