我有一个查询,可以为员工提供销售信息。
查询的一个例子是:
Select Name, Customer_count, item1, item2, item3
from Invoices
输出:
Name Customer_Count item1 item2 item3
Rob 10 1 2 0
Bill 10 3 0 2
Jim 10 2 1 3
我有第二个查询来提取每个项目的百分比。
Select
Name,
Customer_count,
Cast(Cast(item1 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item1 ,
Cast(Cast(item2 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item2,
Cast(Cast(item3 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item3
from Invoices
Name Customer_Count item1 item2 item3
Rob 10 10.00 20.00 0.00
Bill 10 30.00 0.00 20.00
Jim 10 20.00 10.00 30.00
我怎样才能输出以下输出,查询1,名称,查询2,不列出名称。:
Name Customer_Count item1 item2 item3
Rob 10 1 2 0
10 10.00 20.00 0.00
Bill 10 3 0 2
10 30 0.00 20.00
Jim 10 2 1 3
10 20 10.00 30.00
答案 0 :(得分:3)
这会让你非常接近。
select name, customer_count, item1, item2, item3
from
(
select
rownum = row_number() over (order by name),
name, customer_count, item1, item2, item3
from Invoices
union
Select rownum = row_number() over (order by name),
'', Customer_count,
Cast(Cast(item1 as float) / Cast(Customer_Count as float) as decimal (10,2)) * 100 as item1 ,
Cast(Cast(item2 as float) / Cast(Customer_Count as float) as decimal (10,2)) * 100 as item2,
Cast(Cast(item3 as float) / Cast(Customer_Count as float) as decimal (10,2)) * 100 as item3
from Invoices
) x
order by rownum, name desc
注意item [n]值都有小数。我不确定如何让它看起来像你的例子,但也许这并不重要。
诺尔
答案 1 :(得分:3)
您可以使用UNION ALL
子句并执行两个查询的并集,然后使用FROM
句子中的结果。要按照您希望的方式对行进行排序,您只需将主键列(或多列)添加到每个SELECT
句子中,然后添加一个包含可见名称的列。此Visible_name
列将包含第一个联合选择的Name
值,而第二个选择将包含空字符串。
ORDER BY
子句必须首先按主键列排序,然后按Visible_name
列按降序排序。
假设Invoices
表有一个Id
列作为主键,查询可以如下:
SELECT * FROM
(Select Id, Name, Name AS Visible_name, Customer_count, item1, item2, item3
from Invoices
UNION ALL
Select
Id,
Name,
'' AS Visible_Name,
Customer_count,
Cast(Cast(item1 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item1 ,
Cast(Cast(item2 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item2,
Cast(Cast(item3 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item3
from Invoices
)
ORDER BY Id, Visible_name DESC
答案 2 :(得分:2)
您可以使用两组混合物。使用跳数为2(2,4,6,8,...)的运行数创建两个集合,并在第二个集合中添加1(3,5,7,9,...)。
如果你在此之后对它进行排序,你将"混合"两套
我使用名字字段进行排序 - 这肯定是一个坏主意......
并且 - 如果您希望没有小数的项目显示为" 1"而不是" 1.00",你必须做额外的输出格式......
编辑:我刚刚更换了"名称"在第二组中有一个空的空间......DECLARE @dummyTbl TABLE(Name VARCHAR(100),Customer_count INT, item1 INT ,item2 INT,item3 INT);
INSERT INTO @dummyTbl VALUES
('Rob',10,1,2,0)
,('Bill',10,3,0,2)
,('Jim',10,2,1,3);
SELECT *
FROM
(
Select ROW_NUMBER() OVER(ORDER BY Name) * 2 AS Nr
,Name, Customer_count, item1, item2, item3
from @dummyTbl
UNION ALL
Select
ROW_NUMBER() OVER(ORDER BY Name) * 2 + 1 AS Nr,
'',
Customer_count,
Cast(Cast(item1 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item1 ,
Cast(Cast(item2 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item2,
Cast(Cast(item3 as float) / Cast(Customer_Count as float) as decimal (10,2)) as item3
from @dummyTbl
) AS tbl
ORDER BY tbl.Nr
结果
Nr Name Customer_count item1 item2 item3
2 Bill 10 3.00 0.00 2.00
3 10 0.30 0.00 0.20
4 Jim 10 2.00 1.00 3.00
5 10 0.20 0.10 0.30
6 Rob 10 1.00 2.00 0.00
7 10 0.10 0.20 0.00