Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2011-12-22 : 00:00:19
Hi

Table USERS

columns:
User_id (PK) (generated via a sequence)
Email
Gender
Age
Name

table SUBSCRIPTIONS
columns:
SUbscription_id (PK) (generated via a sequence)
user_id (UK) (FK from users)
subscription_type (UK)
active_indicator

table TRANSACTIONS
columns:
subscription_id (PK) (FK from subscriptions)
action (PK)
timestamp (PK)


sample data:

USERS

user_id email gender age name
1 a@a.com m 30 rob
2 a@a.com m 31 robert
3 b@b.com f 18 lucie
4 b@b.com f 22 lulu
5 c@c.com m 10 kim
6 c@c.com f 18 kim
7 c@c.com f 08 kim
8 d@d.com f 18 JJ
9 d@d.com m 22 Jay
10 e@e.com f 88 Bill
11 e@e.com f 88 Will
12 e@e.com f 60 Will
13 f@f.com m 70 George

subscriptions

subscription_id user_id subsciption_type active_indicator
1 2 Magazine Yes
2 3 Music CD No
3 3 Magazine Yes
4 3 Video Yes
5 8 Magazine Yes
6 9 Video Yes
7 10 Magazine No
8 13 Magazine yes

transactions
subscription_id action timestamp
1 Renewal 2002-sep-10
2 Renewal 2002-Jan-01
2 Cancellation 2002-Feb-01
3 Renewal 2002-Aug-20
4 Renewal 2002-Aug-01
4 Renewal 2002-Sep-01
5 Renewal 2002-Aug-01
6 Renewal 2001-Sep-01
7 Renewal 2002-Sep-01
7 Cancellation 2002-Sep-10

Based on query criteria:
Generate a list of unique email addresses with the latest name, gender
and age for a user with that email

The selection criteria limit the list to users which never subscribed to
anything; or;
users with inactive subscriptions; or;
users with active subscriptions that renewed between Sep 1st and sep
30th of any year

How do i return data answer that would: thanks a lot

a@a.com m 31 robert
b@b.com f 22 lulu
c@c.com f 08 kim
d@d.com m 22 Jay
e@e.com f 60 Will

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-22 : 01:56:09
ok...your assignment questions looks good
now post us what you tried so far and issue faced and then we will help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2011-12-22 : 08:59:13
hi

This is the query with the following results, but i am not able to get the result as expected. thanks a lot

SELECT DISTINCT dbo.Users.Email, dbo.Users.gender, dbo.Users.age, dbo.Users.name
FROM dbo.subsciption INNER JOIN
dbo.transactions ON dbo.subsciption.subscription_id = dbo.transactions.subscription_id INNER JOIN
dbo.Users ON dbo.subsciption.user_id = dbo.Users.UserID
WHERE (dbo.subsciption.active_indicator = 'No') OR
(MONTH(dbo.transactions.timestamp) = 9)

a@a.com m 31 robert
b@b.com f 18 lucie
d@d.com m 22 Jay
e@e.com f 88 Bill
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-22 : 10:58:58
[code]
SELECT u.email, u.gender, u.age, u.name
FROM users u
OUTER APPLY (SELECT COUNT(*) AS cnt
FROM subscriptions
WHERE user_id =u.user_id
AND active_indicator='Yes'
)scr1
OUTER APPLY (SELECT COUNT(*) AS cnt
FROM subscriptions s
INNER JOIN transactions t
ON t.subscription_id = s.subscription_id
WHERE s.userid=u.user_id
AND s.active_indicator='Yes'
AND t.action ='RENEWAL'
AND MONTH(t.timestamp)<>9
)scr2
WHERE COALESCE(scr1.cnt,0) = 0
OR COALESCE(scr2.cnt,0) = 0
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-22 : 11:44:14
why do you allow different users to have the same email address?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -