简体   繁体   English

SQL查询获取多条记录到多列

[英]SQL query to get multiple records into multiple columns

I have a CSV that I am trying to create and pull data for.我有一个 CSV,我正在尝试为其创建和提取数据。 The CSV template has multiple columns for Status (IE: Status1, Status2, Status3). CSV 模板有多个状态列(即:Status1、Status2、Status3)。 Some locations have only one status and some can have up to 3. I am trying to find a way in SQL that I can have 3 status columns and populate those based on the number of records for a location.有些位置只有一个状态,有些最多可以有 3 个。我试图在 SQL 中找到一种方法,我可以有 3 个状态列,并根据一个位置的记录数填充这些列。 For example, a location called John's Office could have a status of Ready.例如,名为 John's Office 的位置可能具有就绪状态。 Another location called IT Workroom may have 3 statuses, Ready, In-Repair, and In-Use.另一个名为 IT Workroom 的位置可能有 3 种状态,Ready、In-Repair 和 In-Use。 The columns in the SQL query would look something like this: SQL 查询中的列如下所示:

Location          Status1         Status2        Status3
----------------------------------------------------------
John's Office     Ready
IT Workroom       Ready           In-Repair       In-Use

The column names Status1, Status2, etc would be column names that are just made up.列名 Status1、Status2 等将是刚刚组成的列名。 I would want to populate Status1 with the first record that returns, regardless of the actual status name.我想用返回的第一条记录填充 Status1,而不考虑实际的状态名称。 So Status1 would always be populated.因此 Status1 将始终被填充。 Status2 will only be populated if their is a second status. Status2 只有在它们是第二状态时才会被填充。

In the original table that holds this information, it looks like this:在包含此信息的原始表中,它看起来像这样:

LocationName           StatusName
------------------------------------
IT Workroom            In-Repair
IT Workroom            Ready
John's Office          Ready
IT Workroom            In-Use

I have tried PIVOT , but I realized that this is not what I need as I am not technically aggregating the data.我试过PIVOT ,但我意识到这不是我需要的,因为我没有在技术上聚合数据。

you can use row number with max case when like this:你可以像这样使用最大大小写的行号:

select LocationName, MAX(CASE WHEN RowNum = 1 THEN StatusName ELSE NULL END) Status1
    , MAX(CASE WHEN RowNum = 2 THEN StatusName ELSE NULL END) Status2
    , MAX(CASE WHEN RowNum = 3 THEN StatusName ELSE NULL END) Status3 
from (
    select *, ROW_NUMBER() OVER (PARTITION BY LocationName ORDER BY LocationName, StatusName) RowNum
    from #data
) Recs
group by LocationName

Please try the following solution mimicking PIVOT operation.请尝试以下解决方案模仿 PIVOT 操作。

SQL SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (id INT IDENTITY PRIMARY KEY, LocationName VARCHAR(30), StatusName VARCHAR(50));
INSERT INTO @tbl VALUES
('IT Workroom', 'In-Repair'),
('IT Workroom', 'Ready'),
('John''s Office', 'Ready'),
('IT Workroom', 'In-Use');
-- DDL and sample data population, end

SELECT  LocationName
   , MAX(CASE WHEN StatusName = 'Ready' THEN StatusName ELSE '' END) [Status1]
   , MAX(CASE WHEN StatusName = 'In-Repair' THEN StatusName ELSE '' END) [Status2]
   , MAX(CASE WHEN StatusName = 'In-Use' THEN StatusName ELSE '' END) [Status3]
FROM @tbl
GROUP BY LocationName
ORDER BY LocationName;

Output Output

+---------------+---------+-----------+---------+
| LocationName  | Status1 |  Status2  | Status3 |
+---------------+---------+-----------+---------+
| IT Workroom   | Ready   | In-Repair | In-Use  |
| John's Office | Ready   |           |         |
+---------------+---------+-----------+---------+

This is very simple solution considering you have just 3 different types of statuses so the code is not so much replicated.这是一个非常简单的解决方案,考虑到您只有 3 种不同类型的状态,因此代码不会被复制太多。

insert into new_locations (
    select distinct location,
        case when location in (select distinct location from old_locations where status = 'ready') then 'ready' else '' end,
        case when location in (select distinct location from old_locations where status = 'active') then 'active' else '' end,
        case when location in (select distinct location from old_locations where status = 'dead') then 'dead' else '' end
    from old_locations
);

SQL is quite handy and this is just the case where you can kind of cheat with using bit of code replication, it is very simple to understand and does its job. SQL 非常方便,这只是您可以通过使用一些代码复制来作弊的情况,它非常容易理解并发挥作用。 There usually is some pure SQL solution for this kind of problems without need of using T-SQL or other sql 'upgrades'.对于此类问题,通常有一些纯粹的 SQL 解决方案,无需使用 T-SQL 或其他 sql “升级”。

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

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