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

Java Swing MySql JDBC: insert data into database

Program import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class insertswing implements ActionListener {   JFrame fr;JPanel po;   JLabel l1,l2,main;   JTextField tf1,tf2;   GridBagConstraints gbc;   GridBagLayout go;   JButton ok,exit; public insertswing(){ fr=new JFrame("New User Data "); Font f=new Font("Verdana",Font.BOLD,24); po=new JPanel(); fr.getContentPane().add(po); fr.setVisible(true); fr.setSize(1024,768); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); po.setBackground(Color.WHITE); go=new GridBagLayout(); gbc=new GridBagConstraints(); po.setLayout(go); main=new JLabel("Enter User Details "); main.setFont(f); l1=new JLabel("Name  :");tf1=new JTextField(20); l2=new JLabel("User Name  :");tf2=new JTextField(20); ok=new JButton("Accept"); exit=new JButton("Exit"); gbc.anchor=GridBagConstraints.NORTH;gbc.gridx=5;gbc.gridy=0; go.s...

Guidewire Policy - Spin Up Spin Off Transactions

Guidewire PolicyCenter - Spin Up and Spin Off Policy Job Transactions In Guidewire PolicyCenter, "spin up" and "spin off" refer to specific actions you can take with policy job transactions. These terms are related to how new policy transactions (such as renewals, endorsements, or cancellations) are created or modified. Here's an explanation of each: 1. Spin Up: "Spin up" refers to the process of creating a new policy job from an existing policy or transaction. When you "spin up" a policy job, you're essentially initiating a new transaction based on an existing policy. This new transaction could be a renewal, an endorsement, or any other type of policy change. For example: - Renewal : When a policy's term is about to expire, you might "spin up" a renewal job to create a new policy term based on the existing one. The new job will carry forward much of the existing policy's data but may allow for updates or cha...

Guidewire Reinstatement and Rewrite

Guidewire Reinstatement, Rewrite Mid Term, Rewrite Full Term, and Rewrite New Term In Guidewire PolicyCenter, different types of policy transactions allow users to modify, renew, reinstate, or rewrite policies under various circumstances. Here̢۪s an explanation of Reinstatement, Rewrite Mid Term, Rewrite Full Term, and Rewrite New Term, along with their similarities, differences, and example scenarios. 1. Reinstatement Definition: - Reinstatement is a process that brings a canceled policy back into force. This is typically done after a policy has been canceled due to non-payment or other reasons, and the insurer agrees to reinstate the policy, often after the insured has met certain conditions (e.g., paying outstanding premiums). Scenario Example: - A policyholder misses their premium payment, and the policy is canceled. After paying the overdue amount, the insurer reinstates the policy without any changes to the original policy terms and conditions. Key Points: - The poli...