我在SQL Server中遇到条件排序问题。我有一个冗长的程序,最终会生成一份杂志订阅列表,可以续订。这是最终结果表的基本结构:
create table #renewals
(
product_no integer NULL,
quantity integer NULL,
order_date datetime NULL,
order_no integer NULL,
customer_no varchar(10) NULL,
description varchar(50) NULL,
first_name varchar(30) NULL,
middle_name varchar(30) NULL,
last_name varchar(30) NULL,
salute varchar(5) NULL,
acct_no char(8) NULL,
contact_type varchar(8) NULL,
contact_no integer NULL,
line_item int,
edition_code char(3) NULL
)
问题在于我想要实现的多级排序。最后,我需要按edition_code
ASC排序的这些数据,这很简单。
但是,某些版本的附加产品需要插入正确的edition_code
下方。这是我遇到问题的地方。常规版代码的示例是002
或014
。附加组件版本代码为300
,302
等。如果我只按edition_code
排序,则所有附加组件版本都会移到列表底部。
以下是结果集的纲要:
product_no -- This is a different number than edition_code, but same principal
quantity -- self-explanatory
order_date -- self-explanatory
order_no -- For each unique result set, this number is the same
customer_no -- this is unique for each customer and helps to bundle main editions with their add-ons
description -- title of edition
first (through) last_name -- self-explanatory
salute -- self-explanatory
acct_no -- For each result set, this number is the same because all the customers will belong to the same account (think multiple doctors subscribing to a magazine but all work out of the same hospital)
contact_type -- PK
contact_no -- PK
line_item -- line number when entered on order (this does not help with sorting at all)
edition_code -- Unique code for an edition of our product
以下是我期待的结果:
Customer_1 | Edition_1
Customer_2 | Edition_1
Customer_2 | Add-On_1
Customer_2 | Add-On_2
Customer_9 | Edition_1
Customer_6 | Edition_2
Customer_5 | Edition_2
Customer_5 | Add-On_2
Customer_3 | Edition_3
我能得到的最接近的是简单地按customer_no
排序,这会将附加组件与他们的版本放在一起(你不能在不订购主版的情况下订购附加组件),但这并不是按主要版本排序的。版本代码,这是整体(主要)排序应该完成的。
这是产生结果的过程,不确定它将如何帮助:
CREATE TABLE #renewals
(
product_no integer NULL,
quantity integer NULL,
order_date datetime NULL,
order_no integer NULL,
customer_no varchar(10) NULL,
description varchar(50) NULL,
first_name varchar(30) NULL,
middle_name varchar(30) NULL,
last_name varchar(30) NULL,
salute varchar(5) NULL,
acct_no char(8) NULL,
contact_type varchar(8) NULL,
contact_no integer NULL,
line_item int,
edition_code char(3) NULL
)
INSERT INTO #renewals
(
product_no,
quantity,
order_date,
order_no,
customer_no,
description,
first_name,
middle_name,
last_name,
salute,
acct_no,
contact_type,
contact_no ,
line_item,
edition_code
)
SELECT
product.product_no,
order_table_1.quantity,
order_table_1.order_date,
order_table_1.order_no,
order_table_1.customer_no,
product.description,
isnull(customer_table_1.first_name, ''),
isnull(customer_table_1.middle_name, ''),
isnull(customer_table_1.last_name, ''),
isnull(customer_table_1.salute, ''),
order_table_1.acct_no,
'',
-1,
line_item_no,
order_table_1.edition_code
FROM order_table_1
LEFT OUTER JOIN product ON order_table_1.edition_code = product.edition_code
LEFT OUTER JOIN customer_table_1 ON order_table_1.customer_no = customer_table_1.customer_no AND order_table_1.acct_no = customer_table_1.acct
WHERE
(order_table_1.acct_no = @acct) AND
(order_table_1.school_year = @school_year) AND
(order_table_1.edition_code <> '065') AND
(order_table_1.status_code in ('E','A','F')) AND
(isnull(order_table_1.renewed,'N') <> 'Y')
UPDATE o
SET o.first_name = isnull(tc.first_name, ''),
o.middle_name = isnull(tc.middle_name, ''),
o.last_name = isnull(tc.last_name, ''),
o.salute = isnull(tc.salute, ''),
o.contact_type = tc.contact_type,
o.contact_no = tc.contact_no
FROM #renewals o, accounts ta, contacts tc
WHERE
(o.acct_no = ta.acct) AND
(ta.acct_type = tc.acct_type) AND
(ta.acct_no = tc.acct_no) AND
(o.customer_no = tc.customer_no) AND
(o.customer_no is not null) AND
(o.customer_no <> '') AND
(o.customer_no not like '999999999%')
SELECT
cast(o.product_no as int) as 'product_no',
o.quantity,
o.order_date,
o.order_no,
o.customer_no,
o.description,
o.first_name,
o.middle_name,
o.last_name,
o.salute,
o.acct_no,
o.contact_type,
o.contact_no,
o.line_item,
o.edition_code
FROM #renewals o
ORDER BY o.edition_code