SQL选择列WHERE值IN GROUP BY子句BY不同

时间:2018-10-03 16:33:09

标签: sql sql-server

如果结果分组。即:

(结果集)

Id (bigint), UserId (int), Field1 (varchar), Field2 (varchar)
=============================================================
1,           22,           'Job',            'Teacher'
2,           22,           'Car',            'Mustang'
3,           22,           'Age',            '35'
4,           47,           'Job',            'Clerk'
5,           47,           'Car',            'Corolla'
6,           47,           'Age',            '23'
7,           85,           'Job',            'Banker'
8,           85,           'Car',            'Volkswagon'
9,           85,           'Age',            '44'

如何才能根据唯一列(在这种情况下为UserId)返回结果集,并根据具体情况在where子句中获取字段?

要匹配此内容:

Id (bigint), UserId (int), Car (varchar), Job (varchar), Age (varchar)
======================================================================
1,           22,           'Mustang',     'Teacher',     '35'
2,           47,           'Corolla',     'Clerk',       '23'
3,           85,           'Volkswagon',  'Banker',      '44'

我知道这是不好的数据库设计,但是我正在使用旧的应用程序。 我已经尝试过基于单独的选择查询进行联合或加入,但无济于事。

感谢您的帮助,因为我不精通SQL。

5 个答案:

答案 0 :(得分:2)

您可以尝试使用条件聚合和row_number()窗口函数。

SELECT row_number() OVER (ORDER BY x.userid) id,
       *
       FROM (SELECT t.userid,
                    max(CASE t.field1
                          WHEN 'Car' THEN
                            t.field2
                        END) car,
                    max(CASE t.field1
                          WHEN 'Job' THEN
                            t.field2
                        END) job,
                   max(CASE t.field1
                         WHEN 'Age' THEN
                           t.field2
                       END) age
                   FROM elbat t
                   GROUP BY t.userid) x;

(顺便说一下,它是“大众”,而不是“大众”。)

答案 1 :(得分:1)

您可以按userid分组并使用条件聚合来获取值。

select
  userid,
  max(case when field1 = 'Car' then field2 end) as car,
  max(case when field1 = 'Job' then field2 end) as job,
  max(case when field1 = 'Age' then field2 end) as age
from mytable
group by userid
order by userid;

答案 2 :(得分:0)

您可以为每个额外字段加入一次额外时间:

SELECT row_number() over (order by t1.UserId)
    t1.UserId, t1.Field2 As Car, t2.Field2 As Job, t3.Field2 As Age
FROM [MyTable] t1
LEFT JOIN [MyTable] t2 ON t2.UserId = t1.UserID AND t2.Field1 = 'Job'
LEFT JOIN [MyTable] t3 ON t3.UserId = t1.UserID AND t3.Field1 = 'Age'
WHERE t1.Field1 = 'Car'
ORDER BY t1.UserID

(当前已删除的)条件聚合答案可能会更好地工作,但是很高兴知道您的选择。

答案 3 :(得分:0)

您可以使用自我加入

    select month(month_table) as month_table
         , sum(cstart) as cstart 
         , sum(cend) as cend 
      from 
         (
          (select `start_date` as month_table
                , 1 as cstart
                , 0 as cend 
             from newdata
          ) 
  union all 
          ( select `end_date`
                 , 0
                 , 1 
              from newdata 
           ) 
          ) dd 
      group 
         by monthname(month_table)
          , month(month_table) 
      order 
         by month(month_table)

答案 4 :(得分:0)

您可以使用SQL PIVOT

SELECT UserId, Job, Car, Age
FROM
   (SELECT UserId, Field1, Field2 FROM dbo.mytable) x
PIVOT (
   MAX(Field2) FOR Field1 IN (Job, Car, Age)
) AS pvt

PIVOT通过将值(Field1)作为新列的名称并聚合另一个值(Field2)作为这些新列的值,将行转换为列。