2

I have two separate PostgreSQL count queries that I would like to output as one combined report. I have done a little bit of research into this and found that it could be done through a stored procedure, but I am not sure how I should go about doing this (I'm fairly new to Postgres programming).

Both of the queries are returning counts.

Any insight into this would be much appreciated!

1
  • Do you want each count returned as a column, or as a row? Commented Sep 7, 2011 at 20:14

2 Answers 2

5

You don't even need a stored procedure for this. You can just make one big query:

SELECT a.a_count, b.b_count FROM
  (SELECT COUNT(*) a_count FROM table_a) AS a,
  (SELECT COUNT(*) b_count FROM table_b) AS b;
Sign up to request clarification or add additional context in comments.

Comments

0

I don't believe PostgreSQL has stored procedures, only functions. However, you could do what you're talking about with a FUNCTION.

CREATE FUNCTION getQtyOrders(customerID int) RETURNS int AS $$
DECLARE
qty int;
BEGIN
SELECT COUNT(*) INTO qty
FROM Orders
WHERE accnum = customerID;
RETURN qty;
END;

Comments

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.