我正在寻找一种方法来排除我用作报告中显示的排序键的列。在Oracle中我可以简单地说COLUMN SORTKEY NOPRINT
,但我还没能找到MySQL中的等价物。据我所知,我需要排序键,以便行以正确的顺序显示(借记,贷记,然后是总计)。
我正在使用的查询如下(它非常大):
SQL
SELECT CASE
WHEN t.transaction_account = '111-111-111-111' THEN 'Debit'
WHEN t.transaction_account = '222-222-222-222' THEN 'Credit'
END AS "Transaction",
CASE
WHEN t.transaction_account = '111-111-111-111' THEN 1
WHEN t.transaction_account = '222-222-222-222' THEN 2
END AS "SORTKEY",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 1 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Jan",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 2 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Feb",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 3 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Mar",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('1','2','3') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ1",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 4 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Apr",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 5 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "May",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 6 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Jun",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('4','5','6') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ2",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 7 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Jul",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 8 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Aug",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 9 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Sep",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('7','8','9') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ3",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 10 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Oct",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 11 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Nov",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 12 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Dec",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('10','11','12') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ4",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "YTD"
FROM transaction t INNER JOIN common_lookup cl
ON t.transaction_type = cl.common_lookup_id
WHERE cl.common_lookup_table = 'TRANSACTION'
AND cl.common_lookup_column = 'TRANSACTION_TYPE'
GROUP BY CASE
WHEN t.transaction_account = '111-111-111-111' THEN 'Debit'
WHEN t.transaction_account = '222-222-222-222' THEN 'Credit'
END
, CASE
WHEN t.transaction_account = '111-111-111-111' THEN 1
WHEN t.transaction_account = '222-222-222-222' THEN 2
END
UNION ALL
SELECT 'Total' AS "Transaction",
3 AS "SORTKEY",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 1 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Jan",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 2 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Feb",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 3 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Mar",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('1','2','3') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ1",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 4 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Apr",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 5 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "May",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 6 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Jun",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('4','5','6') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ2",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 7 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Jul",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 8 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Aug",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 9 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Sep",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('7','8','9') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ3",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 10 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Oct",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 11 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Nov",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) = 12 AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "Dec",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(MONTH FROM transaction_date) IN ('10','11','12') AND
EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "FQ4",
LPAD(FORMAT(SUM(
CASE
WHEN EXTRACT(YEAR FROM transaction_date) = 2009
THEN
CASE
WHEN cl.common_lookup_type = 'DEBIT'
THEN t.transaction_amount
ELSE t.transaction_amount * -1
END
END), 2),10,' ') AS "YTD"
FROM transaction t INNER JOIN common_lookup cl
ON t.transaction_type = cl.common_lookup_id
WHERE cl.common_lookup_table = 'TRANSACTION'
AND cl.common_lookup_column = 'TRANSACTION_TYPE'
GROUP BY 'Total'
ORDER BY SORTKEY;
查询显示如下:
+-------------+---------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| Transaction | SORTKEY | Jan | Feb | Mar | FQ1 | Apr | May | Jun | FQ2 | Jul | Aug | Sep | FQ3 | Oct | Nov | Dec | FQ4 | YTD |
+-------------+---------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| Debit | 1 | 2,957.40 | 4,022.70 | 5,654.04 | 12,634.14 | 4,595.10 | 2,219.64 | 1,300.62 | 8,115.36 | 2,413.62 | 2,149.68 | 2,162.40 | 6,725.70 | 3,291.30 | 3,246.78 | 2,299.14 | 8,837.22 | 36,312.42 |
| Credit | 2 | -750.48 | -992.16 | -1,437.36 | -3,180.00 | -1,217.94 | -546.96 | -302.10 | -2,067.00 | -597.84 | -537.42 | -604.20 | -1,739.46 | -829.98 | -829.98 | -594.66 | -2,254.62 | -9,241.08 |
| Total | 3 | 2,206.92 | 3,030.54 | 4,216.68 | 9,454.14 | 3,377.16 | 1,672.68 | 998.52 | 6,048.36 | 1,815.78 | 1,612.26 | 1,558.20 | 4,986.24 | 2,461.32 | 2,416.80 | 1,704.48 | 6,582.60 | 27,071.34 |
+-------------+---------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
答案 0 :(得分:1)
我不知道MySQL中允许这样做的任何功能,但我相信你可以在MySQL CASE
中使用ORDER BY
,所以你可能不需要。
/* Remove SORTKEY from SELECT */
...
ORDER BY
CASE
WHEN t.transaction_account = '111-111-111-111' THEN 1
WHEN t.transaction_account = '222-222-222-222' THEN 2
END;