views:

197

answers:

4

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://..
A: 
select id, type, group_concat(link) from table

Try that.

toby
If you're using MySQL ...
Rabid
This will not actually work, the group by clause is missing
MaxiWheat
MaxiWheat: yeah oops. Rabid: the question IS tagged mysql
toby
oh yeah well spotted
Rabid
A: 

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

Rabid
+1  A: 

With MySQL you can do this :

SELECT id, type, group_concat(link) 
FROM table
GROUP BY id, type
MaxiWheat
+1  A: 
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
Quassnoi
great it works, but can you also please tell me, how can i just group images only and avoid text from grouping too?
Basit
SOLUTION:SELECT grouper, @type, type, GROUP_CONCAT(thumbnail), count(*), date_format(created_date,'%H %k %I %r %T %S %w')FROM ( SELECT @group := @group + (NOT (COALESCE(@type, type) = type) or type = 'feed') AS grouper, @type := type, m.* FROM ( SELECT @group := 0, @type := NULL ) vars, feed morder by created_date desc ) qGROUP BY grouper
Basit
this query was working fine, but until i start showing other users media feed also, which means it also group there media as same user.. how can we seperate the groups for each different user seperately, even if the next row is same as previous row.
Basit
@Quassnoi can you help on this please http://stackoverflow.com/questions/3366597/how-to-make-slow-query-faster
Basit