Multi-table query for SQL Server (left join)

Source: Internet
Author: User
Tags joins

Today wrote a SQL to get three times to calculate success, is previously written sq himself l never noticed, left join

DECLARE @SendIntervalDays int, @OrderIntervalDays int, @OrderDate DATETIMESET @SendIntervalDays = 7--SMS Send Interval daysSET @OrderIntervalDays = 30--The number of days the order has been signedSET @OrderDate = ' 2014-12-12 '--order timeSELECTO.userid, Max (o.orderid) OrderID, MAX (mobilephone) MobileFrom View_saleorder O with (NOLOCK)Left joins Spikeactivityorderrequest so with (NOLOCK) on O.userid=so. UserIDand O.mobilephone=so.mobile and DateDiff (D,createtime,getdate ()) >= @SendIntervalDays--Sent SMS time interval greater than 7 days this sentence is actually wrong, The latter condition is completely useless. This is a thinking problem, should first put the data source to, and then in the screening, not here to fix, there is done, put together, bigger picture not good ahWHERE o.province in (310000,320000,330000)and o.deliverysigndate >= @OrderDateand DateDiff (d,o.deliverysigndate,getdate ()) = @OrderIntervalDays--Group by O.useridUNION All--Seconds to kill ordersSELECTO.userid, Max (o.orderid) OrderID, MAX (mobilephone) MobileFrom View_saleorder O with (NOLOCK)INNER JOIN orderdiscountdetail OD (NOLOCK) on O.orderid=od. OrderIDWHERE OD. Saleactivityruletype = Tenand o.province in (310000,320000,330000)and o.deliverysigndate >= @OrderDate-- signedGroup by O.userid

--declare @SendIntervalDays int, @OrderIntervalDays int, @OrderDate datetime--set @SendIntervalDays =30     & nbsp;--sms Send interval days--set @OrderIntervalDays =7    --order has been signed for days--set @OrderDate = ' 2014-12-12 '  --order time; With TEMP as (--Seconds to kill order Select o.userid Userid,max (O.orderid) Orderid ,max (mobilephone) Mobile from View_ Saleorder O with (NOLOCK) INNER joins Orderdiscountdetail OD (NOLOCK) on O.orderid=od. Orderid where  datediff (d,o.deliverysigndate,getdate ()) = 0  --Day sign and   o.creationdate >= @ Orderdate and   OD. Saleactivityruletype = 10AND   o.province in (310000,320000,330000) Group by o.userid union   all--General Order Select o.userid Userid,max (O.orderid) Orderid ,max (mobilephone) Mobile from  View_ Saleorder o with (NOLOCK) WHERE DateDiff (d,o.deliverysigndate,getdate ()) = @OrderIntervalDays-The order has been signed for 7 days and O. CreationDate >= @OrderDate  and o.province in (310000,320000,330000) Group by O.userid)--Go to possibly repeat UseriDselect temp. UserID Userid,max (TEMP. OrderID) Orderid ,max (TEMP. Mobile) Mobile from TEMPLeft joins Spikeactivityorderrequest so with (NOLOCK) on (TEMP. Userid=so. UserID OR temp.mobile=so.mobile)WHERE (DateDiff (d,so. Createtime,getdate ()) >= @SendIntervalDays OR so. Createtime is NULL)--text messages that have been sent for more than 30 days should be all data sources assembled to filterGROUP by TEMP. Useridorder by TEMP. Userid

Think more about how to think more summarize and investigate more attention to performance, priority location data Source Filter data source

Multi-table query for SQL Server (left join)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.