2

私は次のようなテーブルを持っています:

    Table1
---------------------
Id |Kind1|Kind2|Kind3|
---------------------
1  |  2  |  2  |  3  | *
---------------------
2  |  2  |  2  |  2  | +
---------------------
3  |  2  |  2  |  1  |
---------------------
4  |  2  |  2  |  3  | *
---------------------
5  |  2  |  1  |  1  |
---------------------
6  |  1  |  1  |  1  |
---------------------
7  |  2  |  2  |  2  | +
----------------------

そして、以下に示すようにその結果を取得したいIDは自動的に生成する必要があります同じ行グループのカウントを取得する必要があります

Result
------------------------------
Id |  Count |New1 |New2 |New3 |
------------------------------
1  |    2   |  2  |  2  |  2  | 
------------------------------
2  |    2   |  2  |  2  |  3  |
------------------------------
3  |    1   |  2  |  2  |  1  |
------------------------------
4  |    1   |  2  |  1  |  1  |
------------------------------
5  |    1   |  1  |  1  |  1  |
------------------------------

SQL Serverでそれを行うにはどうすればよいですか?

4

6 に答える 6

1
select ROW_NUMBER() over (order by (select 0)) as ID,count(*) as count,kind1 as new1,kind2 as new2,kind3 as new3
from yourtable
group by kind1,kind2,kind3
于 2012-10-17T13:35:56.373 に答える
1
;WITH CTE
AS
(
    SELECT Kind1, Kind2, Kind3, COUNT(*) 'Count'
    FROM TableName
    GROUP BY Kind1, Kind2, Kind3
)
SELECT 
  ROW_NUMBER()(OVER ORDER BY SELECT 0) Id,
  [Count], 
  Kind1 AS New1, Kind2 AS New2, Kind3 AS New3
FROM CTE
于 2012-10-17T13:36:11.127 に答える
1

あなたはグループ化することができます(kind1, kind2, kind3)

select  row_number() over (order by min(Id)) as Id
,       count(*) as [Count]
,       kind1
,       kind2
,       kind3
from    YourTable
group by
        kind1
,       kind2
,       kind3
于 2012-10-17T13:37:52.557 に答える
1
BEGIN
DECLARE @t TABLE (
    id INT PRIMARY KEY
    ,kind1 INT
    ,kind2 INT
    ,kind3 INT
)
INSERT INTO @t 
VALUES (1, 1,1,1)
    ,(2, 1,1,2)
    , (3, 2,2,2)
    , (4, 1,1,1)

SELECT
    ROW_NUMBER() OVER (ORDER BY kind1, kind2, kind3) [id]
    ,COUNT(*) [count]
    ,kind1
    ,kind2
    ,kind3
FROM @t
GROUP BY kind1, kind2, kind3

END
于 2012-10-17T13:38:58.987 に答える
1
declare @Temp table (id int, kind1 int, kind2 int, kind3 int)

insert into @Temp
select 1, 2, 2, 3 union all
select 2, 2, 2, 2 union all
select 3, 2, 2, 1 union all
select 4, 2, 2, 3 union all
select 5, 2, 1, 1 union all
select 6, 1, 1, 1 union all
select 7, 2, 2, 2

select
    row_number() over (order by min(T.id)) as id,
    count(*),
    T.kind1, T.kind2, T.kind3
from @Temp as T
group by T.kind1, T.kind2, T.kind3
于 2012-10-17T13:42:36.633 に答える
1

このようなものはあなたが望むものを手に入れるはずです

SELECT Row_Number() Over (Order By cnt desc, kind1 desc, kind2 desc, kind3 desc) as id
     , a.*
FROM (
SELECT COUNT(*) cnt, kind1, kind2, kind3
  FROM table1
GROUP BY kind1, kind2, kind3
) a
于 2012-10-17T14:04:32.983 に答える