SQL— 创建计算字段(以 PostgreSQL为例)

来源:互联网 发布:金山数据恢复 编辑:程序博客网 时间:2024/06/06 06:46

拼接字段 — 生成一个供应商报表,返回供应商名称和地址

  • SQL 语句
select vend_name || '(' || vend_country || ')'from vendorsorder by vend_name
  • 输出
------------------------------------Bear Emporium          (USA        )Bear R Us              (USA        )Jouets et ours         (France     )
  • 去除中间空格的 SQL 语句
select RTRIM(vend_name) || '(' || RTRIM(vend_country) || ')'from vendorsorder by vend_name
  • 去除中间空格的输出
------------------------------------Bear Emporium (USA)Bear R Us (USA)Jouets etours (France)

执行算术计算 — 汇总物品的价格(单价乘以订购数量)

  • SQL 语句
select prod_id,quantity,item_price,       quantity*item_price as expanded_pricefrom orderItemswhere order_num = 20008;
  • 输出
prod_id     quantity        item_price      expanded_price-------     --------        ----------      --------------RGAN01      5               4.9900          24.9500BR03        5               11.9900         59.9500
0 0
原创粉丝点击