SQL reporting query - sql

I have a database with following structure.
CREATE TABLE Party
(
PartyID INT IDENTITY
PRIMARY KEY ,
StatusID INT ,
Weigth INT ,
OldWeigth INT
);
GO
CREATE TABLE PartyLocation
(
PartyLocationID INT IDENTITY
PRIMARY KEY ,
PartyID INT FOREIGN KEY REFERENCES dbo.Party ( PartyID ) ,
LocationID INT ,
Distance INT
);
GO
CREATE TABLE PartyRole
(
PartyRoleID INT IDENTITY
PRIMARY KEY ,
PartyID INT FOREIGN KEY REFERENCES dbo.Party ( PartyID ) ,
RoleID INT
);
with some simple data.
INSERT INTO dbo.Party
( StatusID, Weigth, OldWeigth )
VALUES ( 1, -- StatusID - int
10, -- Age - int
20 -- OldAge - int
),
( 1, 15, 25 ),
( 2, 20, 30 );
INSERT INTO dbo.PartyLocation
( PartyID, LocationID, Distance )
VALUES ( 1, -- PartyID - int
1, -- LocationID - int
100 -- Distance - int
),
( 1, 2, 200 ),
( 1, 3, 300 ),
( 2, 1, 1000 ),
( 2, 2, 2000 ),
( 3, 1, 10000 );
INSERT INTO dbo.PartyRole
( PartyID, RoleID )
VALUES ( 1, -- PartyID - int
1 -- RoleID - int
),
( 1, 2 ),
( 1, 3 ),
( 2, 1 ),
( 2, 2 ),
( 3, 1 );
I want to query the following information
Return sum of Weigth of all parties that has roleID = 1 in PartyRole table
Return sum of OldWeigth of all parties that has statusID = 2
Return sum of distances of all parties that has locationID = 3
Return sum of distances of all parties that has roleID = 2
So the expected results are
FilteredWeigth FilteredOldWeigth FilteredDistance AnotherFilteredDistance
-------------- ----------------- ---------------- -----------------------
45 30 600 3600
Can we write a query that will query each table just once? If no what will be the most optimal way to query the data?

You can try this.
SELECT
FilteredWeigth = SUM(CASE WHEN RoleID = 1 AND RN_P = 1 THEN Weigth END) ,
FilteredOldWeigth = SUM(CASE WHEN StatusID = 2 AND RN_P = 1 THEN OldWeigth END),
FilteredDistance = SUM(CASE WHEN LocationID = 3 AND RN_L = 1 THEN Distance END),
AnotherFilteredDistance = SUM(CASE WHEN RoleID = 2 THEN Distance END)
FROM (
SELECT P.Weigth, P.StatusID, P.OldWeigth, PL.LocationID, PL.Distance, PR.RoleID,
RN_P = ROW_NUMBER() OVER (PARTITION BY P.PartyID ORDER BY PL.PartyLocationID),
RN_L = ROW_NUMBER() OVER (PARTITION BY PL.LocationID ORDER BY PR.PartyRoleID)
FROM Party P
INNER JOIN PartyLocation PL ON P.PartyID = PL.PartyID
INNER JOIN PartyRole PR ON P.PartyID = PR.PartyID
) AS T

the below gives
45 20 300 3600
the third column gives 300 which does not correspond to your expected result.
with q1
as
(
select sum(weigth) FilteredWeigth
from party join partyrole on party.partyid = partyrole.partyid
where partyrole.RoleID = '1'
),
q2 as
(
select sum(weigth) OldWeigth from party where StatusID = '2'
),
q3 as (
select sum(Distance) FilteredDistance
from party join PartyLocation on party.partyid = PartyLocation.partyid
where PartyLocation.locationID = '3'
),
q4 as
(
select sum(Distance) AnotherFilteredDistance
from party join partyrole on party.partyid = partyrole.partyid
join PartyLocation on party.partyid = PartyLocation.partyid
where partyrole.RoleID = '2'
)
select FilteredWeigth,OldWeigth,FilteredDistance,AnotherFilteredDistance
from q1,q2,q3,q4

When Using Individual Queries, you can achieve this using the following
Return sum of Weight of all parties that has roleID = 1 in PartyRole table
SELECT
SUM(Weight) FilteredWeigth
FROM dbo.Party P
WHERE EXISTS
(
SELECT
1
FROM dbo.PartyRole PR
WHERE PR. PartyID = P.PartyID
AND PR.RoleId = 1
)
Return sum of OldWeigth of all parties that has statusID = 2
SELECT
SUM(OldWeigth) FilteredOldWeigth
FROM dbo.Party P
WHERE EXISTS
(
SELECT
1
FROM dbo.PartyRole PR
WHERE PR. PartyID = P.PartyID
AND PR.RoleId = 2
)
Return sum of distances of all parties that has locationID = 3
SELECT
SUM(Distance) FilteredDistance
FROM dbo.PartyLocation
WHERE LocationID = 3
Return sum of distances of all parties that has roleID = 2
SELECT SUM(Distance) FROM PartyLocation PL
WHERE EXISTS
(
SELECT 1 FROM PartyRole PR
WHERE PR.PartyID = PL.PartyID
AND PR.Roleid = 2
)
If you want to get the result of all these in a single result set. then maybe you can try a pivot query. Like this
WITH CTE
AS
(
SELECT
'FilteredWeigth' ColNm,
SUM(Weigth) Val
FROM dbo.Party P
WHERE EXISTS
(
SELECT
1
FROM dbo.PartyRole PR
WHERE PR. PartyID = P.PartyID
AND PR.RoleId = 1
)
UNION
SELECT
'FilteredOldWeigth' ColNm,
SUM(OldWeigth) Val
FROM dbo.Party P
WHERE EXISTS
(
SELECT
1
FROM dbo.PartyRole PR
WHERE PR. PartyID = P.PartyID
AND PR.RoleId = 2
)
UNION
SELECT
'FilteredDistance' ColNm,
SUM(Distance) Val
FROM dbo.PartyLocation
WHERE LocationID = 3
UNION
SELECT
'AnotherFilteredDistance' ColNm,
SUM(Distance) Val FROM PartyLocation PL
WHERE EXISTS
(
SELECT 1 FROM PartyRole PR
WHERE PR.PartyID = PL.PartyID
AND PR.Roleid = 2
)
)
SELECT
*
FROM CTE
PIVOT
(
SUM(Val)
FOR ColNm IN
(
[FilteredWeigth],[FilteredOldWeigth],[FilteredDistance],[AnotherFilteredDistance]
)
)Pvt
The Result Will be

I could think of only three possible options:
Union query with four different select statements as answered by #ab-bennett
Join all tables then use select statements as answered by sarslan
Mix of 1 and 2, based on experiments
Coming to the question you asked:
Can we write a query that will query each table just once?
Assuming best performance is the goal, following could happen in each of the above cases:
All select statements would have their own where clause. This would perform best when where produces few rows compared to the count(*). Note that Joins are terrible for very large tables.
A join is made once, and the desired output is obtained from the same Joined table. This would perform optimal when where produces significant number of rows and the table is not too big to join.
You can mix JOIN / IN / EXISTS / WHERE to optimize your queries based on number of rows you are having in table. This approach could be used when your dataset cardinality might not vary a lot.

Related

SQL Server : split row into multiple rows based on a column value

I have a question regarding splitting rows based on column value
My example data set is :
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
I want to split rows those have some Expense Types such as Car into two rows . Others should remain as one row.
First row Price *70
Second Row Price *30
Returned dataset should be
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
Thanks for your answers in advance
If you want to split more expense types than car you could use:
WITH r AS (
SELECT 'Car' AS ExpenseType, 0.7 AS Ratio
UNION SELECT 'Car' AS ExpenseType, 0.3 AS Ratio
-- add more ExpenseTypes/Ratios here
)
SELECT
t.id,
t.ExpenseType,
t.Price * ISNULL(r.Ratio, 1.0) AS Price
FROM
your_table t
LEFT OUTER JOIN r ON t.ExpenseType = r.ExpenseType
A simple way uses union all:
select id, expensetype, price
from t
where expensetype <> 'Car'
union all
select id, expensetype, price * 0.7
from t
where expensetype = 'Car'
union all
select id, expensetype, price * 0.3
from t
where expensetype = 'Car';
This is not the most efficient method. For that, a cross apply with filtering logic is better:
select t.id, v.*
from t cross apply
(values (NULL, price), ('Car', price * 0.3), ('Car', price * 0.7)
) v(expensetype, price)
where v.expensetype = t.expense_type or
v.expensetype <> 'Car' and t.expense_type is null;
A less simple way is to use an OUTER APPLY
CREATE TABLE YourSampleData
(
Id INT IDENTITY(1,1) PRIMARY KEY,
ExpenseType VARCHAR(30) NOT NULL,
Price INT NOT NULL DEFAULT 0
);
INSERT INTO YourSampleData
(ExpenseType, Price) VALUES
('Car', 100)
,('Hotel', 50)
,('Gold', 1)
;
SELECT Id, ExpenseType
, COALESCE(a.Price, t.Price) AS Price
FROM YourSampleData t
OUTER APPLY
(
SELECT Price * Perc AS Price
FROM (VALUES
('Car',0.3E0), ('Car',0.7E0)
,('Gold',1.618E0)
) AS v(ExpType, Perc)
WHERE t.ExpenseType = v.ExpType
) a
GO
Id | ExpenseType | Price
-: | :---------- | ----:
1 | Car | 30
1 | Car | 70
2 | Hotel | 50
3 | Gold | 1.618
db<>fiddle here
I ran into a similar need, here is my solution.
Problem statement:
My organization is switching from an in-house build system to a third-party system. Numerical values in the original system surpassed the value size that the destination system could handle. The third-party system will not allow us to increase the field size, as a result we need to split the data up into values that do not surpass the field size limit.
Details:
Destination system can only support values under 1 billion (can include a negative sign)
Example:
DROP TABLE IF EXISTS #MyDemoData /* Generate some fake data for the demo */
SELECT item_no = 1, item_description = 'zero asset', amount = 0 INTO #MyDemoData
UNION SELECT item_no = 2, item_description = 'small asset', amount = 5100000
UNION SELECT item_no = 3, item_description = 'mid asset', amount = 510000000
UNION SELECT item_no = 4, item_description = 'large asset', amount = 5100000000
UNION SELECT item_no = 5, item_description = 'large debt', amount = -2999999999.99
SELECT * FROM #MyDemoData
DECLARE #limit_size INT = 1000000000
DROP TABLE IF EXISTS #groupings;
WITH
max_groups AS
(
SELECT max_groups=100
)
,groups AS
(
SELECT 1 AS [group]
UNION ALL
SELECT [group]+1
FROM groups
JOIN max_groups ON 1=1
WHERE [group]+1<=max_groups
)
,group_rows AS
(
SELECT 0 AS [row]
UNION ALL
SELECT [row]+1
FROM group_rows
JOIN max_groups ON 1=1
WHERE [row]+1<=max_groups
)
,groupings AS
(
SELECT [group],[row]
FROM group_rows
CROSS JOIN groups
WHERE [row] <= [group]
)
SELECT * INTO #groupings FROM groupings;
WITH /* Split out items that are under the limit and over the limit */
t1 AS /* Identify rows that are over the limit and by how many multiples over it is */
(
SELECT
item_no
, item_description
, amount
, over_limit = FLOOR(ABS(amount/#limit_size))
FROM #MyDemoData
)
SELECT /* select the items that are under the limit and do not need manipulated */
item_no
, item_description
, amount = CAST(amount AS DECIMAL(16,2))
FROM t1
WHERE ABS([amount]) < #limit_size
UNION ALL /* select the items that are over the limit, join on the groupings cte and calculate the split amounts */
SELECT
item_no
, item_description
, [Amount] = CAST(
CASE
WHEN row != 0 THEN (#limit_size-1) * ([amount]/ABS([amount]))
ELSE (ABS([amount]) - (t1.over_limit * #limit_size) + t1.over_limit) * ([amount]/ABS([amount]))
END AS DECIMAL(16,2))
FROM t1
JOIN #groupings bg ON t1.over_limit = bg.[group]
WHERE ABS([amount]) >= #limit_size
ORDER BY item_no

Where clause on Running total

I have this table which stores containers by region and the number of coffee pouches in each of the containers.
if object_id( 'dbo.Container' ) is not null
drop table dbo.Container
go
create table dbo.Container
(
Id int not null,
Region int not null,
NumberOfCoffeePouches int not null,
constraint pkc_Container__Id primary key clustered(Id asc)
)
go
insert into dbo.Container
( Id , Region , NumberOfCoffeePouches )
values
( 1, 1, 10 ),
( 2, 1, 30 ),
( 3, 1, 5),
( 4, 1, 7),
( 5, 1, 1),
( 6, 1, 3),
( 7, 2, 4),
( 8, 2, 4),
( 9, 2, 4)
I need to list out the container Ids that will be used to fulfill an order of, say 50, coffee pouches. Over supplying is OK.
Here is query I have come up with
declare #RequiredCoffeePouches int = 50
select
sq2.Id,
sq2.NumberOfCoffeePouches,
sq2.RunningTotal,
sq2.LagRunningTotal
from
(
select
sq1.Id,
sq1.NumberOfCoffeePouches,
sq1.RunningTotal,
lag(sq1.RunningTotal, 1, 0) over (order by sq1.Id asc)
as 'LagRunningTotal'
from
(
select
c.Id,
c.NumberOfCoffeePouches,
sum(c.NumberOfCoffeePouches)
over (order by c.Id asc) as 'RunningTotal'
from
dbo.Container as c
where
c.Region = 1
) as sq1
) as sq2
where
sq2.LagRunningTotal <= #RequiredCoffeePouches
It gives the expected result
Id NumberOfCoffeePouches RunningTotal LagRunningTotal
----------- --------------------- ------------ ---------------
1 10 10 0
2 30 40 10
3 5 45 40
4 7 52 45
Question:
Is there a better and more optimized way to achieve this?
Specially the Container table is very large table and I think the sub query sq1 will unnecessarily calculate the RunningTotals for all the containers in the region. I was wondering if there is anyway to have sq1 stop processing more rows once the RunnningTotal exceeds over the #RequiredCoffeePouches.
Two things:
Moving your WHERE clause inside of the relevant sub-select can greatly increase the speed of the query because it'll pull less data. Using your example:
SELECT
sq2.Id,
sq2.NumberOfCoffeePouches,
sq2.RunningTotal,
sq2.LagRunningTotal
FROM
(
SELECT
sq1.Id,
sq1.NumberOfCoffeePouches,
sq1.RunningTotal,
lag(sq1.RunningTotal, 1, 0) over (order by sq1.Id asc) AS 'LagRunningTotal'
FROM
(
SELECT
c.Id,
c.NumberOfCoffeePouches,
SUM(c.NumberOfCoffeePouches) OVER (order by c.Id asc) AS 'RunningTotal'
FROM dbo.Container AS c
WHERE c.Region = 1
) AS sq1
WHERE sq2.LagRunningTotal <= #RequiredCoffeePouches
) AS sq2
CTEs can also improve performance:
;WITH sql1CTE AS (
SELECT
c.Id,
c.NumberOfCoffeePouches,
SUM(c.NumberOfCoffeePouches) OVER (order by c.Id asc) AS 'RunningTotal'
FROM dbo.Container AS c
WHERE c.Region = 1
),
sql2CTE AS (
SELECT
Id,
NumberOfCoffeePouches,
RunningTotal,
lag(RunningTotal, 1, 0) over (order by Id asc) AS 'LagRunningTotal'
FROM sql1CTE
WHERE LagRunningTotal <= #RequiredCoffeePouches
)
SELECT
Id,
NumberOfCoffeePouches,
RunningTotal,
LagRunningTotal
FROM sql2CTE
SQL Server CTE Basics
If you're using SSMS, select "Include Client Statistics" and "Include Actual Execution Plan" to keep track of how your query performs while you're crafting it.

db2 query for multiple cases

I have three queries below (identical except for the first line of the WHERE clauses) that all work perfectly in my script. The first one queries orders for a customer, the 2nd for all orders assigned to a representative and the third are all orders period, across the whole company.
Again, they all work given their respective variables (all variables come from the same page) but I'm trying to fill columns on a table for all 3 cases.
Is there a way I can combine these and create one query that gives me the same values for each respective clause?
So, I would expect all 6 columns returned for one query. This is running on db2 so I don't know the best way to proceed but could I create a larger CASE based query?
//query on orders for this customer
SELECT
count(*) as sales_180Cust,
180/count(*) as velocityCust
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE g.cust = $customer
AND g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;
//orders belonging to representative
SELECT
count(*) as sales_180Rep,
180/count(*) as velocityRep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE g.rep = $rep
AND g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;
//query across ALL orders
SELECT
count(*) as sales_180Company,
180/count(*) as velocityCompany
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;
This would be another way to do this
WITH CTE AS (
SELECT g.cust
, g.rep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE
g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
)
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, 'cust' as query
FROM CTE
WHERE cust = $customer
UNION ALL
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, 'rep' as query
FROM CTE
WHERE rep = $rep
UNION ALL
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, 'all' as query
FROM CTE
which returns e.g.
SALES_180COMPANY VELOCITYCOMPANY QUERY
---------------- --------------- -----
3 60 cust
2 90 rep
5 36 all
select q1.*, q2.*, q3.*
from
(select count(*) as sales_180Cust, 180/count(*) as velocityCust from table(values 1) t(i)) q1
, (select count(*) as sales_180Rep, 180/count(*) as velocityRep from table(values 1, 2) t(i)) q2
, (select count(*) as sales_180Company, 180/count(*) as velocityCompany from table(values 1, 2, 3) t(i)) q3
I amended your FROM and WERE clauses to show the idea.
Lots of ways to do this. UNION would be an obvious way. GROUPING SETs is a bit more clever.
create table orders(date1 int, rep int, cust int,frm int, cvr int, clr int, aci int);
create table dates(acyyyymmdd int, aciso date);
create variable $frm int default 1;
create variable $cvr int default 1;
create variable $clr int default 1;
create variable $customer int default 1;
create variable $rep int default 1;
insert into orders values (1,0,0,1,1,1,1), (1,1,1,1,1,1,1), (1,2,1,1,1,1,1), (1,3,1,1,1,1,1), (1,1,2,1,1,1,1);
insert into dates values (1, current date);
then this
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, g.cust
, g.rep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE
g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date
GROUP BY GROUPING SETS ( (), (cust), (rep) )
HAVING (cust = $customer AND rep is null)
OR (cust is null AND rep = $rep)
OR (cust is null AND rep is null)
gives this
SALES_180COMPANY VELOCITYCOMPANY CUST REP
---------------- --------------- ---- ----
5 36 NULL NULL
3 60 1 NULL
2 90 NULL 1
Or this... there are often a multitude of ways to do things in SQL
WITH CTE AS (
SELECT g.cust
, g.rep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE
g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
) , CUST AS (
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
FROM CTE
WHERE cust = $customer
) , REP AS (
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
FROM CTE
WHERE rep = $rep
) , ALL AS (
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
FROM CTE
)
SELECT * FROM CUST, REP, ALL
retuning
SALES_180COMPANY VELOCITYCOMPANY SALES_180COMPANY VELOCITYCOMPANY SALES_180COMPANY VELOCITYCOMPANY
---------------- --------------- ---------------- --------------- ---------------- ---------------
3 60 2 90 5 36

How to optimize this query , it is taking time 40 minuts in execution?

select * from non_bidders_report_view
where applicant_category_id =1314
and applicant_status_id not in(10,11)
and partner_id = 4
and applicant_status_id <> 6
and applicant_id not in (
Select apb.applicant_id
from applicant_property_bids apb
inner join applicants a on
a.applicant_id=apb.applicant_id
where to_date(apb.bid_Date) >= to_date('30/4/2012','dd/mm/yyyy')
and to_date(apb.bid_Date) <= to_date('30/4/2015','dd/mm/yyyy')
and a.partner_id = 4 group by apb.applicant_Id
union
select aba.applicant_Id from Archive_Bid_Applicants aba
inner join applicants a on a.applicant_id=aba.applicant_id
where to_date(aba.bid_Date) >= to_date('30/4/2012','dd/mm/yyyy')
and to_date(aba.bid_Date) <= to_date('30/4/2015','dd/mm/yyyy')
and a.partner_id = 4 group by aba.applicant_Id
);
You can try this query:
select * from non_bidders_report_view nb
where applicant_category_id = 1314 and partner_id = 4
and applicant_status_id not in (6, 10, 11)
and not exists (
select 1 from applicant_property_bids abp
join applicants a on a.applicant_id=abp.applicant_id and a.partner_id=4
and abp.bid_Date between date '2012-04-30' and date '2015-04-30'
where abp.applicant_id = nb.applicant_id )
and not exists (
select 1 from archive_bid_applicants aba
join applicants a on a.applicant_id=aba.applicant_id and a.partner_id=4
and aba.bid_Date between date '2012-04-30' and date '2015-04-30'
where aba.applicant_id = nb.applicant_id )
The idea is to get rid of group by and union which seems to be unnecesary here and change not in to not exists.
Alternative solution:
select * from non_bidders_report_view nb
where applicant_category_id = 1314 and partner_id = 4
and applicant_status_id not in (6, 10, 11)
and not exists (
select 1 from (
select applicant_id, bid_date from applicant_property_bids
union all
select applicant_id, bid_date from archive_bid_applicants
) ab
join applicants a on a.applicant_id=ab.applicant_id and a.partner_id=4
and ab.bid_Date between date '2012-04-30' and date '2015-04-30'
where ab.applicant_id = nb.applicant_id )
If you have millions of data then create index on primary key. It will increase your performance. Indexes helps to speed up data retrieval. Create index on all 3 tables.

SQL sub query with complex criteria

I have a table like this:
TransId. LayerNo. AccountId.
100. 1. 2.
100. 2. 3.
120. 1. 5.
120. 2. 6.
120. 3. 12.
70. 1. 2.
I want to find transId(s) where:
(LayerNo = 1 and (accountId = 2 or 5))
and
(LayerNo = 2 and (accountId = 3 or 6))
And result set would be row no 1,2,3,4.
How could I write query to get the result?
My database is SQL server 2008 r2
Thanks in advance
Nima
SELECT TransId
FROM your_table
WHERE ( layerno = 1
AND accountid IN ( 2, 5 ) )
INTERSECT
SELECT TransId
FROM your_table
WHERE ( layerno = 2
AND accountid IN ( 3, 6 ) )
One approach is to ensure that each transID must have two records that satisfy the conditions you outlined.
SELECT * FROM
TABLE
WHERE TransID IN(
SELECT TransId
FROM table
WHERE ( layerno = 1
AND accountid IN ( 2, 5 ) )
OR ( layerno = 2
AND accountid IN( 3, 6 ) )
GROUP BY
TransId
HAVING Count(*) = 2
)
However this could be a problem if you can have multple records where layerno = 1. So you can use self joins instead to ensure the criteria.
SELECT DISTINCT a.transid
FROM table a
INNER JOIN table b
ON a.transid = b.transid
INNER JOIN table c
ON a.transid = c.transid
WHERE b.layerno = 1
AND accountid IN ( 2, 5 )
AND c.layerno = 2
AND accountid IN ( 3, 6 )
That said Martin's INTERSECT approach is probably the best
Do you mean:
SELECT
TransId,
LayerNo,
AccountId
FROM Table
WHERE (LayerNo = 1 AND AccountId IN (2, 5)) OR
(LayerNo = 2 AND AccountId IN (3, 7))
create table #temp
( rowId Int Identity(1,1), transId int)
INSERT INTO #temp(transId)
select TransId
from TableName
where (layerNo = 1 and accountID IN (2, 5))
OR (layerNo = 2 and accountId IN (3, 6))
select * from #temp
SELECT
base.TransId,
base.LayerNo,
base.AccountId
FROM TableX AS base
JOIN TableX AS a
ON a.TransId = base.TransId
AND a.LayerNo = 1 AND a.AccountId IN (2, 5)
JOIN TableX AS b
ON b.TransId = base.TransId
AND b.LayerNo = 2 AND b.AccountId IN (3, 7)
WHERE (base.LayerNo = 1 AND base.AccountId IN (2, 5))
OR (base.LayerNo = 2 AND base.AccountId IN (3, 7))
This intersection is empty. If you take the values where LayerNo = 1 and LayerNo = 2 and intersect them their intersection is empty because these events are mutually exclusive. I believe this error comes from how the question was originally stated. I might be wrong but the predicate should have been
(LayerNo = 1 and (accountId = 2 or 5)) OR (LayerNo = 2 and (accountId = 3 or 6))
Replace the AND with an OR. If the predicate was stated correctly then the intersect is correct but will always be empty.
SELECT *
FROM table
WHERE (LayerNo = 1 AND (AccountID = 2 OR AccountID = 5))
OR (LayerNo = 2 AND (AccountID = 3 OR AccountID = 6))