SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 ORDER BY goes where?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

KnooKie
Aged Yak Warrior

United Kingdom
623 Posts

Posted - 01/24/2013 :  07:41:40  Show Profile  Visit KnooKie's Homepage  Reply with Quote
Hi All

I have the following query that is fine but where within it would i put the ORDER BY part?

many thanks

/********************************************************************
All Medec Registrants who do NOT receive the Medeconomics Bulletin
*********************************************************************/
(
/******************
Medec Registrants
*******************/
SELECT DISTINCT
	u.userid
	,u.usertitle
	,u.userfirstname
	,u.userlastname
    ,ltrim(rtrim(lower(u.useremail)))     AS userEmail
FROM 
      dbo.tbl_Users u
INNER JOIN 
      u.f_sub_site ss ON u.userID = ss.userID
WHERE 
      ss.d_sub_type_id = 3    -- 3 = free trial
      AND ss.magJournalCode IN (
            'GP'
            ,'MM2'
            )
)
EXCEPT		-- exclude everyone from below from the above query
(
/**********************************
Users who receive Medec bulletin
***********************************/
SELECT DISTINCT
	u.userid
	,u.usertitle
	,u.userfirstname
	,u.userlastname
	,ltrim(rtrim(lower(u.useremail)))	AS userEmail
FROM
 tbl_Users U
 INNER JOIN (
  SELECT
   tusers.userId AS userId
   ,eusers.ecircleGID
   ,eusers.ecircledateadded AS  dateadded
   ,ecast.cmpID AS cmpID
  FROM
   [HBPL_ECircle].[tbl_ECircleUsers] AS eusers
   INNER JOIN [dbo].[tbl_ECast_Campaigns] AS ecast ON eusers.[ecircleGID] = ecast.[ecircleGID]
   INNER JOIN [dbo].[tbl_Users] AS tusers ON eusers.[userUUID] = tusers.[userUID]
  WHERE
   ecast.cmpID IN (1826)	-- Medec campaign code
  UNION ALL
  SELECT
   rpt_userID AS userID
   ,NULL AS ecircleGID
   ,rptdateadded AS dateadded
   ,rpt_cmpid AS cmpID
  FROM
   [dbo].[tbl_ECast_Recipients] AS erecip
  WHERE
   erecip.rpt_cmpID IN (1826)	-- Medec campaign code
  ) AS E ON U.[userID] = E.[userID]

WHERE
 U.userOptInHBPLEmails = 1	-- HM email opt in
)


====
Paul

James K
Flowing Fount of Yak Knowledge

3742 Posts

Posted - 01/24/2013 :  07:54:44  Show Profile  Reply with Quote
At the very end:
.....
WHERE
 U.userOptInHBPLEmails = 1	-- HM email opt in
)
ORDER BY userid;
Go to Top of Page

KnooKie
Aged Yak Warrior

United Kingdom
623 Posts

Posted - 01/24/2013 :  07:59:08  Show Profile  Visit KnooKie's Homepage  Reply with Quote
quote:
Originally posted by James K

At the very end:
.....
WHERE
 U.userOptInHBPLEmails = 1	-- HM email opt in
)
ORDER BY userid;




Great thanks

====
Paul
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000