简体   繁体   中英

sql complex group query

I have a table with the following rows:

id. type  - link 

 1. image - http://1
 2. image - http://2
 3. text  - none
 4. video - http://..
 5. image - http://..
 6. text  - http://..

I want to group the type (image) by date, so they show as single row. In this example, the first two images will merge together and output will be like following:

1. image - http://1, http://2  ** GROUPED BY DATE, if they are same type and not break type after it.
2. text  - none
3. video - http://..
4. image - http://..
5. text  - http://..

With MySQL you can do this :

SELECT id, type, group_concat(link) 
FROM table
GROUP BY id, type
SELECT  grouper, type, GROUP_CONCAT(link)
FROM    (
        SELECT  @group := @group + (NOT (COALESCE(@type, type) = type)) AS grouper,
                @type := type,
                m.*
        FROM    (
                SELECT  @group := 0,
                        @type := NULL
                ) vars,
                mytable m
        ) q
GROUP BY
        grouper
select id, type, group_concat(link) from table

试试吧。

Here is a complete mind bending equivalent example for SQL Server 2005 and beyond:


create table #Temp
    (GroupField int, ValueType varchar(10), Value varchar(2048))

insert into #Temp (GroupField, ValueType, Value) VALUES (1, 'image', 'http://1')
insert into #Temp (GroupField, ValueType, Value) VALUES (1, 'image', 'http://2')
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'text', 'none')
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'video', '30mins')
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'image', 'http://5')
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'image', 'http://4')
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'text', 'hello')
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'image', 'http://7')
insert into #Temp (GroupField, ValueType, Value) VALUES (4, 'image', 'http://0')

    SELECT GroupField, ValueType,
           LEFT([Values],LEN([Values]) - 1)   AS [Values]
    FROM   (SELECT GroupField, ValueType,
                   (SELECT value + ', ' AS [text()]
                    FROM   #Temp AS internal
                    WHERE  internal.GroupField = GroupFields.GroupField and internal.ValueType = GroupFields.ValueType 
                    FOR xml PATH ('')
                   ) AS [Values]
            FROM   (SELECT  GroupField, ValueType
                    FROM     #Temp
                    GROUP BY GroupField, ValueType) AS GroupFields) AS pre_trimmed;

Produces the result:


GroupField   ValueType    Values
1            image        http://1, http://2
2            image        http://5
2            text         none
2            video        30mins
3            image        http://4, http://7
3            text         hello
4            image        http://0

It might need some ORDER BY clauses for your particular case ... adapted from Rational Relational - Emulating MySQL's GROUP_CONCAT() Function in SQL Server 2005

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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