简体   繁体   English

SQL-转换值,计算跨多列的实例数

[英]SQL - Convert values, count number of instances across multiple columns

I'm very new to SQL, and I'm not sure how to accomplish the following task: I have a set of data in a table, and I need to convert the data into one of two values, then adding up the totals per row in a pair of summary columns. 我是SQL的新手,我不确定如何完成以下任务:表中有一组数据,我需要将数据转换为两个值之一,然后将每个一对摘要列中的行。 See below: 见下文:

tbl1: TBL1:

row    P1    P2    P3
---    ---   ---   ---
1      a     b     c
2      a     a     a
3      b     c     b
4      a     b     b
5      NULL  NULL  NULL

with a conversion table as: 转换表为:

PNum    PType
----    -----
a       car
b       truck
c       car

The desired end result: 所需的最终结果:

row    P1    P2    P3    numCars    numTrucks
---    ---   ---   ---   -------    ---------
1      a     b     c     2          1
2      a     a     a     3          0
3      b     c     b     1          2
4      a     b     b     1          2
5      NULL  NULL  NULL  NULL       NULL

Code to get the test tables: 获取测试表的代码:

DECLARE @tbl1 TABLE(P1 VARCHAR,P2 VARCHAR,P3 VARCHAR)

INSERT INTO @tbl1
SELECT 'a','b','c' UNION ALL
SELECT 'a','a','a' UNION ALL
SELECT 'b','c','b' UNION ALL
SELECT 'a','b','b'


DECLARE @tbl2 TABLE(PNum VARCHAR,PType NVARCHAR(100))

INSERT INTO @tbl2
SELECT 'a','car' UNION ALL
SELECT 'b','truck' UNION ALL
SELECT 'c','car'

I've been looking into PIVOT but I'm having a hard time seeing how to implement it, (especially with the conversion piece). 我一直在研究PIVOT,但是很难了解如何实现它(尤其是在转换件上)。 I thought about trying to first insert a set of columns just to convert the values (eg "P1_converted"), then trying to summarize them but my actual table has 10 of these type columns and I didn't want to add an additionial 10 columns to the table if there is a simpler way. 我考虑过尝试先插入一组列以转换值(例如“ P1_converted”),然后尝试对它们进行汇总,但是我的实际表中有10个此类列,而我不想添加其他10个列如果有更简单的方法,请转到表格中。 Any thoughts? 有什么想法吗? Thanks! 谢谢!

I'm using SQL Server 2008. 我正在使用SQL Server 2008。

You can use the following which will unpivot the data to get the total count for the cars and trucks. 您可以使用以下命令将取消数据透视表 ,以获取小汽车和卡车的总数。

You can use the UNPIVOT function but since you are using SQL Server 2008, you can use CROSS APPLY and VALUES to convert your P1 , P2 , and P3 from columns into rows. 您可以使用UNPIVOT函数,但是由于使用的是SQL Server 2008,因此可以使用CROSS APPLYVALUESP1P2P3从列转换为行。 The code to convert the data is: 转换数据的代码是:

select *
from tbl1 
cross apply 
(
  values ('p1', p1), ('p2', p2), ('p3', p3)
) c(col, value)

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle This converts the data to the format: 这会将数据转换为以下格式:

| P1 | P2 | P3 | COL | VALUE |
------------------------------
|  a |  b |  c |  p1 |     a |
|  a |  b |  c |  p2 |     b |
|  a |  b |  c |  p3 |     c |
|  a |  a |  a |  p1 |     a |

Once the data is in the rows, then you can easily join to the conversion table to get the count for each row: 一旦数据在行中,那么您可以轻松地加入转换表以获取每一行的计数:

select rn, p1, p2, p3,
  sum(case when t.ptype = 'car' then 1 else 0 end) numCars,
  sum(case when t.ptype = 'truck' then 1 else 0 end) numTrucks
from
(
  select p1, p2, p3,
    row_number() over(order by p1, p2, p3) rn
  from tbl1
) d
cross apply (values ('p1', p1), ('p2', p2), ('p3', p3)) c(col, value)
inner join tbl2 t
  on c.value = t.pnum
group by rn, p1, p2, p3

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle This gives the result: 结果如下:

| RN | P1 | P2 | P3 | NUMCARS | NUMTRUCKS |
-------------------------------------------
|  1 |  a |  a |  a |       3 |         0 |
|  2 |  a |  b |  b |       1 |         2 |
|  3 |  a |  b |  c |       2 |         1 |
|  4 |  b |  c |  b |       1 |         2 |

Here's a slightly different approach using PIVOT : 这是使用PIVOT的稍微不同的方法:

SELECT * FROM 
(
  SELECT d.[row], d.P1, d.P2, d.P3, t.PType,
    [tc] = CASE WHEN t.PNum = d.P1 THEN 1 ELSE 0 END
         + CASE WHEN t.PNum = d.P2 THEN 1 ELSE 0 END
         + CASE WHEN t.PNum = d.P3 THEN 1 ELSE 0 END
  FROM @tbl1 d CROSS JOIN @tbl2 t
) AS x
PIVOT (SUM([tc]) FOR PType IN ([car],[truck])) AS p;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM