Inner join base on specific condition - sql

There are 3 tables Brand,Product and BrandProduct respectively.Table has following structure.
Brand : id,BrandName
Product : id,ProductName
BrandProduct :id,Brandid,Productid,Prize,RetailerName
I want to display the name of product,prize and retailername.This is my query.
select ProductName,Prize,RetailerName from BrandProduct BP
inner join Product P
on P.id = BP.Productid
When user select Brand,only that's brand record should come.So I changed a query.It is working properly
create proc_sample
#Brandid int = null
as
begin
select ProductName,Prize,RetailerName from BrandProduct BP
inner join Product P
on P.id = BP.Productid
inner join Brand B
on B.id = BP.Brandid
where (#Brandid is null or BP.Brandid= #Brandid)
End
When user will not select brand,join of Brand should not come.Any Idea?I know Dynamic sql come into picture.Any alternative to dynamic sql

No need to join Brand table, because BrandProduct table contains BrandId.
select ProductName,Prize,RetailerName from BrandProduct BP
inner join Product P
on P.id = BP.Productid
where (#Brandid is null or BP.Brandid = #Brandid)

Related

Access Subquery On mulitple conditions

This SQL query needs to be done in ACCESS.
I am trying to do a subquery on the total sales, but I want to link the sale to the province AND to product. The below query will work with one or the other: (po.product_name = allp.all_products) AND (p.province = allp.all_province); -- but it will no take both.
I will be including every month into this query, once I can figure out the subquery on with two criteria.
Select
p.province as [Province],
po.product_name as [Product],
all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id)
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province
)
as allp
on (po.product_name = allp.all_products) AND (p.province = allp.all_province);
Make the first select sql into a table by giving it an alias and join table 1 to table 2. I don't have your table structure or data to test it but I think this will lead you down the right path:
select table1.*, table2.*
from
(Select
p.province as [Province],
po.product_name as [Product]
--removed this ,all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id) table1
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province --check your group by, I dont think you want pp1.price here if you want to aggregate
) as table2 --changed from allp
on (table1.product = table2.all_products) AND (table1.province = table2.all_province);

How to replace only some Guids from a table to another, under conditions

I've created a View, but I wanted to change some of the result's Guids to Guids from another table, I want it directly in my view Result and I Don't know how?
select p.[Guid], c.[Guid] detailsGuid
INTO #temp1
from
ret_PayrollCalculationCommands s INNER JOIN
ret_PayrollCalculations p ON p.CalculationCommandGuid = s.Guid INNER JOIN
ret_vwPayrollCalculationDetails c ON c.CalculationGuid = p.Guid
you should INNER JOIN your table with same table that has target Guid column with another condition:
SELECT
p.[Guid],
c.[Guid] detailsGuid,
pMainCalculation.[Guid] AS [TargetGuid]
INTO #temp1
FROM
ret_PayrollCalculationCommands s INNER JOIN
ret_PayrollCalculations p ON p.CalculationCommandGuid = s.Guid INNER JOIN
ret_PayrollCalculations pMainCalculation ON pMainCalculation.CalculationCommandGuid = p.CalculationCommandGuid AND pMainCalculation.MainCalculation = 1 INNER JOIN
ret_vwPayrollCalculationDetails c ON c.CalculationGuid = p.Guid

SQL Filtering rows with no duplicate value

Hi so I'm new to SQL and I'm trying to find a way in which I can obtain only the rows that have values that are not duplicate to each other in a specific column of table.
For example the Table below is called T1 and contains:
ID|Branch ID
1 444
2 333
3 444
4 111
5 555
6 333
The result I want will be
ID|Branch ID
4 111
5 555
So only showing non duplicate rows
Edit: I want to apply this to a large relational code. Here is a snippet of where I want it to be added
FROM dbo.LogicalLine
INNER JOIN dbo.Page ON dbo.LogicalLine.page_id = dbo.Page.id
INNER JOIN dbo.Branch ON dbo.LogicalLine.branch_id = dbo.Branch.id
The table LogicalLine will have a column called branch_id containing duplicate id values. I wish to filter those out showing only the non-duplicate branch_id like above example then INNER JOIN the Branch table into the LogicalLine which I have done.
Added -Full Code here:
SELECT
(SELECT name
FROM ParentDevice
WHERE (Dev1.type NOT LIKE '%cable%') AND (id = Dev1.parent_device_id))T1_DeviceID,
(SELECT name
FROM Symbol
WHERE (id = CP1.symbol_id) AND (type NOT LIKE '%cable%'))T1_DeviceName,
(SELECT name
FROM Location
WHERE (id = Page.location_id))T1_Location,
(SELECT name
FROM Installation
WHERE (id = Page.installation_id))T1_Installation,
(SELECT name
FROM ParentDevice
WHERE (Dev2.type NOT LIKE '%cable%') AND (id = Dev2.parent_device_id))T2_DeviceID,
(SELECT name
FROM Symbol
WHERE ( id = CP2.symbol_id) AND (type NOT LIKE '%cable%'))T2_DeviceName,
(SELECT name
FROM Location
WHERE (id = PD2.location_id))T2_Location,
(SELECT name
FROM Installation
WHERE (id = Page.installation_id))T2_Installation,
(SELECT devicefamily
FROM Device
WHERE (type LIKE '%cable%') AND (id = SymCable.device_id))CablePartNumber,
(SELECT name
FROM ParentDevice
WHERE (id = DevCable.parent_device_id) AND (DevCable.type LIKE '%cable%'))CableTag
FROM dbo.LogicalLine
INNER JOIN dbo.Page ON dbo.LogicalLine.page_id = dbo.Page.id
INNER JOIN dbo.Branch ON dbo.LogicalLine.branch_id = dbo.Branch.id
LEFT OUTER JOIN dbo.Symbol AS SymCable ON dbo.LogicalLine.cable_id = SymCable.id
LEFT OUTER JOIN dbo.Device AS DevCable ON SymCable.device_id = DevCable.id
LEFT OUTER JOIN dbo.ParentDevice AS ParentCable ON DevCable.parent_device_id = ParentCable.id
INNER JOIN dbo.SymbolCP AS CP1 ON dbo.Branch.cp1_id = CP1.id
INNER JOIN dbo.SymbolCP AS CP2 ON dbo.Branch.cp2_id = CP2.id
INNER JOIN dbo.Symbol AS S1 ON CP1.symbol_id = S1.id
INNER JOIN dbo.Symbol AS S2 ON CP2.symbol_id = S2.id
INNER JOIN dbo.Device AS Dev1 ON S1.device_id = Dev1.id
INNER JOIN dbo.Device AS Dev2 ON S2.device_id = Dev2.id
INNER JOIN dbo.ParentDevice AS PD1 ON Dev1.parent_device_id = PD1.id
INNER JOIN dbo.ParentDevice AS PD2 ON Dev2.parent_device_id = PD2.id
INNER JOIN dbo.Location AS L1 ON PD1.location_id = L1.id
INNER JOIN dbo.Location AS L2 ON PD2.location_id = L2.id
INNER JOIN dbo.Installation AS I1 ON L1.installation_id = I1.id
INNER JOIN dbo.Installation AS I2 ON L2.installation_id = I2.id
WHERE
(PD1.project_id = #Projectid) AND (dbo.LogicalLine.drawingmode LIKE '%Single Line%');
Select Id, BranchId from table t
Where not exists
(Select * from table
where id != t.Id
and BranchId = t.BranchId)
or
Select Id, BranchId
From table
Group By BranchId
Having count(*) == 1
EDIT: to modify as requested, simply add to your complete SQL query a Where clause:
Select l.Id BranchId, [plus whatever else you have in your select clause]
FROM LogicalLine l
join Page p ON p.id = l.page_Id
join Branch b ON b.Id = l.branch_id
Group By l.branch_id, [Plus whatever else you have in Select clause]
Having count(*) == 1
or
Select l.Id BranchId, [plus whatever else you have in your select clause]
FROM LogicalLine l
join Page p on p.id = l.page_Id
join Branch b on b.Id = l.branch_id
Where not exists
(Select * from LogicalLine
where id != l.Id
and branch_id = l.branch_id)

Sqlite select statement from 2 tables

I have two tables: Products and CategoryProducts.
CategoryProducts contains:
PrdID Category
-----------------
Products contains:
PrdID Barcode Url
-----------------------
I have Product's Barcode value, for example 111111.
Need to select all Products.Url with the same Category, as this item's
Category.
Having Products.PrdID, i can get all needed PrdID's from CategoryProducts like this:
select distinct c1.PrdID
from CategoryProduct_MM c1
where c1.CategoryID in (select c2.CategoryID
from CategoryProduct_MM c2
where c2.PrdID = 175)
Based on your comment, this is what you want:
SELECT t.Url
FROM Products t
INNER JOIN CategoryProducts s
ON(s.PrdID = t.PrdID)
WHERE s.CategoryID = (select p.categoryID from CategoryProducts p
INNER JOIN Products f ON(p.prdID = f.prdID)
WHERE f.barcode = 42244)
Selects all the URLS , that their users are in the same category as PrdID ->175
SELECT
p.url
FROM
Products p
JOIN
CategoryProducts cp ON cp.PrdID = p.id
// WHERE p.id = 175
GROUP BY p.url, p.Category
And if you need add comment query
SELECT p.Url, p.PrdID
FROM Products p
JOIN CategoryProduct cp
ON p.PrdID = cp.PrdID
JOIN
Category c on c.id = cp.CategoryID AND cp.PrdID =175
// you need join table Category
Show me your Category table structure

How to select top when already selected fields

Just wanted to ask how to add a 'select top 1 *' when I've already selected fields from a list? I seen examples in other codes but don't quite get it. Thought will be easier if see it in a code I constructed.
Below is an example of a query I have:
select frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId, per.TitleID, per.surname, per.FirstName, per.PersonTypeId, tor.PersonID, tor.Reference
from package pk
inner join product p on p.packageid = pk.packageid
inner join productperson pp on pp.productid = p.productid
inner join person per on per.personid = pp.personid
left join flightlogicalseat fls on fls.productpersonid = pp.productpersonid
inner join TourOperatorReference tor on tor.PersonID = per.PersonId
inner join FlightReservationPassenger frp on frp.ProductPersonID = pp.ProductPersonId
where pk.Reference LIKE '%'
and ProductTypeId =1
Simply try to use TOP keyword like this:
select TOP 1 frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId, per.TitleID,
You can just wrap your existing query in new query:
SELECT TOP 1 * FROM
(select frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId, per.TitleID, per.surname, per.FirstName, per.PersonTypeId, tor.PersonID, tor.Reference
from package pk
inner join product p on p.packageid = pk.packageid
inner join productperson pp on pp.productid = p.productid
inner join person per on per.personid = pp.personid
left join flightlogicalseat fls on fls.productpersonid = pp.productpersonid
inner join TourOperatorReference tor on tor.PersonID = per.PersonId
inner join FlightReservationPassenger frp on frp.ProductPersonID = pp.ProductPersonId
where pk.Reference LIKE '%'
and ProductTypeId =1) t