How to join tables where a variable number of conditions match - sql

I have two tables, tblPerson and tblOffer. The person table has details about which products they currently have. The offers have multiple criteria for whether or not we should suggest that a person get the new product.
I'm trying to join these based on multiple criteria in tblOffer, but getting stuck on making sure that all of the selected criteria are judged, not just any one of them. For example Here are some fields in my tables.
tblPerson
pkPersonId
HasCreditCard
HasEmail
HasLoan
tblOffer
pkOfferId
NeedsCreditCard
NeedsEmail
NeedsLoan
Sample Data:
tblPerson
1, 0, 0, 1
2, 0, 1, 0
3, 0, 0, 0
tblOffer
100, 1, 0, 1
200, 0, 0, 1
300, 1, 1, 0
I'm trying to return a result for person 1 which contains Offer 300, Person 2 gets offer 100 and 200, and Person 3 gets offers 100, 200, and 300.
I have tried Cross APPLY between the two tables, and then using my WHERE clause to say:
SELECT * FROM tblPerson prs
CROSS JOIN tblMrmOffer ofr
WHERE prs.pkPersonId = #PersonId AND (
(prs.HasEmail = 0 AND ofr.NeedsEmail = 1) OR
(prs.HasCreditCard = 0 AND ofr.NeedsCreditCard = 1) OR
(prs.HasLoan = 0 AND ofr.NeedsLoan = 1))
This will give me a row if any of the selected criteria are true but not limited to rows where all selected criteria are set. For example, Offer 300 would match if the person needs a Credit Card or Email, but not necessarily if they need both. I'm trying to work this out as a Cross Tab Pivot, but not clear how to JOIN this. Any help would be appreciated.

If you are trying to make it so that only offers show up for which each of the items in the offer are needed by a person, but offers are disqualified if the person already has at least one of the things, then here is some SQL to try. What each of the AND conditions says is we want to disqualify cases where the person already has an item in the offer.
SELECT * FROM tblPerson prs
CROSS JOIN tblMrmOffer ofr
WHERE prs.pkPersonId = #PersonId
AND
NOT (prs.HasEmail = 1 AND ofr.NeedsEmail = 1)
AND
NOT (prs.HasCreditCard = 1 AND ofr.NeedsCreditCard = 1)
AND
NOT (prs.HasLoan = 1 AND ofr.NeedsLoan = 1)
If instead, you are looking for cases where the offer has ALL of the person's needs, then this SQL might be more up your alley. The theory with the case statements is if the criteria IS important (which is the case when the HasX = 0), then the filter needs to be 1 for the NeedsX. Otherwise, it just matches the NeedsX value (and so the filter wouldn't do anything).
SELECT * FROM tblPerson prs
CROSS JOIN tblMrmOffer ofr
WHERE prs.pkPersonId = #PersonId
AND
(
ofr.NeedsEmail
=
CASE
WHEN prs.HasEmail = 0
THEN 1
ELSE ofr.NeedsEmail
END
)
AND
(
ofr.NeedsCreditCard
=
CASE
WHEN prs.HasCreditCard = 0
THEN 1
ELSE ofr.NeedsCreditCard
END
)
AND
(
ofr.NeedsLoan
=
CASE
WHEN prs.HasLoan = 0
THEN 1
ELSE ofr.NeedsLoan
END
)
If you want to get really fancy, you can, in your select statement, put a ranking that ranks cases that perfectly match the person's needs above those that just match some of the user's unmet needs. For this, it would look something like this, which prioritizes perfect matches first, and good matches second (and within good matches, those with more items in the offer), and then within those categories, latest offers first:
SELECT
...
DENSE_RANK() OVER (
PARTITION BY prs.pkPersonID
ORDER BY
CASE
WHEN <logic from 2nd SQL above>
THEN 1
WHEN <logic from 1st SQL above>
THEN ofr.NeedsEmail + ofr.NeedsCreditCard + ofr.NeedsLoan
ELSE NULL
END,
ofr.pkOfferId DESC
) AS Order
...

Related

Out of range integer: infinity

So I'm trying to work through a problem thats a bit hard to explain and I can't expose any of the data I'm working with but what Im trying to get my head around is the error below when running the query below - I've renamed some of the tables / columns for sensitivity issues but the structure should be the same
"Error from Query Engine - Out of range for integer: Infinity"
WITH accounts AS (
SELECT t.user_id
FROM table_a t
WHERE t.type like '%Something%'
),
CTE AS (
SELECT
st.x_user_id,
ad.name as client_name,
sum(case when st.score_type = 'Agility' then st.score_value else 0 end) as score,
st.obs_date,
ROW_NUMBER() OVER (PARTITION BY st.x_user_id,ad.name ORDER BY st.obs_date) AS rn
FROM client_scores st
LEFT JOIN account_details ad on ad.client_id = st.x_user_id
INNER JOIN accounts on st.x_user_id = accounts.user_id
--WHERE st.x_user_id IN (101011115,101012219)
WHERE st.obs_date >= '2020-05-18'
group by 1,2,4
)
SELECT
c1.x_user_id,
c1.client_name,
c1.score,
c1.obs_date,
CAST(COALESCE (((c1.score - c2.score) * 1.0 / c2.score) * 100, 0) AS INT) AS score_diff
FROM CTE c1
LEFT JOIN CTE c2 on c1.x_user_id = c2.x_user_id and c1.client_name = c2.client_name and c1.rn = c2.rn +2
I know the query works for sure because when I get rid of the first CTE and hard code 2 id's into a where clause i commented out it returns the data I want. But I also need it to run based on the 1st CTE which has ~5k unique id's
Here is a sample output if i try with 2 id's:
Based on the above number of row returned per id I would expect it should return 5000 * 3 rows = 150000.
What could be causing the out of range for integer error?
This line is likely your problem:
CAST(COALESCE (((c1.score - c2.score) * 1.0 / c2.score) * 100, 0) AS INT) AS score_diff
When the value of c2.score is 0, 1.0/c2.score will be infinity and will not fit into an integer type that you’re trying to cast it into.
The reason it’s working for the two users in your example is that they don’t have a 0 value for c2.score.
You might be able to fix this by changing to:
CAST(COALESCE (((c1.score - c2.score) * 1.0 / NULLIF(c2.score, 0)) * 100, 0) AS INT) AS score_diff

troubles with next and previous query

I have a list and the returned table looks like this. I took the preview of only one car but there are many more.
What I need to do now is check that the current KM value is larger then the previous and smaller then the next. If this is not the case I need to make a field called Trustworthy and should fill it with either 1 or 0 (true/ false).
The result that I have so far is this:
validKMstand and validkmstand2 are how I calculate it. It did not work in one list so that is why I separated it.
In both of my tries my code does not work.
Here is the code that I have so far.
FullList as (
SELECT
*
FROM
eMK_Mileage as Mileage
)
, ValidChecked1 as (
SELECT
UL1.*,
CASE WHEN EXISTS(
SELECT TOP(1)UL2.*
FROM FullList AS UL2
WHERE
UL2.FK_CarID = UL1.FK_CarID AND
UL1.KM_Date > UL2.KM_Date AND
UL1.KM > UL2.KM
ORDER BY UL2.KM_Date DESC
)
THEN 1
ELSE 0
END AS validkmstand
FROM FullList as UL1
)
, ValidChecked2 as (
SELECT
List1.*,
(CASE WHEN List1.KM > ulprev.KM
THEN 1
ELSE 0
END
) AS validkmstand2
FROM ValidChecked1 as List1 outer apply
(SELECT TOP(1)UL3.*
FROM ValidChecked1 AS UL3
WHERE
UL3.FK_CarID = List1.FK_CarID AND
UL3.KM_Date <= List1.KM_Date AND
List1.KM > UL3.KM
ORDER BY UL3.KM_Date DESC) ulprev
)
SELECT * FROM ValidChecked2 order by FK_CarID, KM_Date
Maybe something like this is what you are looking for?
;with data as
(
select *, rn = row_number() over (partition by fk_carid order by km_date)
from eMK_Mileage
)
select
d.FK_CarID, d.KM, d.KM_Date,
valid =
case
when (d.KM > d_prev.KM /* or d_prev.KM is null */)
and (d.KM < d_next.KM /* or d_next.KM is null */)
then 1 else 0
end
from data d
left join data d_prev on d.FK_CarID = d_prev.FK_CarID and d_prev.rn = d.rn - 1
left join data d_next on d.FK_CarID = d_next.FK_CarID and d_next.rn = d.rn + 1
order by d.FK_CarID, d.KM_Date
With SQL Server versions 2012+ you could have used the lag() and lead() analytical functions to access the previous/next rows, but in versions before you can accomplish the same thing by numbering rows within partitions of the set. There are other ways too, like using correlated subqueries.
I left a couple of conditions commented out that deal with the first and last rows for every car - maybe those should be considered valid is they fulfill only one part of the comparison (since the previous/next rows are null)?

Is it possible to replace the following two SQL selects with just one?

Please, observe:
DECLARE #UseFastLane BIT
SELECT TOP 1 #UseFastLane = 1
FROM BackgroundJobService
WHERE IsFastLane = 1;
SELECT TOP 1 bjs.HostName AllocatedAgentHostName,
bjs.ServiceName AllocatedAgentServiceName,
bjs.IsFastLane,
SUM(CASE
WHEN bjw.WorkStatusTypeId IN ( 2, 3, 4, 10 ) THEN 1
ELSE 0
END) AS InProgress
FROM BackgroundJobService bjs
LEFT JOIN BackgroundJobWork bjw
ON bjw.AllocatedAgentHostName = bjs.HostName
AND bjw.AllocatedAgentServiceName = bjs.ServiceName
WHERE bjs.AgentStatusTypeId = 2
AND bjs.IsFastLane = COALESCE(#UseFastLane, 0)
GROUP BY bjs.HostName,
bjs.ServiceName,
bjs.IsFastLane
ORDER BY IsFastLane DESC,
InProgress
I am using two SQL select statements here. Is it possible to use just one top level SQL select statement, nesting another one within?
You can replace the text AND bjs.IsFastLane = COALESCE(#UseFastLane, 0) with this:
AND bjs.IsFastLane = (SELECT Max(IsFastLane)
FROM BackgroundJobService)
which should give you an equivalent query assuming that there are rows in the BackgroundJobService.
If there might be zero rows in BackgroundJobService then you can wrap the select with a COALESCE function to return 0, like this:
COALESCE((SELECT Max(IsFastLane) FROM BackgroundJobService), 0)

Need COUNT from a more complex query

I usually use COUNT within a subquery to grab the desired number, but in this case I need a little help as the query contains too many arguments.
SELECT a.[QueueID]
,a.[CouponID]
,a.[ListingID]
,a.[User_ID]
,b.[CouponID]
,b.[ListingID]
,b.[CouponActive]
,b.[CouponExpire]
,b.[IsDeleted]
,c.[ListingID]
,c.[TypeID]
,c.[LevelID]
,#passedUserID as User_ID
FROM CouponQueue a
JOIN Coupon b
on a.CouponID = b.CouponID
JOIN Listing c
on b.ListingID = c.ListingID
WHERE (a.[User_ID] = #passedUserID)
AND (b.[CouponActive] = 1)
AND (b.[IsDeleted] = 0)
AND (b.[CouponExpire] > DATEADD(dd, -1, GETDATE()) OR b.[CouponExpire] IS NULL)
So lets say this query returns a result of 7 rows. All I need is this number for my VIEW. So I want to limit the ultimate result to a single row so that in the end I get:
[TotalCount] <-- Field name
[7] <-- Result
But not 7 rows of data.. I just need the count from the above query. Still plugging away and trying to learn. I looked at a few other examples but I haven't found one with all the conditions... which is what's messing me up. Please help!
Thank you so much!
Would this work for you?
select count(*) as TotalCOunt from (
SELECT a.[QueueID] /*
,a.[CouponID]
,a.[ListingID]
,a.[User_ID]
,b.[CouponID]
,b.[ListingID]
,b.[CouponActive]
,b.[CouponExpire]
,b.[IsDeleted]
,c.[ListingID]
,c.[TypeID]
,c.[LevelID]
,#passedUserID as User_ID */
FROM CouponQueue a
JOIN Coupon b
on a.CouponID = b.CouponID
JOIN Listing c
on b.ListingID = c.ListingID
WHERE (a.[User_ID] = #passedUserID)
AND (b.[CouponActive] = 1)
AND (b.[IsDeleted] = 0)
AND (b.[CouponExpire] > DATEADD(dd, -1, GETDATE()) OR b.[CouponExpire] IS NULL)
) t
You can remove the columns for the count. They are not actually necessary.
Should be able to just add COUNT(*):
SELECT COUNT(*) as TotalCount
FROM CouponQueue a
JOIN Coupon b
on a.CouponID = b.CouponID
JOIN Listing c
on b.ListingID = c.ListingID
WHERE (a.[User_ID] = #passedUserID)
AND (b.[CouponActive] = 1)
AND (b.[IsDeleted] = 0)
AND (b.[CouponExpire] > DATEADD(dd, -1, GETDATE()) OR b.[CouponExpire] IS NULL)
Good luck.
You can put the below sample on your SQL Developer to run for the count:
SELECT count(*) as totalCount (*-open a parentheses - your original query -close the parentheses*)

SQL: Return "true" if list of records exists?

An alternative title might be:
Check for existence of multiple rows?
Using a combination of SQL and C# I want a method to return true if all products in a list exist in a table. If it can be done all in SQL that would be preferable. I have written a method that returns whether a single productID exists using the following SQL:
SELECT productID FROM Products WHERE ProductID = #productID
If this returns a row, then the c# method returns true, false otherwise.
Now I'm wondering if I have a list of product IDs (not a huge list mind you, normally under 20). How can I write a query that will return a row if all the product id's exist and no row if one or more product id's does not exist?
(Maybe something involving "IN" like:
SELECT * FROM Products WHERE ProductID IN ('1', '10', '100', 'ABC'))
EDIT:
How the result is expressed is not important to me. Whether the query returns a 1 or 0, an empty resultset or a non-empty one, true or false doesn't matter. I'd prefer the answer that is 1) easy to read and understand and 2) performant
I was envisioning concatenating the list of product id's with the SQL. Obviously this opens the code up to SQL injection (the product id's are actually varchar. in this case the chance is slim but still want to avoid that possibility). So if there is a way around this that would be better. Using SQL Server 2005.
Product ID's are varchar
Here's how I usually do it:
Just replace your query with this statement SELECT * FROM table WHERE 1
SELECT
CASE WHEN EXISTS
(
SELECT * FROM table WHERE 1
)
THEN 'TRUE'
ELSE 'FALSE'
END
Given your updated question, these are the simplest forms:
If ProductID is unique you want
SELECT COUNT(*) FROM Products WHERE ProductID IN (1, 10, 100)
and then check that result against 3, the number of products you're querying (this last part can be done in SQL, but it may be easier to do it in C# unless you're doing even more in SQL).
If ProductID is not unique it is
SELECT COUNT(DISTINCT ProductID) FROM Products WHERE ProductID IN (1, 10, 100)
When the question was thought to require returning rows when all ProductIds are present and none otherwise:
SELECT ProductId FROM Products WHERE ProductID IN (1, 10, 100) AND ((SELECT COUNT(*) FROM Products WHERE ProductID IN (1, 10, 100))=3)
or
SELECT ProductId FROM Products WHERE ProductID IN (1, 10, 100) AND ((SELECT COUNT(DISTINCT ProductID) FROM Products WHERE ProductID IN (1, 10, 100))=3)
if you actually intend to do something with the results. Otherwise the simple SELECT 1 WHERE (SELECT ...)=3 will do as other answers have stated or implied.
#Mark Hurd, thanks for pointing out the error.
this will work (if you are using Postgresql, Sql Server 2008):
create table products
(
product_id int not null
);
insert into products values(1),(2),(10),(100);
SELECT
CASE
WHEN EXISTS(
SELECT 1
FROM (values(1),(10),(100)) as x(id)
WHERE x.id NOT IN (select product_id from products))
THEN 0 --'NOT ALL'
ELSE 1 -- 'ALL'
END
If you are using MySQL, make a temporary memory table(then populate 1,10,100 there):
create table product_memory(product_id int) engine=MEMORY;
insert into product_memory values(1),(10),(100);
SELECT
CASE
WHEN EXISTS(
SELECT 1
FROM product_memory
WHERE product_memory.id NOT IN (select product_id from products))
THEN 0 -- 'NOT ALL'
ELSE 1 -- 'ALL'
END
On your C# code:
bool isAllExist = (int)(new SqlCommand(queryHere).ExecuteScalar()) == 1;
[EDIT]
How can I write a query that will
return a row if all the product id's
exist and no row if one or more
product id's does not exist?
Regarding, returning a row(singular) if all rows exists, and no row to be returned if one or more product id does not exists:
MySql:
SELECT 1
WHERE
NOT EXISTS(
SELECT 1
FROM product_memory
WHERE product_memory.id NOT IN (select product_id from products) )
Posgresql, Sql Server 2008:
SELECT 1
WHERE
NOT EXISTS(
SELECT 1
FROM (values(1),(10),(100)) as x(id)
WHERE x.id NOT IN (select product_id from products) )
Then on your C# code:
var da = new SqlDataAdapter(queryhere, connectionhere);
var dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
return true;
else
return false;
Or just make the condition shorter:
return dt.Rows.Count > 0;
Assuming you're using SQL Server, the boolean type doesn't exist, but the bit type does, which can hold only 0 or 1 where 0 represents False, and 1 represents True.
I would go this way:
select 1
from Products
where ProductId IN (1, 10, 100)
Here, a null or no row will be returned (if no row exists).
Or even:
select case when EXISTS (
select 1
from Products
where ProductId IN (1, 10, 100)
) then 1 else 0 end as [ProductExists]
Here, either of the scalar values 1 or 0 will always be returned (if no row exists).
DECLARE #values TABLE (ProductId int)
INSERT #values (1)
INSERT #values (10)
INSERT #values (100)
SELECT CASE WHEN (SELECT COUNT(*) FROM #values v) =
(SELECT COUNT(*) FROM Products p WHERE p.ProductId IN
(SELECT v.ProductId FROM #values v))
THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END [AreAllFound]
I know this is old but I think this will help anyone else who comes looking...
SELECT CAST(COUNT(ProductID) AS bit) AS [EXISTS] FROM Products WHERE(ProductID = #ProductID)
This will ALWAYS return TRUE if exists and FALSE if it doesn't (as opposed to no row).
You can use a SELECT CASE statement like so:
select case when EXISTS (
select 1
from <table>
where <condition>
) then TRUE else FALSE end
It returns TRUE when your query in the parents exists.
For PostgreSQL:
SELECT COUNT(*) = 1 FROM (
SELECT 1 FROM $table WHERE $condition LIMIT 1
) AS t
// not familiar with C#, but C#'s equivalent of PHP's:
$count = count($productIds); // where $productIds is the array you also use in IN (...)
SELECT IF ((SELECT COUNT(*) FROM Products WHERE ProductID IN (1, 10, 100)) = $count, 1, 0)
If the IN clause is a parameter (either to SP or hot-built SQL), then this can always be done:
SELECT (SELECT COUNT(1)
FROM product_a
WHERE product_id IN (1, 8, 100)
) = (number of commas in product_id as constant)
If the IN clause is a table, then this can always be done:
SELECT (SELECT COUNT(*)
FROM product_a
WHERE product_id IN (SELECT Products
FROM #WorkTable)
) = (SELECT COUNT(*)
FROM #WorkTable)
If the IN clause is complex then either spool it into a table or write it twice.
If you have the IDs stored in a temp table (which can be done by some C# function or simple SQL) then the problem becomes easy and doable in SQL.
select "all exist"
where (select case when count(distinct t.id) = (select count(distinct id) from #products) then "true" else "false" end
from ProductTable t, #products p
where t.id = p.id) = "true"
This will return "all exists" when all the products in #products exist in the target table (ProductTable) and will not return a row if the above is not true.
If you are not willing to write to a temp table, then you need to feed in some parameter for the number of products you are attempting to find, and replace the temp table with an 'in'; clause so the subquery looks like this:
SELECT "All Exist"
WHERE(
SELECT case when count(distinct t.id) = #ProductCount then "true" else "false"
FROM ProductTable t
WHERE t.id in (1,100,10,20) -- example IDs
) = "true"
If you are using SQL Server 2008, I would create a stored procedure which takes a table-valued parameter. The query should then be of a particularly simple form:
CREATE PROCEDURE usp_CheckAll
(#param dbo.ProductTableType READONLY)
AS
BEGIN
SELECT CAST(1 AS bit) AS Result
WHERE (SELECT COUNT(DISTINCT ProductID) FROM #param)
= (SELECT COUNT(DISTINCT p.ProductID) FROM #param AS p
INNER JOIN Products
ON p.ProductID = Products.ProductID)
END
I changed this to return a row, as you seem to require. There are other ways to do this with a WHERE NOT EXISTS (LEFT JOIN in here WHERE rhs IS NULL):
CREATE PROCEDURE usp_CheckAll
(#param dbo.ProductTableType READONLY)
AS
BEGIN
SELECT CAST(1 AS bit) AS Result
WHERE NOT EXISTS (
SELECT * FROM #param AS p
LEFT JOIN Products
ON p.ProductID = Products.ProductID
WHERE Products.ProductID IS NULL
)
END
Your c# will have to do just a bit of work (counting the number of IDs passed in), but try this:
select (select count(*) from players where productid in (1, 10, 100, 1000)) = 4
Edit:
4 can definitely be parameterized, as can the list of integers.
If you're not generating the SQL from string input by the user, you don't need to worry about attacks. If you are, you just have to make sure you only get integers. For example, if you were taking in the string "1, 2, 3, 4", you'd do something like
String.Join(",", input.Split(",").Select(s => Int32.Parse(s).ToString()))
That will throw if you get the wrong thing. Then just set that as a parameter.
Also, be sure be sure to special case if items.Count == 0, since your DB will choke if you send it where ParameterID in ().
Where is this list of products that you're trying to determine the existence of? If that list exists within another table you could do this
declare #are_equal bit
declare #products int
SELECT #products =
count(pl.id)
FROM ProductList pl
JOIN Products p
ON pl.productId = p.productId
select #are_equal = #products == select count(id) from ProductList
Edit:
Then do ALL the work in C#. Cache the actual list of products in your application somewhere, and do a LINQ query.
var compareProducts = new List<Product>(){p1,p2,p3,p4,p5};
var found = From p in GetAllProducts()
Join cp in compareProducts on cp.Id equals p.Id
select p;
return compareProducts.Count == found.Count;
This prevents constructing SQL queries by hand, and keeps all your application logic in the application.
This may be too simple, but I always use:
SELECT COUNT(*)>0 FROM `table` WHERE condition;
Example:
SELECT iif(count(id)=0,'false','true') FROM table WHERE id = 19