Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Group By - question Expand / Collapse
Author
Message
Posted Sunday, September 23, 2012 5:07 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
I have a question which is probably really basic but can't work it out for the life of me so i thought i'll ask for some help. Below is a rough sketch of how my database is there are more details but principle is the same.

In my database i have a table called "Meetings" & in my query i want to group all my meetings that are possible such as below.

DATE | TypeOfMeeting | NameOfMeeting

20120801 private Review
20120801 private Review
20120801 group Review
20120801 group Review
20120801 group Review
20120801 Conference Call Review
20120801 Conference Call Review


SELECT
[DATE],
[TypeofMeeting],
[NameofMeeting]
FROM
dbo.meetings
GROUP BY
[DATE],
[TypeofMeeting],
[NameofMeeting]


Only thing is in "TypeOfMeeting" i have the options of

- Group
- Conference Call
- Private


I want to group all the meetings except "Private" so if i have the same Date,TypeOfMeeting,NameofMeeting for Group or Conference Call they should all be groups for people involved. However if its Private then it should not be grouped and all the Private meetings should be seperated.

So the expected result should be:


DATE | TypeOfMeeting | NameOfMeeting

20120801 private Review
20120801 private Review
20120801 group Review
20120801 Conference Call Review



How do i ignore private from the group by results?
Post #1363310
Posted Sunday, September 23, 2012 7:57 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
Update:

I got around the issue by just doing a quick fix, just made 2 seperate scripts one to only be "Private" and one to exclude only "Private" in my where condition.

I'm Not to worried about the permanent fix to my problem as it was a once off so thats fine, but would like to know for future reference if that is possible to achieve what has been requested or seperate scripts is the only way to go.



Post #1363317
Posted Monday, September 24, 2012 7:43 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 28, 2014 8:02 AM
Points: 290, Visits: 1,659
Not exactly sure that I follow your requirements, but it sounds like you could use UNION to combine two data sets, one with a GROUP BY and one without. You're not including any aggregate logic in your select, so I assume that in this instance you're using GROUP BY to achieve the same effect as DISTINCT and avoid repeated rows in the non-private list of meetings (??).

SELECT
[DATE],
[TypeofMeeting],
[NameofMeeting]
FROM
dbo.meetings
WHERE
[TypeofMeeting] = 'private'

UNION ALL

SELECT
[DATE],
[TypeofMeeting],
[NameofMeeting]
FROM
dbo.meetings
WHERE
[TypeofMeeting] <> 'private'
GROUP BY
[DATE],
[TypeofMeeting],
[NameofMeeting]

Post #1363489
Posted Monday, September 24, 2012 7:46 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
This should do what you want:


--I have a question which is probably really basic but can't work it out for the life of me so i thought i'll ask for some help. Below is a rough sketch of how my database is there are more details but principle is the same.

--In my database i have a table called "Meetings" & in my query i want to group all my meetings that are possible such as below.

--========= TEST DATA =============
declare @Meetings table ([DATE] Char(8), TypeOfMeeting Varchar(20), NameOfMeeting Varchar(10));

insert @Meetings values ( '20120801', 'private', 'Review' );
insert @Meetings values ( '20120801', 'private', 'Review' );
insert @Meetings values ( '20120801', 'group', 'Review' );
insert @Meetings values ( '20120801', 'group', 'Review' );
insert @Meetings values ( '20120801', 'group', 'Review' );
insert @Meetings values ( '20120801', 'Conference Call', 'Review' );
insert @Meetings values ( '20120801', 'Conference Call', 'Review' );

select * from @Meetings;

/*
DATE | TypeOfMeeting | NameOfMeeting

20120801 private Review
20120801 private Review
20120801 group Review
20120801 group Review
20120801 group Review
20120801 Conference Call Review
20120801 Conference Call Review
*/

--========= SOLUTION =============
-- Use 2 selects, 1 grouped for [TypeofMeeting] <> 'private',
-- & 1 not grouped for [TypeofMeeting] = 'private', & UNION them together.
-- NOTE: You MUST use UNION ALL, or duplicate rows are removed from the result set -
-- so only 1 'private' would appear.
SELECT
[DATE],
[TypeofMeeting],
[NameofMeeting]
FROM
@meetings
WHERE [TypeofMeeting] <> 'private'
GROUP BY
[DATE],
[TypeofMeeting],
[NameofMeeting]
UNION ALL
SELECT
[DATE],
[TypeofMeeting],
[NameofMeeting]
FROM
@meetings
WHERE [TypeofMeeting] = 'private'

/*
Only thing is in "TypeOfMeeting" i have the options of

- Group
- Conference Call
- Private

I want to group all the meetings except "Private" so if i have the same Date,TypeOfMeeting,NameofMeeting
for Group or Conference Call they should all be groups for people involved. However if its Private
then it should not be grouped and all the Private meetings should be seperated.

So the expected result should be:

DATE | TypeOfMeeting | NameOfMeeting

20120801 private Review
20120801 private Review
20120801 group Review
20120801 Conference Call Review

How do i ignore private from the group by results?
*/

Post #1363490
Posted Monday, September 24, 2012 3:45 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
Thanks guys, I didn't even think about using a union which was pretty simple idea. I was trying all other stuff.

In terms of my whole process - it probably is easier running 2 separate scripts. Because its quiet a detailed process and easier to troubleshoot / investigate running it separately.

But going forward if I have an issue like this on a non-complex script then union would be way to go.

Thanks again
Post #1363739
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse