Skip to main content

SQL Join Query - Building Complex Join Query - Day 5 #100days of SQL

 Hi ,

In this blog post will check the join queries

SQL Join function below diagram shows difference possibilities

 



Below queries has been executed on the video podcast


-- will see about join query from the db table schema diagram

-- will check about product and transaction history table on join  query


select * from Production.Product

 -- 504 records

select * from Production.TransactionHistory 

-- 113443 records


-- inner join

select * from Production.Product p inner join Production.TransactionHistory t

on

p.ProductID = t.ProductID 


-- full join or full outer join

select * from Production.Product p full outer join Production.TransactionHistory t

on

p.ProductID = t.ProductID


-- right join or right outer join

select * from Production.Product p right outer join Production.TransactionHistory t

on

p.ProductID = t.ProductID


-- left join or left outer join

select * from Production.Product p left outer join Production.TransactionHistory t

on

p.ProductID = t.ProductID


 -- now will complex join and other sql query operations


select 

p.ProductID, p.StandardCost, p.ListPrice,

t.ActualCost, t.Quantity

from Production.Product p cross join Production.TransactionHistory t

where

p.class is not null and 

t.ActualCost <> 0.00 and 

p.StandardCost <> 0.00 and

p.ListPrice <> 0.00 and

p.Name like '%Road%' and

p.ProductNumber like '%7'


-- will try with group by 

-- from the sql query execution cross join performance takes too long time compared with inner join

-- so prefer only inner join, full join , right / left join sql query operations

select 

Top 100

p.ProductID, AVG(p.StandardCost) as AvgStdCost, AVG(p.ListPrice) as AvgListPrice,

Avg(t.ActualCost) as AvgActualCost, Count(t.Quantity) as TotalQuantity,

Row_number() over (order by AVG(p.StandardCost) desc  ) as StandardCostRank,

Rank() over (order by AVG(p.StandardCost) desc  ) as DistinctStandardCostRank,

Dense_Rank() over (order by AVG(p.StandardCost) desc  ) as DistinctNextSeqStandardCostRank

from Production.Product p inner join Production.TransactionHistory t

on

p.ProductID = t.ProductID 

where

p.class is not null and 

t.ActualCost <> 0.00 and 

p.StandardCost <> 0.00 and

p.ListPrice <> 0.00 and

p.Name like '%Road%'

--p.ProductNumber like '%7'

group by

p.ProductID

Order by

AvgStdCost Desc


-- now will do more sql query operations with more than two tables

-- will check in db schema

-- from db schema simple flow will join Sales person with product 

-- Sales person Business EntityId connecting with salesperson id foreignkey SalesOderHeader table

-- finding outliers from max min sales deviations totally 14 records has min max deviations in salesdata

-- thanks for watching

select 

p.ProductID, AVG(sp.Bonus) as AverageBonus, Avg(sp.SalesQuota)  as AverageSalesQuota,

Avg(sp.SalesYTD)  as AverageSalesYTD,Avg(sp.SalesLastYear) as AverageSalesLY,

Avg(sod.UnitPrice)  as AverageUP, Avg(sod.UnitPriceDiscount)  as AverageUPD, 

Avg(sod.LineTotal)  as AverageLineTotal,

(Max(sp.SalesYTD) - Avg(sp.SalesYTD)) as MaxSalesDeviation,

(Avg(sp.SalesYTD) - Min(sp.SalesYTD)) as MinSalesDeviation

from 

Sales.SalesPerson sp

inner join 

Sales.SalesOrderHeader soh

on

sp.BusinessEntityID = soh.SalesPersonID

inner join 

Sales.SalesOrderDetail sod -- salesorderid

on

soh.SalesOrderID = sod.SalesOrderID

inner join

Sales.SpecialOfferProduct sop 

on

sop.SpecialOfferID = sod.SalesOrderDetailID

inner join

Production.Product p

on

p.ProductID = sop.ProductID

group by

p.ProductID

having

(Max(sp.SalesYTD) - Avg(sp.SalesYTD)) <> 0.00 and

(Avg(sp.SalesYTD) - Min(sp.SalesYTD)) <> 0.00

order by

p.ProductID 




Comments

Popular posts from this blog

"How to maintain or retain tabs in same tab after button click events or postback?" using JQuery in ASP.NET C#

In this post I'll share an details about " How to maintain or retain tabs in same tab after button click events or postback? " Step 1: you need to download Jquery and JQueryUI Javascript libraries from this site http://jqueryui.com/ Step 2: As usually you can create ASP.NET website from Visual Studio IDE and add Jquery and JqueryUI plugins in the header section of aspx page. Step 3: Add HiddenField control inside aspx page which is very useful to retain tab in same page Step 4: Use the HiddenField ID in Jquery code to indicate that CurrentTab Index Step 5: In code Behind, using Enumerations concept give the tab index values as user defined variable  Step 6: Use the Enum values in every Button click events on different tabs to check that tab could be retained in the same tab Further, Here I'll give the code details and snap shot pictures, 1. Default.aspx: Design Page First Second Third ...

Guidewire Related Interview Question and answers part 1

common Guidewire questions and answers 20 Guidewire BC Q&A Top 100 Guidewire Interview FAQ Guidewire Claimcenter 20 Interview Questions Guidewire Rating concepts

Login and Registration forms in C# windows application with Back end Microsoft SQL Server for data access

In this article, I'm gonna share about how to make login and register form with MS SQL database; 1. Flow Chart Logic 2. Normal Features 3. Form Designs Login Form Design Sign in Form Design Password Retrieve Form 4. Database Design and SQL queries and Stored Procedure Create new Database as "schooldata" create table registerdata (  ID int identity,  Username nvarchar(100),  Password nvarchar(100),  Fullname  nvarchar(100),  MobileNO nvarchar(100),  EmailID nvarchar(100)  ) select * from registerdata create procedure regis (  @Username as nvarchar(100),  @Password as nvarchar(100),  @Fullname as nvarchar(100),  @MobileNO as nvarchar(100),  @EmailID as nvarchar(100)  ) as begin insert into registerdata (Username, Password, Fullname, MobileNO,EmailID) values (@Username, @Password, @Fullname, @MobileNO, @EmailID) ...