6

在 SQL 中是否可以按列而不是按行排序?我不需要基本的 ORDER BY 语句,我知道它们是如何工作的(即:按 column1、column2 等排序)。

基本上试图对这样的东西进行排序:

column 1    column 2    column 3
   1            0           3 

试图对此进行排序:

column 3    column 1    column 2
   3           1           0 

这在 SQL 中甚至可能吗?最好是 t-sql 或任何将在 sql server 2005 上运行的东西

我在网上搜索了几个小时,似乎没有人想问这个问题。或者我不擅长搜索。

   ; with numbered as
   (
     select SUM(OrderReceived) as c1, SUM(OrderOnHold) as c2, SUM(OrderConfirmed) as     c3,
     row_number() over (order by employee) RecordNumber
     from( SELECT 


e.FirstName+' '+e.LastName AS Employee
,CASE WHEN oim.MilestoneID = 10 THEN 1 ELSE 0 END as OrderReceived
,CASE WHEN oim.MilestoneID = 15 THEN 1 ELSE 0 END as OrderOnHold
,CASE WHEN oim.MilestoneID = 20 THEN 1 ELSE 0 END as OrderConfirmed
FROM OrderItems oi
    JOIN Orders o on o.orderid = oi.orderid
    JOIN OrderItemMilestones oim on oim.orderid = oi.orderid and oim.orderitemid     =     oi.orderitemid
    JOIN Milestones m on m.milestoneid = oim.milestoneid
    JOIN Employees e on e.username = oim.recordedbyuser
    JOIN Clients cl on cl.clientid = o.clientid
WHERE oim.MilestoneDate Between '2012-08-01' and '2012-08-05'
    and e.terminationdate is null
),

ordered as
(
 select SUM(OrderReceived) as c1, SUM(OrderOnHold) as c2, SUM(OrderConfirmed) as c3,
         row_number() over (partition by RecordNumber
                           order by employee desc) rn
    from numbered

  unpivot (v for c in (c1, c2, c3)) u
)
select RecordNumber,
     [1] c1,
     [2] c2,
     [3] c3
 from 
 (
 select RecordNumber,
         v,
         Rn
    from ordered
  ) o
 pivot (min(employee) for Rn in ([1], [2], [3])) p
4

3 回答 3

6

这是对列进行排序的相对简单的方法。如果您首先对数据进行unpivot、排序和旋转,您将获得已排序的列。

这是带有示例的 Sql Fiddle

-- Assign arbitrary numbers to records
-- You might skip this part if you have unique column
-- in which case you should replace RecordNumber with this ID
; with numbered as
(
  select *,
         row_number() over (order by (select null)) RecordNumber
    from test
),
-- Generate order by
-- For all the columns in record.
-- Rn will always be in range
-- 1..NumberOfColumns
-- Order is done on unpivoted data
ordered as
(
  select *,
         row_number() over (partition by RecordNumber
                            order by v desc) rn
    from numbered
 -- list all the columns here
 -- v is for value
 -- c is for column
 unpivot (v for c in (c1, c2, c3)) u
)
-- Finally return the data in original layout
select RecordNumber,
       [1] c1,
       [2] c2,
       [3] c3
  from 
  (
    -- Only the columns needed by the query
    -- Pivot will not play nice even if you
    -- Select only subset of columns in
    -- outer query
    select RecordNumber,
           v,
           Rn
      from ordered
  ) o
 -- Get value for row numbers 1..NumberOfColumns
 pivot (min(v) for Rn in ([1], [2], [3])) p

您可能想要添加标题行,以便知道哪个值来自哪个列。为此,我将添加一个标识标题/行的列,合并所有o以获取相应的标题和顺序,以确保这两行保持在一起:

  (
    select RecordNumber,
           v,
           Rn,
           1 HdrRow
      from ordered
     union all
    select RecordNumber,
           c,    -- Column name is in c
           Rn,
           0 HdrRow
      from ordered
  ) o
    ...
  order by RecordNumber, HdrRow
于 2012-08-24T01:57:35.687 回答
5

你在用SELECT *吗?如果是这样,请停止这样做。如果您知道您希望它们以这种方式列出,请编写:

SELECT [column 3], [column 1], [column 2] FROM dbo.table;

如果您的意思是要在这些列中排序,我不确定这会有多大意义。但我想你可以这样做:

DECLARE @x TABLE(c1 INT, c2 INT, c3 INT);

INSERT @x(c1,c2,c3) SELECT 2,1,3
UNION ALL SELECT 3,4,5
UNION ALL SELECT 5,4,3
UNION ALL SELECT 3,1,2
UNION ALL SELECT 3,3,3
UNION ALL SELECT 3,4,3
UNION ALL SELECT 4,3,4;

SELECT c1 = CASE
  WHEN c1 >= c2 AND c1 >= c3 THEN c1
  WHEN c2 >= c1 AND c2 >= c3 THEN c2
  ELSE c3 END,
c2 = CASE 
  WHEN c1 >= c2 AND c1 >= c3 THEN 
    CASE WHEN c2 >= c3 THEN c2 ELSE c3 END
  WHEN c2 >= c1 AND c2 >= c3 THEN
    CASE WHEN c1 >= c3 THEN c1 ELSE c3 END
  ELSE
    CASE WHEN c1 >= c2 THEN c1 ELSE c2 END
  END,
c3 = CASE
  WHEN c1 <= c2 AND c1 <= c3 THEN c1
  WHEN c2 <= c1 AND c2 <= c3 THEN c2
  ELSE c3 END
FROM @x;

结果:

c1   c2   c3
---- ---- ----
3    2    1
5    4    3
5    4    3
3    2    1
3    3    3
4    3    3
4    4    3
于 2012-08-23T22:00:14.337 回答
0

带动态查询的双重查询

如果您的查询只返回一行,那么您可以通过执行两次查询来解决这个问题:

  1. 查询您的数据以获取列的值
    a)根据结果中的值生成具有列顺序的动态 SQL
  2. 执行动态查询

如果您的查询应该返回几行数据,我想我们将需要更多信息,以便提出一个可行的解决方案或至少一个更好的建议来解决您的问题。

于 2012-08-23T22:09:41.173 回答