Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SQL Fiddle][1]
- **MS SQL Server 2008 Schema Setup**:
- CREATE TABLE tblMessages
- (
- mbrId int primary key,
- messageCount int
- );
- CREATE TABLE tblNotifications
- (
- mbrId int primary key,
- notificationCount int
- );
- CREATE TABLE tblRequests
- (
- mbrId int primary key,
- requestCount int
- );
- INSERT INTO tblMessages
- (mbrId, messageCount)
- VALUES
- (2, 20),
- (3, 2);
- INSERT INTO tblNotifications
- (mbrId, notificationCount)
- VALUES
- (1, 1),
- (3, 2);
- INSERT INTO tblRequests
- (mbrId, requestCount)
- VALUES
- (3, 2);
- **Query 1**:
- SELECT COALESCE(m.mbrid, r.mbrid, n.mbrid) AS mbrid, ISNULL(m.messageCount,0) AS messageCount, ISNULL(n.NotificationCount,0) AS NotificationCount, ISNULL(r.requestCount, 0) AS requestCount
- FROM tblMessages m
- full JOIN tblNotifications n
- ON m.mbrid = n.mbrid
- AND m.messageCount > 0
- AND n.notificationCount > 0
- full JOIN tblRequests r
- ON m.mbrid = r.mbrid
- AND r.requestCount > 0
- ORDER BY mbrid
- **[Results][2]**:
- | MBRID | MESSAGECOUNT | NOTIFICATIONCOUNT | REQUESTCOUNT |
- |-------|--------------|-------------------|--------------|
- | 1 | 0 | 1 | 0 |
- | 2 | 20 | 0 | 0 |
- | 3 | 2 | 2 | 2 |
- [1]: http://sqlfiddle.com/#!3/751f8/18
- [2]: http://sqlfiddle.com/#!3/751f8/18/0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement