1. 程式人生 > >postgre——case、union、小計總計(GROUP BY ROLLUP)寫法

postgre——case、union、小計總計(GROUP BY ROLLUP)寫法

//將幾張不同表結構的資料全部合併在一起,使用了case、union,同時實現小計總計
//小計總計使用的 GROUP BY ROLLUP函式,GROUP BY ROLLUP使用雙括號將欄位括起來,是將這些欄位分為一組,只會產生一個總計
select case when t_all.temp_comm is null then '總計' else t_all.temp_comm end as temp_comm,
commodity_name,guige,t_bi_commbaseinfo.comm_type_id,comm_type_name,t_all.temp_bill,t_all.temp_date,
sum(temp_inCount) as temp_inCount,temp_inPrice as temp_inPrice,
sum(temp_inCount*temp_inPrice) as temp_inTotal,
sum(temp_inCount*temp_inPrice*(1-temp_inTax)) as temp_inTatolTax,
sum(temp_inCount*temp_inTax*temp_inPrice) as temp_inTaxTatal,
sum(temp_outCount) as temp_outCount,temp_outPrice as temp_outPrice,
sum(temp_outCount*temp_outPrice) as temp_outTotal,
sum(temp_retailPrice*temp_inCount) as temp_inSaleTotal,
sum((temp_retailPrice-temp_inPrice)*temp_inCount) as temp_outDivInTotal,
sum(temp_inCount-temp_outCount) as temp_outDivIn,
sum(temp_inCount*temp_inPrice) as temp_divTotal
from ((SELECT receipt_num AS temp_bill,shd.commodity_id as temp_comm,sh.check_date AS temp_date,
				(CASE WHEN shd.order_real_inprice IS NOT NULL THEN shd.order_real_inprice ELSE 0 END) AS temp_inPrice,0 as temp_outPrice,
				(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
				(CASE WHEN shd.in_tax IS NOT NULL THEN shd.in_tax ELSE 0 END) AS temp_inTax,
				(case when shd.real_rece_count is not null then shd.real_rece_count else 0 end) as temp_inCount,0 as temp_outCount
			FROM t_bi_receivedetail AS shd LEFT JOIN t_bi_receive AS sh ON sh.receipt_id = shd.receipt_num
			LEFT JOIN t_bi_commbaseinfo on shd.commodity_id=t_bi_commbaseinfo.commodity_id
			WHERE sh.check_date BETWEEN '2015/7/1' AND '2016/12/10' AND receipt_status = '2'
		)UNION(SELECT thd.return_com_receipt_num AS temp_bill,thd.commodity_id as temp_comm,th.check_date AS temp_date,
					0 AS temp_inPrice,(CASE WHEN thd.return_com_price IS NOT NULL THEN thd.return_com_price ELSE 0 END) AS temp_outPrice,
					(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
					0 AS temp_inTax,0 as temp_inCount,(case when thd.return_com_count is not null then return_com_count else 0 end) as temp_outCount  
				FROM t_bi_returncommdetail AS thd LEFT JOIN t_bi_returncomm AS th ON thd.return_com_receipt_num = th.return_com_receipt_num
				LEFT JOIN t_bi_commbaseinfo on thd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE th.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND th.status = '2')
		UNION(SELECT lyd.entertain_recei_num AS temp_bill,lyd.commodity_id as temp_comm,ly.check_date AS temp_date,0 as temp_inPrice,
					(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
					0 as temp_outPrice,0 as temp_inTax,0 as temp_inCount,0 as temp_outCount
				FROM t_bi_entaindetail AS lyd LEFT JOIN t_bi_entainment AS ly ON lyd.entertain_recei_num = ly.entertain_recei_num
				LEFT JOIN t_bi_commbaseinfo on lyd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE ly.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND ly.status IN ('2', '3')
			)UNION(SELECT bsd.break_rece_num AS temp_bill,bsd.commodity_id as temp_comm,bs.check_date AS temp_date,0 as temp_inPrice,0 as temp_outPrice,
					(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
					0 as temp_inTax,0 as temp_inCount,0 as temp_outCount
				FROM t_bi_breakdetail AS bsd LEFT JOIN t_bi_break AS bs ON bsd.break_rece_num = bs.break_rece_num
				LEFT JOIN t_bi_commbaseinfo on bsd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE bs.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND bs.status = '2'
			)UNION(SELECT fcd.back_reciept_num AS temp_bill,fcd.commodity_id as temp_comm,fc.check_date AS temp_date,0 as temp_inPrice,0 as temp_outPrice,
					(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
					0 as temp_inTax,0 as temp_inCount,0 as temp_outCount
				FROM t_bi_returnwaredetail AS fcd LEFT JOIN t_bi_returnwarehose AS fc ON fcd.back_reciept_num = fc.back_reciept_num
				LEFT JOIN t_bi_commbaseinfo on fcd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE fc.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND fc.status IN ('2', '3', '4')
			)UNION(SELECT zdd.deliever_rece_num AS temp_bill,zdd.commodity_id as temp_comm,zd.check_date AS temp_date,0 as temp_inPrice,0 as temp_outPrice,
					(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
					0 as temp_inTax,0 as temp_inCount,0 as temp_outCount
				FROM t_bi_deliverdetail AS zdd LEFT JOIN t_bi_deliver AS zd ON zdd.deliever_rece_num = zd.deliever_rece_num
				LEFT JOIN t_bi_commbaseinfo on zdd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE zd.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND zd.status IN ('5', '3', '4')
			)UNION(SELECT tgd.group_reciept AS temp_bill,tgd.commodity_id as temp_comm,tg.check_date AS temp_date,0 as temp_inPrice,(case when tgd.groupbuy_price is not null then tgd.groupbuy_price else 0 end) as temp_outPrice,
				(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,	
				0 as temp_inTax,0 as temp_inCount,(case when tgd.groupbuy_count is not null then tgd.groupbuy_count else 0 end) as temp_outCount
				FROM t_bi_hqgroupdetail AS tgd LEFT JOIN t_bi_hqgroup AS tg ON tgd.group_reciept = tg.group_reciept
				LEFT JOIN t_bi_commbaseinfo on tgd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE tg.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND tg.status IN ('2', '3', '4')
			)UNION(SELECT ttgd.group_return_reciept AS temp_bill,ttgd.commodity_id as temp_comm,ttg.check_date AS temp_date,(case when ttgd.groupbuy_price is not null then ttgd.groupbuy_price else 0 end) as temp_inPrice,
				(case when t_bi_commbaseinfo.retail_price is not null then t_bi_commbaseinfo.retail_price else 0 end) AS temp_retailPrice,
				0 as temp_outPrice,0 as temp_inTax,(case when ttgd.groupbuy_count is not null then ttgd.groupbuy_count else 0 end) as temp_inCount,
				0 as temp_outCount
				FROM t_bi_returnhqgroupdetail AS ttgd LEFT JOIN t_bi_returnhqgroup AS ttg ON ttg.group_return_reciept = ttgd.group_return_reciept
				LEFT JOIN t_bi_commbaseinfo on ttgd.commodity_id=t_bi_commbaseinfo.commodity_id
				WHERE ttg.check_date BETWEEN '2015/6/1' AND '2016/12/10' AND ttg.status IN ('2', '3', '4')) 
) as t_all
LEFT JOIN t_bi_commbaseinfo on t_bi_commbaseinfo.commodity_id=t_all.temp_comm
LEFT JOIN t_bi_commtype on t_bi_commtype.comm_type_id=t_bi_commbaseinfo.comm_type_id
GROUP BY ROLLUP((t_all.temp_comm,commodity_name,guige,t_bi_commbaseinfo.comm_type_id,comm_type_name,t_all.temp_bill,t_all.temp_date,
 temp_inPrice,temp_outPrice))
order by temp_comm

相關推薦

postgre——caseunion總計GROUP BY ROLLUP寫法

//將幾張不同表結構的資料全部合併在一起,使用了case、union,同時實現小計總計//小計總計使用的 GROUP BY ROLLUP函式,GROUP BY ROLLUP使用雙括號將欄位括起來,是將這些欄位分為一組,只會產生一個總計select case when t_a

GridView自動求和求平均值

轉自清清月兒gridview72絕技,略有改動 前臺:唯一的花頭就是設定ShowFooter="True" private double sum = 0;//取指定列的資料和,你要根據具體情況對待可能你要處理的是int protected void GridView1_R

46求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C

closed else while spl 判斷語句 stat 條件 執行 ret 思路:循環或者遞歸都有個結束條件和執行條件。用&&短路與代替。 //短路與&&;就是只有前一個條件滿足才可以去判斷第二個條件。 //遞歸的出口

namespacestructenumunionstringday01

mic str 歷史 數組 hello 科學 練習 line 空字符串 一 C++概述 1 C++歷史背景 1)C++的江湖地位 jave C C++ C# python 2)C++之父:Bjarne Stroustrup(1950--) 1979,Cpre,為C語言

結構體聯合體類:structunionclass

                   區別:類的成員預設是私有的private                 &n

【類和物件】求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C

題目描述: 求1+2+3+...+n,要求不能使用乘除法(數學公式)、for、while、if、else、switch、case等關鍵字及條件判斷語句(A?B:C). 分析:這道題雖然常用的方式都給禁掉了,但是可以使用我們c++學過的類和物件,通過使用類和靜態來完成。常規情況下,從1+到n,

劍指offer-求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C

題目如下: 求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字及條件判斷語句(A?B:C) 本題考的知識點就兩點 && 運算子 false && true =>

Leetcode演算法——64路徑之和minimum path sum

給定一個 m*n 的非負整數矩陣,找到一條路徑,從左上角到右下角,要求這條路徑上的所有陣列之和最小。 備註:每次只能向下或向右移動。 Input: [ [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because

題目描述 求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C

//求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字 //及條件判斷語句(A?B:C)。 public class Solution { public int Sum_Solution(int n) { //if

劍指offer---47求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字及條件判斷語句(A?B:C)。 public class Solution { public int Sum_Solution(int n) { // if(

C++實現:求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C

程式碼: class Solution { public: class Sum { public: Sum() { s_count++; s_sum += s_count;

求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C。Java實現

前幾天跟同學交流聽說一個比較有意思的oj題目,具體描述如下: 題目描述: 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字及條件判斷語句(A?B:C)。 輸入: 輸入可能包含多個測試樣例。 對於每個測試案例,輸

的k個數第k的數利用快排,堆排序

快排:(若求top M個元素,則只需把下面的程式裡的k換乘n-M即可) //利用快排求最小的k個數,第k小的數 void GetLeastKNum(int *input,int n,int *output,int k) { if(input==NU

求1+2+3+...+n,要求不能使用乘除法forwhileifelseswitchcase等關鍵字及條件判斷語句A?B:C劍指offer

今天在牛客網上遇到這樣一道題目,求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字及條件判斷語句(A?B:C) 一般大家在看到這種題目的時候是完全沒有頭緒的,但是我們可以從多方面進行思考,首先我們應該想

購物車實現清空總計

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script src="../js/angular.min.js"

關於hive 子查詢union left join

建表語句: create table  tb_in_base (    id  bigint,    devid bigint,     devname string ) partitioned by (j

java題目:求 1+2+...+n,要求不能用除法 for while if else switch case 等關鍵字及條件判斷語句 A?B:C

這是劍指offer的46道面試題,由於面試官諸多條件的限制,本文采用遞迴的手法完成程式碼 public class Sum { public static void main(String[] args) { System.out

【離散數學】最大元素元素極大元素元素上界下界上界上確界最大下界下确界

設(A, ≤)是一偏序集合,B是A的子集。 最大元素、最小元素:(1)元素b∈B是B的最大元素,如果對每一元素x∈B,x≤b(2)元素b∈B是B的最小元素,如果對每一元素x∈B,b≤x即:對於每一

最長連續公共子串最長公共子串可以非連續最長回文串連續最長回文串可以不連續最長遞增數組的求解

鑲嵌 wid 方法 數量 子串 進行 遞增 動態 動態規劃 問題:最長連續公共子串、最長公共子串(可以非連續)、最長回文串(連續)、最長回文串(可以不連續)、最長遞增數組、長方形鑲嵌最多的求解 方法:上述問題有相似性,都可以采用動態規劃進行求解。 (1)最長連續公共子串:

what's the python之可叠代對象叠代器與生成器附面試題

urn for循環 生成器 出現 擁有 移動 iterable 文本 協議 可叠代對象 字符串、列表、元祖、集合、字典都是可叠代的,數字是不可叠代的。(可以用for循環遍歷取出內部元素的就是可叠代的) 如何查看一個變量是否為可叠代: from collections im