I'm running this SQL statement:
SELECT
s.indx, S.custid, S.date, S.qty, S.item, S.price, S.extprice,
S.salestax, S.linetotal, S.salenbr, C.company, P.MOP
FROM
sales S
JOIN
cust C ON S.custid = C.custid
JOIN
pmts P ON S.salenbr = p.salenbr
WHERE
(s.salenbr = 16749)
It's returning this result set:
indx custid date qty item price extprice salestax linetotal salenbr company MOP
170835 695 2021-09-27 10:00:44.000 1.00 1X4X12 7.85 7.85 0.75 8.60 16749 COUNTER SALE CS
170835 695 2021-09-27 10:00:44.000 1.00 1X4X12 7.85 7.85 0.75 8.60 16749 COUNTER SALE CC
170836 695 2021-09-27 10:00:44.000 1.00 1X6X12 11.62 11.62 1.10 12.72 16749 COUNTER SALE CS
170836 695 2021-09-27 10:00:44.000 1.00 1X6X12 11.62 11.62 1.10 12.72 16749 COUNTER SALE CC
I want to just pull the rows where the method of payment "MOP" is different. I'm using the data to run a report and need it just with distinct or unique MOP's.
Thank You
You could use ROW_NUMBER here an arbitrarily take the "first" record from each MOP group, according to some order:
WITH cte AS (
SELECT s.indx, S.custid, S.date, S.qty, S.item, S.price, S.extprice,
S.salestax, S.linetotal, S.salenbr, C.company, P.MOP,
ROW_NUMBER() OVER (PARTITION BY P.MOP ORDER BY S.date) rn
FROM sales S
INNER JOIN cust C ON S.custid = C.custid
INNER JOIN pmts P ON S.salenbr = P.salenbr
WHERE S.salenbr = 16749
)
SELECT indx, custid, date, qty, item, price, exitprice,
salestax, linetotal, salenbr, company, MOP
FROM cte
WHERE rn = 1;
So, what you want to do will probably work better using a Common Table Expression or CTE. Something like this:
WITH CTE_Sales AS
(
SELECT
s.indx, S.custid, S.date, S.qty, S.item, S.price, S.extprice,
S.salestax, S.linetotal, S.salenbr, C.company, P.MOP,
COUNT(1) AS salesCount
FROM
sales S
JOIN
cust C ON S.custid = C.custid
JOIN
pmts P ON S.salenbr = p.salenbr
GROUP BY
s.indx, S.custid, S.date, S.qty, S.item, S.price, S.extprice,
S.salestax, S.linetotal, S.salenbr, C.company, P.MOP
)
SELECT
indx, custid, date, qty, item, price, extprice,
salestax, linetotal, salenbr, company
FROM
CTE_Sales
GROUP BY
indx, custid, date, qty, item, price, extprice,
salestax, linetotal, salenbr, company
HAVING
salesCount > 1
What this does is that the CTE contains all your data, which makes it easier to not deal with joins every time. You've also done a group by so you know how many records you have for the same sale.
Then, when you pull the data, you're grouping the records without the MOP. Since the first record is grouped with MOP and the second is grouped without, you know that the MOPs are different.
Me thinks I need to go back and do some redesigning in my code and data tables SMor you are correct about the details vs. the transactions. Live and learn :) Thanks to you all for taking the time to respond. I work solo so It's always great to hear other comments and ideas. Thanks!
Related
I'm having a heck of a time getting my query to work properly. I have 3 tables (ORDERS, ORDERSITEMS, ORDERPAYMENTS) that I'm trying to build a report for monthly sales taxes due which will look like this:
MONTH YEAR TOTAL RECEIPTS EXEMPT RECEIPTS NON-EXEMPT RECEIPTS SALES TAX
1 2020 $5,000 $4,500 $500 $31.25
TOTAL RECEIPTS: To get this number you have to add together all of the C.OrderPayments_Amount for the given time frame, in this case MONTH(C.OrderPayments_Date) = 1 AND YEAR(C.OrderPayments_Date) = 2020
EXEMPT RECEIPTS: You have to determine if an item is taxable (eg. digital files are not sales taxable, physical prints are). To get this you have determine if the item is taxable by checking the B.OrdersItems_ItemChargeSalesTax. If this field is 0 it does not get charged sales tax and if it is 1 then it does. You then have to filter to only get ORDERSITEMS for the Jan 2020 time frame using by grouping by the C.OrderPayments_Date column that have the B.OrdersItems_ItemChargeSalesTax = 0 and finally add the B.OrdersItems_Total together.
NON-EXEMPT RECEIPTS: To get this number you do the same exact thing for EXEMPT RECEIPTS above except you look for B.OrdersItems_ItemChargeSalesTax = 1 and add the B.OrdersItems_Total together.
SALES TAX: To get this number you use the same date filter as before MONTH(C.OrderPayments_Date) = 1 AND YEAR(C.OrderPayments_Date) = 2020 and then add the A.Order_SalesTax column together.
The first query I have listed is working fine to bring me the total taxes paid and the total income for the month (right now I have hard coded the WHERE statement but that is going to be applied via filters on my list page). What I
need to get from the ORDERSITEMS table is a field called ORDERSITEMS_ItemTotal and sum that field so it is a single line entry on the query. I have seen another person do a nested join. Any suggestions would be greatly appreciated.
**ORDERS TABLE "A"**
Order_ID,
Order_SalesTax,
stateTaxAbbreviation
**ORDERSITEMS TABLE "B"**
Order_FK,
OrdersItems_ItemChargeSalesTax,
OrdersItems_Total
NOTE: In the ORDERSITEMS table a single Order_FK may appear several times as there can be many items on an order
**ORDERPAYMENTS TABLE "C"**
Order_FK,
OrderPayments_PaymentDate,
OrderPayments_Amount
NOTE: In the ORDERPAYMENTS table a single Order_FK may appear several times as there can be multiple payments on an order
While writing this out it seems to be an easy task but when I attempt to put it all together the numbers are wrong because it is adding entries multiple times because there are multiple items on an order and thus it is adding the total payment every time.
Here is the code that I've been tinkering with and I would really appreciate any guidance. Thank you in advance and hopefully I've explained my situation clearly enough.
Select
a.stateTaxAbbreviation AS StateAbbr,
MONTH(c.OrderPayments_Date) AS PaymentMonth,
YEAR(c.OrderPayments_Date) AS PaymentYear,
SUM(c.OrderPayments_Amount) AS TotalPayments,
SUM(a.Order_SalesTax) AS sales_tax
FROM dbo.ORDERS a
INNER JOIN ORDERPAYMENTS as c ON c.Order_FK = a.Order_ID
LEFT OUTER JOIN ORDERITEMS b on b.Order_FK = a.Order_ID
WHERE a.stateTaxAbbreviation = 'MA' AND Month(b.OrderPayments_Date) = 1 AND YEAR(b.OrderPayments_Date) = 2020
GROUP BY stateTaxAbbreviation , MONTH(c.OrderPayments_Date), Year(c.OrderPayments_Date)
You should probably write 2 queries, one where you join the ORDERPAYMENTS table, and another where you join the ORDERITEMS table. Then you can combine them with UNION ALL.
Something like this:
SELECT StateAbbr,PaymentMonth,PaymentYear,SUM(TotalPayments),SUM(sales_tax),SUM(OrdersItems_Total)
FROM (
Select
a.stateTaxAbbreviation AS StateAbbr,
MONTH(c.OrderPayments_Date) AS PaymentMonth,
YEAR(c.OrderPayments_Date) AS PaymentYear,
SUM(c.OrderPayments_Amount) AS TotalPayments,
SUM(a.Order_SalesTax) AS sales_tax,
0 as OrdersItems_Total
FROM dbo.ORDERS a
INNER JOIN ORDERPAYMENTS as c ON c.Order_FK = a.Order_ID
WHERE a.stateTaxAbbreviation = 'MA' AND Month(c.OrderPayments_Date) = 1 AND YEAR(c.OrderPayments_Date) = 2020
GROUP BY stateTaxAbbreviation , MONTH(c.OrderPayments_Date), Year(c.OrderPayments_Date)
UNION ALL
Select
a.stateTaxAbbreviation AS StateAbbr,
MONTH(c.OrderPayments_Date) AS PaymentMonth,
YEAR(c.OrderPayments_Date) AS PaymentYear,
0 AS TotalPayments,
0 AS sales_tax,
SUM(B.OrdersItems_Total) as OrdersItems_Total
FROM dbo.ORDERS a
INNER JOIN ORDERITEMS b on b.Order_FK = a.Order_ID
WHERE a.stateTaxAbbreviation = 'MA' AND Month(b.OrderPayments_Date) = 1 AND YEAR(b.OrderPayments_Date) = 2020
GROUP BY stateTaxAbbreviation , MONTH(c.OrderPayments_Date), Year(c.OrderPayments_Date)
)t
GROUP BY StateAbbr,PaymentMonth,PaymentYear
Thanks to Wouter for pointing me in the right direction. After looking at his suggestion I went back and evaluated what I needed and I create the solution that worked that was based on his idea. Thanks for your patience Wouter, your insight helped a lot!
Select StateAbbr, OrderYear, OrderMonth, SUM(TotalSales) As TotalSales, SUM(TotalSales)-SUM(TaxableRevenue) As ExemptRevenue, SUM(TaxableRevenue) As TaxableRevenue, SUM(SalesTax) As SalesTax
FROM (
Select
/*Get Total Sales and Total Sales Tax Collected*/
a.stateTaxAbbreviation AS StateAbbr,
MONTH(a.Order_Date) As OrderMonth,
YEAR(a.Order_Date) As OrderYear,
SUM((a.Order_TotalBaseSale + a.Order_Shipping) - (((a.Order_PercentDiscount*a.Order_TotalBaseSale)/100) + a.Order_DollarDiscount)) As TotalSales,
SUM(0) As ExemptRevenue,
Sum(0) As TaxableRevenue,
SUM(a.Order_SalesTax) AS SalesTax
FROM dbo.ORDERS a
WHERE a.Order_Status != 'Cancelled'
Group By a.stateTaxAbbreviation, MONTH(a.Order_Date), YEAR(a.Order_Date)
UNION ALL
Select
/*GET EXEMPT ORDERS*/
a.stateTaxAbbreviation AS StateAbbr,
MONTH(a.Order_Date) As OrderMonth,
YEAR(a.Order_Date) As OrderYear,
Sum(0) As TotalSales,
Sum(OrdersItems_ItemTotal) AS ExemptRevenue,
Sum(0) AS TaxableRevenue,
Sum(0) As SalesTax
FROM ORDERSITEMS b
LEFT JOIN ORDERS a ON Order_ID = b.Order_FK
WHERE b.OrdersItems_ItemChargeSalesTax = 0 and a.Order_Status != 'Cancelled'
Group By a.stateTaxAbbreviation, MONTH(a.Order_Date), YEAR(a.Order_Date)
UNION ALL
Select
/*GET NON-EXEMPT ORDERS*/
a.stateTaxAbbreviation AS StateAbbr,
MONTH(a.Order_Date) As OrderMonth,
YEAR(a.Order_Date) As OrderYear,
SUM(0) As TotalSales,
SUM(0) AS ExemptRevenue,
Sum(OrdersItems_ItemTotal) AS TaxableRevenue,
Sum(0) As SalesTax
FROM ORDERSITEMS b
LEFT JOIN ORDERS a ON Order_ID = b.Order_FK
WHERE b.OrdersItems_ItemChargeSalesTax <> 0 and a.Order_Status != 'Cancelled'
Group By a.stateTaxAbbreviation, MONTH(a.Order_Date), YEAR(a.Order_Date)
)t
GROUP BY StateAbbr, OrderMonth, OrderYear
ORDER BY StateAbbr ASC, OrderYear DESC, OrderMonth ASC
I need to write a query that pulls only employees who are missing their degrees entered into our ERP system. For example, we have a table where their degrees are listed.
Employee ID FName LName Degree
100 John Smith BA
200 Bill Jones BS
300 Alice Waters BA
300 Alice Waters MA
400 Joe Lewis MA
They would like me to pull from this table, only Joe Lewis because he doesn't have a bachelors degree entered in the system, but since he has a master's degree, the assumption is he also has a bachelor's, and someone just missed entering it into the system.
I've tried using EXCEPT filtering on Bachelors degrees, however, that still yields
Employee ID FName LName Degree
300 Alice Waters MA
400 Joe Lewis MA
And I don't want Alice in the list because she has a bachelors degree coded into the system.
Any thoughts on how I might approach this would be much appreciated.
If this is just for MA and BA, you can use conditional aggregation:
select empid, fname, lname
from t
group by empid, fname, lname
having sum(case when degree = 'BA' then 1 else 0 end) = 0 and
sum(case when degree = 'MA' then 1 else 0 end) > 0;
Or, you can use exists:
select t.*
from t
where degree = 'MA' and
not exists (select 1
from t t2
where t2.empid = t.empid and t2.degree = 'BA'
);
You could go with a left join of the Masters subset against the Bachelors subset:
select m.EmployeeId, m.FName, m.LName
from (select * from Employee where Degree in ('MA')) m
left join (select * from Employee where Degree in ('BA', 'BS')) b
on m.EmployeeId = b.EmployeeId
where b.EmployeeId is null
Maybe you should consider to use a query without subqueries...
SELECT E.EmployeeId, E.FName, E.LName
FROM Employee AS E
LEFT JOIN Employee AS F ON (E.EmployeeId = F.EmployeeId AND F.Degree = 'BA')
WHERE E.Degree <> 'BA' AND F.EmployeeId IS NULL
Or (if BS must be considered as well) :
SELECT E.EmployeeId, E.FName, E.LName
FROM Employee AS E
LEFT JOIN Employee AS F ON (E.EmployeeId = F.EmployeeId AND F.Degree IN('BA','BS'))
WHERE E.Degree <> 'BA' AND E.Degree <> 'BS' AND F.EmployeeId IS NULL
keep in mind subqueries are often slow, depending on how many row is concerned...
EDIT I updated my question with a SQL Fiddle Sample http://sqlfiddle.com/#!15/8d88b/1
I'm currently making a report from a database records but I don't know how my query should look like, first of all I have 2 tables. Application Forms, and a table for Login Hours of each user
forms
->id
->agent_id
->SomeInfo
->created_at
loginhours
->id
->user_id
->loginhours (decimal)
->created_at
And I have report with the following columns
UserID, TotalLoginHours, TotalApplication, Application Per Hour (aph), Revenue Per Hour (rph)
So right now I have this query
SELECT a.agent_id, SUM(b.loginhours) as TotalLoginHours, COUNT(a.id) as TotalApplication, SUM(b.loginhours) / COUNT(a.id) as ApplicationPerHour, (SUM(b.loginhours) / COUNT(a.id)) * 1.75 as RPH
FROM forms a
INNER JOIN loginhours b ON a.agent_id = b.user_id WHERE a.created_at = '2015-07-17'
GROUP BY a.agent_id
Note that user_id and agent_id is the same.
I want to get the result based on the date selected, example 2015-07-17 I got results but my problem is the loginhours is being SUM based on the number of application for each user. So for example the user1 has 2 records on forms table and his loginhours from 2015-07-17 is 2 then in my result the loginhours becomes 4 which is wrong, I think it is on my GROUP BY statement. Can you help me how to properly query this?
Thanks
I don't know if this is a good practice but somehow I figured it out with
SELECT a.agent_id
,(SELECT SUM(loginhours)
FROM loginhours
WHERE user_id = a.agent_id
AND created_at = '2015-07-17'
GROUP BY created_at) as TotalLoginHours
,COUNT(a.id) as TotalApplication
,(SELECT SUM(loginhours)
FROM loginhours
WHERE user_id = a.agent_id
AND created_at = '2015-07-17'
GROUP BY created_at) / COUNT(a.id) as ApplicationPerHour
,((SELECT SUM(loginhours)
FROM loginhours
WHERE user_id = a.agent_id
AND created_at = '2015-07-17'
GROUP BY created_at) / COUNT(a.id)) * 1.75 as RPH
FROM forms a
INNER JOIN loginhours b
ON a.agent_id = b.user_id
WHERE a.created_at = '2015-07-17'
GROUP BY a.agent_id;
SELECT user_id, loginhours, applications,
loginhours/applications as applications_per_hour,
loginhours/applications * 1.75 as rph
FROM
(
SELECT user_id, SUM(loginhours) as loginhours
FROM loginhours
WHERE created_at = '2015-07-17'
GROUP BY user_id
)hours
JOIN
(
SELECT agent_id, COUNT(DISTINCT id) as applications
FROM forms
WHERE created_at = '2015-07-17'
GROUP BY agent_id
)applications
ON hours.user_id = applications.agent_id
I'm currently facing a problem when calculating the standard deviation in an SQL Server statement.
My problem: I have two tables.
T1:
Reg_Month
---------
1
2
3
...
T2:
Product Month Consumption
-------------------------------
ProdA 1 200
ProdB 1 10
ProdA 1 300
ProdC 2 100
ProdA 2 200
...
Now what I want is something like this, for calculating my STDEV over a year:
Reg_Month Product Month Sum
---------------------------------
1 ProdA 1 500
1 ProdB 1 10
1 ProdC 1 0
2 ProdA 2 200
2 ProdB 2 0
2 ProdC 2 0
So now I don't need this table to be set up, but I need to calculate the STDEV and AVG of the column "Sum" for each Product. The Problem is to include the NULLS.
This gives me the table I want for ProdA, but with NULLS whenever there was no consumption:
SELECT *
FROM T1
FULL JOIN (SELECT Product, Month, SUM(Consumption) AS Sum,
FROM T2
WHERE (Product = 'ProdA')
GROUP BY Month, Product) sub ON (T1.Reg_Month = T2.Month)`
This gives me the STDEV:
SELECT Stdev(Sum)
FROM
(SELECT *
FROM T1
FULL JOIN (SELECT Product, Month, SUM(Consumption) AS Sum,
FROM T2
WHERE (Product = 'ProdA')
GROUP BY Month, Product) sub ON (T1.Reg_Month = T2.Month)) sub
WHERE Product = 'ProdA'`
But the problem is, that it doesn't give me the correct STDEV for the entire year, if there is a month in which there was no consumption, because the NULLs (that appear due to the join) are ignored.
My approaches:
ISNULL():
SELECT Stdev(Sum)
FROM
(SELECT *
FROM T1
FULL JOIN (SELECT Product, Month, ISNULL(SUM(Consumption), 0) AS Sum,
FROM T2
WHERE (Product = 'ProdA')
GROUP BY Month, Product) sub ON (T1.Reg_Month = T2.Month)) sub
WHERE Product = 'ProdA'`
doesn't work (maybe because the null is generated after the join?)
CASE:
SELECT Stdev(Sum)
FROM
(SELECT *
FROM T1
FULL JOIN (SELECT Product, Month, CASE WHEN SUM(Consumption) IS NULL THEN 0 ELSE Sum(Consumption) END AS Sum,
FROM T2
WHERE (Product = 'ProdA')
GROUP BY Month, Product) sub ON (T1.Reg_Month = T2.Month)) sub
WHERE Product = 'ProdA'
doesn't work (probably the same reason)
I hope I was able to illustrate my example properly. Now, do you have any idea how get the right results for the STDEV?
Your input would be greatly appreciated!
Thanks a lot,
Clemens
if you are only doing one product at a time
SELECT sum(isnull(T2month.MonthSum ,0))
, Stdev(isnull(T2month.MonthSum ,0))
FROM T1
LEFT JOIN
(select Month, sum(Consumption) as MonthSum
from T2
where Product = 'ProdA'
group by Month) T2month
ON T1.Reg_Month = T2month.Month
for all products
SELECT Product.Name
, sum(isnull(T2month.MonthSum ,0))
, Stdev(isnull(T2month.MonthSum ,0))
FROM T1
cross apply
(values ('ProdA'), ('ProdB'), ('ProdC')) Product(Name)
LEFT JOIN
(select Product, Month, sum(Consumption) as MonthSum
from T2
group by Product, Month) T2month
ON T1.Reg_Month = T2month.Month
AND T2month.Product = Product.Name
Group By Product.Name
Hey OP left join does NOT leave out periods
That is what a left join does
select lj.val, isnull(jj.val,0)
from ( values (1), (2), (3), (4) ) lj(val)
left join ( values (1), (2), (4) ) jj(val)
on lj.val = jj.val
order by lj.val
I have a pivot query which works well until now, but there has been a requirement where I need to specify a condition in the unpivot query which would omit the rows and column values which are not matching with the condition and displays me null. However, I need to show those value as a part of final results.
I have tried to do union to include the missing values, but won't get the desired output. I am pasting my pivot query below along with the results which are displayed and which needs to be displayed as a result. Please help.
CURRENT QUERY
SELECT
OrderID, AccessName, Address1, Postcode
, Gen_Instr_1 AS "General Instructions for Install"
,Supplier_Name_1 AS "Supplier Name"
,Install_Job_Name_1 AS "Install Jobs"
,Install_Job_Status_1 AS "Install Job Status"
,Install_Job_Name_2 AS "EPR Jobs"
,Install_Job_Status_2 AS "EPR Job Status"
FROM
(
SELECT
OrderID, AccessName, Address1, Postcode
, col+'_'+CAST(rn AS VARCHAR(10)) col,
val
FROM
(
SELECT o.OrderID,
CAST(js.JobStatusID AS VARCHAR(50)) JobStatusId
, CAST(p.Name AS VARCHAR(50)) Install_Job_Name
, o.AccessName, o.Address1, o.Postcode
, CAST(oj.GeneralInstructions AS VARCHAR(50)) Gen_Instr
, CAST(s.CompanyName AS VARCHAR(50)) Supplier_Name
, oj.SupplierID
, CAST(js.Name AS VARCHAR(50)) Install_Job_Status
, ROW_NUMBER() OVER(PARTITION BY o.OrderID ORDER BY o.OrderID) rn
FROM
NEPCCO.Orders o
INNER JOIN NEPCCO.Clients c ON o.ClientID = c.ClientID
INNER JOIN NEPCCO.OrderJobs oj ON o.OrderID = oj.OrderID
INNER JOIN NEPCCO.Suppliers s ON oj.SupplierID = s.SupplierID
INNER JOIN NEPCCO.Products p ON oj.ProductID = p.ProductID
INNER JOIN NEPCCO.OrderStatus os ON o.OrderStatusID = os.OrderStatusID
INNER JOIN NEPCCO.JobStatus js ON oj.JobStatusID = js.JobStatusID
WHERE
o.OrderID IN (6981,6860,6982,6983) AND
(p.ProductID IN (35,36,37,38,38,40,41,42,43) OR p.ProductID IN (33,34))
AND s.CompanyName = 'Northern Gas Heating Ltd'
) d
UNPIVOT
(
val
FOR col IN (JobStatusId, Gen_Instr, Supplier_Name,Install_Job_Name, Install_Job_Status)
) un
) s
PIVOT
(
MAX(val)
FOR col IN (JobStatusID_1, Gen_Instr_1, Supplier_Name_1, Install_Job_Name_1, Install_Job_Status_1,
JobStatusID_2, Gen_Instr_2,
Install_Job_Name_2, Install_Job_Status_2
)
) piv
CURRENT RESULTS - SEE THE NULL VALUES IN LAST TWO COLUMNS
OrderID AccessName Address1 Postcode SupplierName Install Jobs Install Job Status EPR Jobs EPR Job Status
6981 Mrs Cespedes 73 Mill Lane WV11 1DQ Northern Gas Heating Ltd GC1 - 28 Complete NULL NULL
6983 Ms A Mirza 122 Pendleford Avenue WV6 9EN Northern Gas Heating Ltd GC1 - 28 Complete NULL NULL
Now, if I runn the inner unpivot query as below, I get following records/rows
Inner unpivot query
SELECT o.OrderID,
CAST(js.JobStatusID AS VARCHAR(50)) JobStatusId
, CAST(p.Name AS VARCHAR(50)) Install_Job_Name
, o.AccessName, o.Address1, o.Postcode
, CAST(oj.GeneralInstructions AS VARCHAR(50)) Gen_Instr
, CAST(s.CompanyName AS VARCHAR(50)) Supplier_Name
, oj.SupplierID
, CAST(js.Name AS VARCHAR(50)) Install_Job_Status
, ROW_NUMBER() OVER(PARTITION BY o.OrderID ORDER BY o.OrderID) rn1
FROM
NEPCCO.Orders o
INNER JOIN NEPCCO.Clients c ON o.ClientID = c.ClientID
INNER JOIN NEPCCO.OrderJobs oj ON o.OrderID = oj.OrderID
INNER JOIN NEPCCO.Suppliers s ON oj.SupplierID = s.SupplierID
INNER JOIN NEPCCO.Products p ON oj.ProductID = p.ProductID
INNER JOIN NEPCCO.OrderStatus os ON o.OrderStatusID = os.OrderStatusID
INNER JOIN NEPCCO.JobStatus js ON oj.JobStatusID = js.JobStatusID
WHERE
o.OrderID IN (6981,6860,6982,6983) AND
(p.ProductID IN (35,36,37,38,38,40,41,42,43) OR p.ProductID IN (33,34))
that produces following results
OrderID AccessName Address1 Postcode SupplierName Install Jobs Install Job Status EPR Jobs EPR Job Status
6860 6 AW EPR 01625555555 1 Gorsey Road SK9 5DU OGP 14 Cancelled 1
6981 4 AW EPR Mrs Cespedes 73 Mill Lane WV11 1DQ Ian Barnhurst 1 Complete 1
6981 4 GC1 - 28 Mrs Cespedes 73 Mill Lane WV11 1DQ Northern Gas Heating Ltd 403 Complete 2
6982 4 AW EPR Installer Mr N Singh 115 Oxbarn Avenue WV3 7HQ Northern Gas 414 Complete 1
6983 4 AW EPR Installer Ms A Mirza 122 Pendleford Avenue WV6 9EN Northern Gas 414 Complete 1
6983 4 GC1 - 28 Ms A Mirza 122 Pendleford Avenue WV6 9EN Northern Gas Heating Ltd 403 Complete 2
Problem
Now, if you see carefully the above result set, which has the order id's 6860, 6982, which is not included as a part of above result because it hasn't got the supplier name as Northern Gas Heating Ltd, moreover, the results which are displayed on first query, omits the values from order id's 6981, 6983, also, because they have different suppliers.
Also, if you notice, with the records being displayed in first query, the values with row number 2 are not included.
I tried my best but failed. #bluefeet, your expertise highly required over here. Any clarifications please ask.