SQL Server:具有多列的条件排序
我在SQL Server中遇到条件排序问题。我有一个漫长的过程,最终会产生一个杂志订阅列表,供续订。这是最终结果表的基本结构:SQL Server:具有多列的条件排序,sql,sql-server,sorting,Sql,Sql Server,Sorting,我在SQL Server中遇到条件排序问题。我有一个漫长的过程,最终会产生一个杂志订阅列表,供续订。这是最终结果表的基本结构: create table #renewals ( product_no integer NULL, quantity integer NULL, order_date datetime NULL, order_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
)
问题在于我试图实现的多层次分类。最后,我需要按照足够简单的版本代码ASC对这些数据进行排序
但是,有些版本有附加产品,需要将其插入正确的版本代码下面。这就是我遇到问题的地方。常规版本代码的示例为002或014。附加版本代码是300、302等等。如果我只按版本代码排序,那么所有附加版本都会移到列表的底部
以下是结果集的摘要:
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
我能得到的最接近的方法是简单地按客户编号排序,这将把附加组件与它们的版本放在一起。如果不订购主版本,您不能订购附加组件,但这不会按主版本代码排序,这是整个主排序应该完成的
这是产生结果的过程,但不确定它会有什么帮助:
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
你不能把你没有的东西分类。你需要一个版本代码表,插件代码对。然后加入到那张桌子上
#renewals.edition_code in (X.edition_code, X.addon_code)
...
order by X.edition_code, X.addon_code
我做了一些假设:两个代码集不重叠,版本代码总是小于插件代码,并且您希望插件按其代码排序。有时在这种情况下,需要第三列来表示排序顺序 MS SQL Server 2014管理研究一些实际查询或至少排序表达式的示例,会很有帮助。@Dave添加了整个SP,如果您认为它会有所帮助的话-25年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表样式被正确的ANSI联接语法所取代,并且在您的预期结果中不鼓励使用它,我如何区分Add-On-2位于客户2的Edition-1之下,然而,对于客户5,它应该位于版本2的下方?我可以尝试使用order_no,但是客户可以一次订购多个版本吗?用这些版本创建一个表并不困难。但是,我不清楚如何使用新表和您发布的示例,您能澄清一下吗?