目前select訂單inner join銷貨單
因一訂單會對應多筆銷貨單
請問各位大大有法把目前select出來的結果(上圖)變成(下圖)謝謝
<pre class="c" name="code">
-- 自己掰測試資料
CREATE TABLE ithelp0520a(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_no CHAR(7) NOT NULL,
order_qty INT UNSIGNED NOT NULL,
UNIQUE (order_no)
);
INSERT INTO ithelp0520a(order_no, order_qty) VALUES
("001-001", 1000),
("001-002", 1200);
CREATE TABLE ithelp0520b(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_no CHAR(7) NOT NULL,
sale_no CHAR(7) NOT NULL,
sale_qty INT UNSIGNED,
FOREIGN KEY (order_no) REFERENCES ithelp0520a(order_no)
);
INSERT INTO ithelp0520b(order_no, sale_no, sale_qty) VALUES
("001-001", "002-001", 300),
("001-001", "002-002", 200),
("001-001", "002-003", 200),
("001-002", "002-004", 200),
("001-002", "002-004", 500);
--
SELECT a.order_no
, a.order_qty
, b.sale_no
, b.sale_qty
, (a.order_qty - c.sqty ) AS "non_delivery"
FROM ithelp0520a a
JOIN ithelp0520b b
ON a.order_no = b.order_no
JOIN (SELECT order_no, SUM(sale_qty) AS sqty
FROM ithelp0520b
GROUP BY order_no) c
ON a.order_no = c.order_no
ORDER BY a.order_no, b.sale_no;
+----------+-----------+---------+----------+--------------+
| order_no | order_qty | sale_no | sale_qty | non_delivery |
+----------+-----------+---------+----------+--------------+
| 001-001 | 1000 | 002-001 | 300 | 300 |
| 001-001 | 1000 | 002-002 | 200 | 300 |
| 001-001 | 1000 | 002-003 | 200 | 300 |
| 001-002 | 1200 | 002-004 | 200 | 500 |
| 001-002 | 1200 | 002-004 | 500 | 500 |
+----------+-----------+---------+----------+--------------+
-- 使用補助變數來變化
SELECT e.order_no, e.order_qty, e.sale_no, e.sale_qty, e.non_delivery
FROM (SELECT IF(a.order_no = @lorder, "", a.order_no) AS "order_no"
, IF(a.order_qty = @lqty, "" ,a.order_qty) AS "order_qty"
, b.sale_no
, b.sale_qty
, IF((a.order_qty - c.sqty) = @lnon, "", (a.order_qty - c.sqty)) AS "non_delivery"
, @lorder := a.order_no
, @lqty := a.order_qty
, @lnon := (a.order_qty - c.sqty)
FROM ithelp0520a a,
ithelp0520b b,
(SELECT order_no
, SUM(sale_qty) AS sqty
FROM ithelp0520b
GROUP BY order_no) c,
(SELECT @lorder := NULL
, @lqty := NULL
, @lnon := NULL) d
WHERE a.order_no = b.order_no
AND a.order_no = c.order_no
ORDER BY a.order_no, b.sale_no) e;
+----------+-----------+---------+----------+--------------+
| order_no | order_qty | sale_no | sale_qty | non_delivery |
+----------+-----------+---------+----------+--------------+
| 001-001 | 1000 | 002-001 | 300 | 300 |
| | | 002-002 | 200 | |
| | | 002-003 | 200 | |
| 001-002 | 1200 | 002-004 | 200 | 500 |
| | | 002-004 | 500 | |
+----------+-----------+---------+----------+--------------+
5 rows in set (0.00 sec)
大大不行耶><我用的是MSSQL
當然不是百分之百copy過去啊,MySQL 用 IF()函數,你 SQL Server
就用對應與語法及函數處理.
還有,不要說大大不行,這樣不太好....
IF 改寫成 case when 的寫法啊
hitomitanaka提到:
不要說大大不行
更不要說太大不行...
1000字很容易就不夠用了.分兩段吧.
<pre class="c" name="code">
-- 改用 CASE 的方式
SELECT e.order_no, e.order_qty, e.sale_no, e.sale_qty, e.non_delivery
FROM (SELECT CASE WHEN a.order_no = @lorder
THEN ""
ELSE a.order_no
END AS "order_no"
, CASE WHEN a.order_qty = @lqty
THEN ""
ELSE a.order_qty
END AS "order_qty"
, b.sale_no
, b.sale_qty
, CASE WHEN(a.order_qty - c.sqty) = @lnon
THEN ""
ELSE (a.order_qty - c.sqty)
END AS "non_delivery"
, @lorder := a.order_no
, @lqty := a.order_qty
, @lnon := (a.order_qty - c.sqty)
<pre class="c" name="code">
FROM ithelp0520a a,
ithelp0520b b,
(SELECT order_no
, SUM(sale_qty) AS sqty
FROM ithelp0520b
GROUP BY order_no) c,
(SELECT @lorder := NULL
, @lqty := NULL
, @lnon := NULL) d
WHERE a.order_no = b.order_no
AND a.order_no = c.order_no
ORDER BY a.order_no, b.sale_no) e;
+----------+-----------+---------+----------+--------------+
| order_no | order_qty | sale_no | sale_qty | non_delivery |
+----------+-----------+---------+----------+--------------+
| 001-001 | 1000 | 002-001 | 300 | 300 |
| | | 002-002 | 200 | |
| | | 002-003 | 200 | |
| 001-002 | 1200 | 002-004 | 200 | 500 |
| | | 002-004 | 500 | |
+----------+-----------+---------+----------+--------------+
不好意思 超行的大大
執行出現必須宣告純量變數 "@lorder"。
(SELECT @lorder := NULL
, @lqty := NULL
, @lnon := NULL) d
我是在這裡宣告的,SQL Server 若無法這樣的話,你可以在這道指令之前, 先行宣告這3個變數,然後將這部份拿掉,再執行.
超行的大大
我認識一個超級行者,他是一隻...猢孫...
幫忙回答,一下被說超行,一下被說那個,對方還戴面罩,
改天網聚想哈拉一下都沒機會.或是作個實驗,純粹是學術性的.
小雨大的圖真多
行行行的大大們
在最前面有作DECLARE的變數宣告
但結果還是一樣 沒變空白
拜託一下吧! 自己也要有學習研究精神, MySQL轉到SQL Server有什麼困難?
另外, SQL Server撈資料後, 再用Excel或其他報表工具做出的報表, 怎麼會要求撈出的資料就要構成報表表達方式? 這不是自找苦吃?
你把你的code貼上來看看吧.
不然大家很難去猜你哪裡少打了,或是哪裡需要調整一下.
我發覺提問的人,很多都惜字如金啊.
hitomitanaka提到:
惜字如金
我可是很聒噪的,一發問題一有回覆立刻update
hitomitanaka提到:
字如金
真的嗎??那我在這說過那麼多話,那時可以折現??....
折現喔...那可能要排在名嘴後面...
感謝各位大大那麼多建議
目前自行修改的
<pre class="c" name="code">DECLARE @A1 nvarchar(20) , @A2 nvarchar(20) , A3 nvarchar(20)
SELECT CASE WHEN dbo.訂單明細.訂單編號 = @A1 THEN '' ELSE dbo.訂單明細.訂單編號 END ,
CASE WHEN dbo.訂單明細.訂單數量 = @A2 THEN '' ELSE dbo.訂單明細.訂單數量 END ,
dbo.銷貨明細.銷貨編號 ,
dbo.銷貨明細.銷貨數量 ,
CASE WHEN dbo.訂單明細.未交數量 = @A3 THEN '' ELSE dbo.訂單明細.未交數量 END
FROM dbo.訂單明細
INNER JOIN
dbo.銷貨明細
ON dbo.訂單明細.訂單編號 = dbo.銷貨明細.訂單編號
那你有發覺有少掉哪裡了呢?
沒擺
SET @A1 = dbo.訂單明細.訂單編號
SET @A2 = dbo.訂單明細.訂單數量
SET @A3 = dbo.訂單明細.未交數量
你要注意一下資料型態,數字欄位的,你用 VARCHAR型態去接,再來作後續比較,雖然有些系統會作型別轉換,但是總是不太好,有時候會凸鎚的;
另外最好給初始值,像我是給 NULL.
感謝大大提醒,因為我有試用給NULL,但跑出來結果還是一樣沒變
你給NULL,那次有沒有 使用 SET @A1 = dbo.訂單明細.訂單編號
??
我看到數字要丟到VARCHAR,然後進行比較,就覺得不知道要怎樣說才好了.
你的數量是用 VARCHAR 去存的嗎??