Guest User

Untitled

a guest
Jul 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION test(uuidin text)
  2. RETURNS TABLE(uuid integer, name text, business_account_id integer, upline_id integer, user_username text, grade_name text, grade_background_image text) AS $BODY$
  3. BEGIN
  4. RETURN QUERY
  5. WITH RECURSIVE subordinates AS (SELECT b.uuid , b.name, b.business_account_id, b.upline_id,
  6. b.user_username, g.name AS grade_name, g.background_image AS grade_background_image, (SELECT SUM(amount) FROM user_pin_transactions WHERE business_account_uuid = b.uuid) AS omzet FROM business_accounts b
  7. INNER JOIN grades g ON id = b.grade_id
  8. WHERE b.uuid = uuidin
  9. UNION SELECT db.uuid, db.name, db.business_account_id, db.upline_id, db.user_username, dg.name AS grade_name, dg.background_image AS grade_background_image,
  10. (SELECT SUM(amount) FROM user_pin_transactions WHERE business_account_uuid = db.uuid) AS omzet
  11. FROM business_accounts db
  12. INNER JOIN grades dg ON id = db.grade_id
  13. INNER JOIN subordinates s ON s.business_account_id = db.upline_id) SELECT *, (SELECT SUM(omzet) FROM subordinates) AS omzet_group FROM subordinates;
  14. END;
  15. $BODY$ LANGUAGE plpgsql;
Add Comment
Please, Sign In to add comment