Select distinctPurchasedbycontactid asPurchasedbycontactid into#SP fromBasket. Baseordertracker (NOLOCK)whereOrderDate> '2014-2-1' andOrderDate< '2014-3-1' andOrdertypeid= the andOesourceid= 2 andStageid= 10400Select distinctPurchasedbycontactid asPurchasedbycontactid into#P fromBasket. Baseordertracker (NOLOCK)whereOrderDate> '2014-2-1' andOrderDate< '2014-3-1' andOrdertypeid= the andOesourceid= 2--The Lost ManSelectA.* into#NSP from#P (NOLOCK) asawhere not exists(Select Top 1 1 from#SP asBwhereA.purchasedbycontactid=B.purchasedbycontactid)--detailed information about the lost personSelectC. startdate asStartDate, C. consultantlevelid asLevelcode, I.directsellerid asDirectsellerid, #NSP. Purchasedbycontactid ascontactId into#SNPD fromCONTACTS. Consultants C (NOLOCK)Inner Join#NSP (NOLOCK) on#NSP. Purchasedbycontactid=C. ContactIDInner JoinCONTACTS. Internationalconsultants I (NOLOCK) onC. ContactID=i. ContactID--The loss of the person's orderSelect#SNPD.*, B.orderid asOrderID into#porder fromBasket. Baseordertracker B (NOLOCK)Inner Join#SNPD (NOLOCK) onB.purchasedbycontactid=#SNPD. ContactId andB. OrderDate>'2014-2-1' andB. OrderDate< '2014-3-1' andB.ordertypeid= the andB. Stageid<> 10400 andB. Oesourceid= 2--Order Maximum ValueSelect#porder. OrderIDMax(O. Stageid) asStageid into#odermax from#porder (NOLOCK)Inner JoinBasket. Ordertrackeraudit O (nolock) onO. OrderID=#porder. OrderIDGroup by#porder. OrderID-- LastSelect#porder.*, #odermax. Stageid into#last from#porder (NOLOCK), #odermax (NOLOCK)where#odermax. OrderID=#porder. OrderID--Query onlySelect * from#last (NOLOCK)whereStageid= 10400--stage analysis for failed personnel ordersSelectStageid,COUNT(OrderID) from#last (NOLOCK)Group byStageidOrder byStageid--Level analysis for failed personnel ordersSelectcom. purchasercareerlevelid,COUNT(COM. OrderID) from#last (NOLOCK) LInner Joincosmeticordermaster (nolock) COM onL. OrderID=com. orderidGroup bycom. PurchasercareerlevelidOrder bycom. Purchasercareerlevelid--Level Analysis of successful ordersSelectcom. purchasercareerlevelid,COUNT(COM. OrderID) fromBasket. Baseordertracker (NOLOCK) botInner Joincosmeticordermaster (nolock) COM onBot. OrderID=com. orderidwhereOrderDate> '2014-2-1' andOrderDate< '2014-3-1' andOrdertypeid= the andOesourceid= 2 andStageid= 10400Group bycom. PurchasercareerlevelidOrder byCom. Purchasercareerlevelid
SQL Server Order Analysis