1

I'm using a query, I want to group them by 1 specific column. i get an error for other field witch are not listed in GROUP BY clause, yes i know i should tell Sql Server witch row of grouped by rows you should pick but i don't know how to do that. If that's int i use MIN() function and if its a text field i don't know what to do. I want to select all data and if videos.number,videos.title is replicated than get the first row of grouped rows. What this query do is: get all channels from packages according to client subscription. sometime different packages have same channels and I don't want this channel to be shown twice to client.

USE [DB]

 SELECT Videos.number,
        Videos.title,
        Videos.description,
        Videos.format,
        Videos.urlTwo AS url,
        Videos.icon,
        Videos.streamtype,
        'pin' AS password,
        Videos.categoryid,
        Videos.videotype,
   FROM subscription
        INNER JOIN packages_channels
            ON subscription.pkgid = packages_channels.package_id 
        INNER JOIN Videos 
            ON packages_channels.video_id = Videos.number
  WHERE (GETDATE() >= subscription.startdate) 
    AND (GETDATE() <= subscription.enddate) 
    AND (subscription.username = 'mon1')
  GROUP BY videos.number, Videos.title
  ORDER BY Videos.number asc

this is what it shows right now:

number  title        description  format    url                      icon 
101     channel1     101          101       URLstream1               iconPath1
101     channel1     101          101       URLstream1               iconPath1  
102     channel2     101          102       URLstream2               iconPath2
107     channel7     107          107       URLstream7               iconPath7
108     channel8     108          108       URLstream8               iconPath8

but the result i want to get is this:

   number  title        description  format    url                      icon 
    101     channel1     101          101       URLstream1               iconPath1  
    102     channel2     101          102       URLstream2               iconPath2
    107     channel7     107          107       URLstream7               iconPath7
    108     channel8     108          108       URLstream8               iconPath8

as you see channel1 its only once url,and icon is the problem. this fields are text

I can't show a real sql result for security reason.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3189504
  • 161
  • 1
  • 1
  • 12

2 Answers2

1

I don't think you really need a group by here since you are not using any aggregate function. Using distinct will hopefully do the job like below

 SELECT DISTINCT
        Videos.number,
        Videos.title,
        Videos.description,
        Videos.format,
        convert(varchar(max),Videos.urlTwo) AS url,
        convert(varchar(max),Videos.icon) as icon,
        Videos.streamtype,
        'pin' AS password,
        Videos.categoryid,
        Videos.videotype
   FROM subscription
        INNER JOIN packages_channels 
            ON subscription.pkgid = packages_channels.package_id
        INNER JOIN Videos 
            ON packages_channels.video_id = Videos.number
  WHERE (GETDATE() >= subscription.startdate)
    AND (GETDATE() <= subscription.enddate)
    AND (subscription.username = 'mon1')
  ORDER BY Videos.number asc
Kevin Hogg
  • 1,771
  • 25
  • 34
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

If the other columns such as description and format are functionally dependant on the column you want to group on, you can simply include them in the group by clause.

Functional Dependancy means that the column will always have the same value for a certain value of the column it is functionally dependant on.

If they are not functionally dependant, then this simply will not work, because if you have different descriptions for one number, how is the SQL Server to determine which description to display?

One way to overcome this is by using MAX(Description) for instance. This makes an arbitrary choice for the description that happens to come last alphabetically.

wvdz
  • 16,251
  • 4
  • 53
  • 90