如何选择最大和最小

时间:2015-05-29 04:39:47

标签: php postgresql

我有脚本查询选择这样。我想从查询选择中搜索max max和min'jumlah'。

SELECT 
     "B"."company" AS "B__company", 
     "User"."company" AS "User__company", 
     "Transaction"."create_date" AS "Transaction__create_date",
     extract(day from "Transaction"."create_date"),
     sum(cast("PLNPostpaid"."jumlahrek" as integer)) as jumlah,
     sum("Transaction"."price_buy") as jumbuy,
     sum(("Transaction"."price_sell")-("Transaction"."price_buy")) as admin


FROM "public"."transactions" AS "Transaction" 
     LEFT OUTER JOIN "public"."users" AS "User" ON ("User"."id" = "Transaction"."user_id") 
     FULL OUTER JOIN "public"."users" AS "B" ON ("B"."id" = "User"."bank") 
     LEFT JOIN "public"."inboxes" AS "Inbox" ON ("Inbox"."id" = "Transaction"."inbox_id")
     LEFT JOIN "public"."pln_postpaids" AS "PLNPostpaid" ON ("Inbox"."id" = "PLNPostpaid"."inbox_id")


WHERE "Transaction"."create_date" >= '01-May-2015 00:00:00' AND "Transaction"."create_date" <= '31-May-2015 23:59:59' 
      AND "Transaction"."product_id"=100


GROUP BY extract(day from "Transaction"."create_date"), "Transaction"."create_date", "B"."company", "User"."company"

我想实现到php。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

  1. 我认为你需要一个子查询来选择min和max。 例如,
  2. WITH all_data AS(         - 你的初始请求在这里     )     选择MIN(jumlah)为min_jumlah,MAX(jumlah)为max_jumlah     来自all_data

    1. 目前还不清楚代码所代表的语法。如果你从php编译查询,那么你不应该使用这样的引号。包含查询的整个字符串应该是双引号,但你不需要这些双引号。