SQL join where all joined meet condition












0















I need a little help for an SQL query.



I have tables: clients | claims | statuses | contacts



I have a query to find all clients and their contact details where any of their claims is a specified status:



SELECT
clients.id AS "Client Ref"
,claims.clientclaimid AS "Claim Number"
,Contacts.PhoneHome AS "Mobile"
,statuses.description AS Status
FROM
dbo.claims
LEFT JOIN
statuses
ON
dbo.claims.statusID = statuses.ID
LEFT JOIN
clients
ON
dbo.claims.clientid = clients.id
LEFT JOIN
contacts
ON
clients.contactid = Contacts.id
WHERE
statuses.description = 'client - pack sent to customer'
AND (DATEADD(MM, -@joinedpremonthsago, GETDATE()) > clients.DateJoined)
AND clients.DateJoined > 01 / 01 / 2012
AND claims.active = 1
ORDER BY
[Client Ref]
,[Claim Number];


I now need this to only pull clients where ALL of their claims are in specified status, but I don't know how to do this. How can I get clients where all of the claims have this status description? Could I have guidance or solutions for this?



Here is relevant schema;
Claims Table



enter image description here



Contacts Table



enter image description here



Clients Table



enter image description here



Here is an image of the query returning where ANY of the client's claims are in status;
Current results



enter image description here










share|improve this question

























  • hi thanks for using Stack Overflow. Good description above. It would be useful to have some sample data, in a format that we can use to build a schema. Having desired output based on sample data would be useful as well

    – EoinS
    Nov 27 '18 at 16:36
















0















I need a little help for an SQL query.



I have tables: clients | claims | statuses | contacts



I have a query to find all clients and their contact details where any of their claims is a specified status:



SELECT
clients.id AS "Client Ref"
,claims.clientclaimid AS "Claim Number"
,Contacts.PhoneHome AS "Mobile"
,statuses.description AS Status
FROM
dbo.claims
LEFT JOIN
statuses
ON
dbo.claims.statusID = statuses.ID
LEFT JOIN
clients
ON
dbo.claims.clientid = clients.id
LEFT JOIN
contacts
ON
clients.contactid = Contacts.id
WHERE
statuses.description = 'client - pack sent to customer'
AND (DATEADD(MM, -@joinedpremonthsago, GETDATE()) > clients.DateJoined)
AND clients.DateJoined > 01 / 01 / 2012
AND claims.active = 1
ORDER BY
[Client Ref]
,[Claim Number];


I now need this to only pull clients where ALL of their claims are in specified status, but I don't know how to do this. How can I get clients where all of the claims have this status description? Could I have guidance or solutions for this?



Here is relevant schema;
Claims Table



enter image description here



Contacts Table



enter image description here



Clients Table



enter image description here



Here is an image of the query returning where ANY of the client's claims are in status;
Current results



enter image description here










share|improve this question

























  • hi thanks for using Stack Overflow. Good description above. It would be useful to have some sample data, in a format that we can use to build a schema. Having desired output based on sample data would be useful as well

    – EoinS
    Nov 27 '18 at 16:36














0












0








0








I need a little help for an SQL query.



I have tables: clients | claims | statuses | contacts



I have a query to find all clients and their contact details where any of their claims is a specified status:



SELECT
clients.id AS "Client Ref"
,claims.clientclaimid AS "Claim Number"
,Contacts.PhoneHome AS "Mobile"
,statuses.description AS Status
FROM
dbo.claims
LEFT JOIN
statuses
ON
dbo.claims.statusID = statuses.ID
LEFT JOIN
clients
ON
dbo.claims.clientid = clients.id
LEFT JOIN
contacts
ON
clients.contactid = Contacts.id
WHERE
statuses.description = 'client - pack sent to customer'
AND (DATEADD(MM, -@joinedpremonthsago, GETDATE()) > clients.DateJoined)
AND clients.DateJoined > 01 / 01 / 2012
AND claims.active = 1
ORDER BY
[Client Ref]
,[Claim Number];


I now need this to only pull clients where ALL of their claims are in specified status, but I don't know how to do this. How can I get clients where all of the claims have this status description? Could I have guidance or solutions for this?



Here is relevant schema;
Claims Table



enter image description here



Contacts Table



enter image description here



Clients Table



enter image description here



Here is an image of the query returning where ANY of the client's claims are in status;
Current results



enter image description here










share|improve this question
















I need a little help for an SQL query.



I have tables: clients | claims | statuses | contacts



I have a query to find all clients and their contact details where any of their claims is a specified status:



SELECT
clients.id AS "Client Ref"
,claims.clientclaimid AS "Claim Number"
,Contacts.PhoneHome AS "Mobile"
,statuses.description AS Status
FROM
dbo.claims
LEFT JOIN
statuses
ON
dbo.claims.statusID = statuses.ID
LEFT JOIN
clients
ON
dbo.claims.clientid = clients.id
LEFT JOIN
contacts
ON
clients.contactid = Contacts.id
WHERE
statuses.description = 'client - pack sent to customer'
AND (DATEADD(MM, -@joinedpremonthsago, GETDATE()) > clients.DateJoined)
AND clients.DateJoined > 01 / 01 / 2012
AND claims.active = 1
ORDER BY
[Client Ref]
,[Claim Number];


I now need this to only pull clients where ALL of their claims are in specified status, but I don't know how to do this. How can I get clients where all of the claims have this status description? Could I have guidance or solutions for this?



Here is relevant schema;
Claims Table



enter image description here



Contacts Table



enter image description here



Clients Table



enter image description here



Here is an image of the query returning where ANY of the client's claims are in status;
Current results



enter image description here







sql sql-server join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 19:09









GGadde

392113




392113










asked Nov 27 '18 at 16:30









terrygterryg

33




33













  • hi thanks for using Stack Overflow. Good description above. It would be useful to have some sample data, in a format that we can use to build a schema. Having desired output based on sample data would be useful as well

    – EoinS
    Nov 27 '18 at 16:36



















  • hi thanks for using Stack Overflow. Good description above. It would be useful to have some sample data, in a format that we can use to build a schema. Having desired output based on sample data would be useful as well

    – EoinS
    Nov 27 '18 at 16:36

















hi thanks for using Stack Overflow. Good description above. It would be useful to have some sample data, in a format that we can use to build a schema. Having desired output based on sample data would be useful as well

– EoinS
Nov 27 '18 at 16:36





hi thanks for using Stack Overflow. Good description above. It would be useful to have some sample data, in a format that we can use to build a schema. Having desired output based on sample data would be useful as well

– EoinS
Nov 27 '18 at 16:36












2 Answers
2






active

oldest

votes


















1














The solution is to use the principle of exclusion. You write a query to get all clients that do have the status at least once. Great news: that part is already done :) Next you write a query to find clients that have any other status. Once you have both queries, you put them together to exclude the 2nd set from the first. You can do this in several ways: a NOT EXISTS() expression, a NOT IN() expression, an exclusion join, or the EXCEPT keyword can all work.



Personally I'm most comfortable with exclusion joins, but NOT EXISTS() is more common and tends to perform a little better:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id

where s.description = 'client - pack sent to customer'
and (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

and NOT EXISTS (
select 1
from clients cli0
inner join claims cla0 on cla0.clientid = cli0.id
inner join statuses s0 on s0.ID = cla0.statusID
WHERE cli0.ID = cli.ID
AND s0.description <> 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Exclusion join version:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID AND s.description = 'client - pack sent to customer'
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id
-- the "JOIN" part of an exclusion join
left join statuses s2 on cla.statusID = s2.ID AND s2.description <> 'client - pack sent to customer'

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

-- the "EXCLUSION" part of an exclusion join
and s2.ID IS NULL

order by [Client Ref], [Claim Number]


Note how I chose inner rather then left for some of the original joins. The way fields from those tables were used in the WHERE clause already made them effectively inner joins. Being honest about the join type helps you spot bugs and may allow Sql Server to build a better execution plan.



Also note I removed the status from the SELECT clause results, as that is now implied by the requirements.



Finally, note how I added table aliases to the query. It's good practice to always use table aliases with your queries. It's absolutely necessary to avoid ambiguity if you want to reference the same table more than once in a single query, as we do in both examples here. By convention, these aliases are often short — even single letter — mnemonics for the table names. So cli in this query is short for client, and I used 3 whole characters so it can be distinguished from claims. cli0 is used in the inner query to mean "client prime"... think of it as if the 0 were a subscript.






share|improve this answer


























  • Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

    – terryg
    Nov 27 '18 at 16:54











  • That works well. thank. I am going to pit this solution against @James solution for speed

    – terryg
    Nov 27 '18 at 17:10











  • I'm not sure James solution will actually do you what you asked for.

    – Joel Coehoorn
    Nov 27 '18 at 17:11











  • @James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

    – terryg
    Nov 27 '18 at 17:52





















0














Something like (totaly untested code!):



select  clients.id as "Client Ref", claims.clientclaimid as "Claim Number",
Contacts.PhoneHome as "Mobile",statuses.description as Status
from dbo.claims
left join clients on dbo.claims.clientid = clients.id
left join contacts on clients.contactid = contacts.id

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > clients.DateJoined)
and clients.DateJoined > 01/01/2012
and claims.active=1
and dbo.claims.clientID in (
select dbo.claims.clientID
from dbo.claims
left join statuses on dbo.claims.statusID = statuses.ID
where statuses.description = 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Should do the trick.






share|improve this answer
























  • Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

    – terryg
    Nov 27 '18 at 17:20













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53504103%2fsql-join-where-all-joined-meet-condition%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The solution is to use the principle of exclusion. You write a query to get all clients that do have the status at least once. Great news: that part is already done :) Next you write a query to find clients that have any other status. Once you have both queries, you put them together to exclude the 2nd set from the first. You can do this in several ways: a NOT EXISTS() expression, a NOT IN() expression, an exclusion join, or the EXCEPT keyword can all work.



Personally I'm most comfortable with exclusion joins, but NOT EXISTS() is more common and tends to perform a little better:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id

where s.description = 'client - pack sent to customer'
and (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

and NOT EXISTS (
select 1
from clients cli0
inner join claims cla0 on cla0.clientid = cli0.id
inner join statuses s0 on s0.ID = cla0.statusID
WHERE cli0.ID = cli.ID
AND s0.description <> 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Exclusion join version:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID AND s.description = 'client - pack sent to customer'
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id
-- the "JOIN" part of an exclusion join
left join statuses s2 on cla.statusID = s2.ID AND s2.description <> 'client - pack sent to customer'

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

-- the "EXCLUSION" part of an exclusion join
and s2.ID IS NULL

order by [Client Ref], [Claim Number]


Note how I chose inner rather then left for some of the original joins. The way fields from those tables were used in the WHERE clause already made them effectively inner joins. Being honest about the join type helps you spot bugs and may allow Sql Server to build a better execution plan.



Also note I removed the status from the SELECT clause results, as that is now implied by the requirements.



Finally, note how I added table aliases to the query. It's good practice to always use table aliases with your queries. It's absolutely necessary to avoid ambiguity if you want to reference the same table more than once in a single query, as we do in both examples here. By convention, these aliases are often short — even single letter — mnemonics for the table names. So cli in this query is short for client, and I used 3 whole characters so it can be distinguished from claims. cli0 is used in the inner query to mean "client prime"... think of it as if the 0 were a subscript.






share|improve this answer


























  • Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

    – terryg
    Nov 27 '18 at 16:54











  • That works well. thank. I am going to pit this solution against @James solution for speed

    – terryg
    Nov 27 '18 at 17:10











  • I'm not sure James solution will actually do you what you asked for.

    – Joel Coehoorn
    Nov 27 '18 at 17:11











  • @James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

    – terryg
    Nov 27 '18 at 17:52


















1














The solution is to use the principle of exclusion. You write a query to get all clients that do have the status at least once. Great news: that part is already done :) Next you write a query to find clients that have any other status. Once you have both queries, you put them together to exclude the 2nd set from the first. You can do this in several ways: a NOT EXISTS() expression, a NOT IN() expression, an exclusion join, or the EXCEPT keyword can all work.



Personally I'm most comfortable with exclusion joins, but NOT EXISTS() is more common and tends to perform a little better:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id

where s.description = 'client - pack sent to customer'
and (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

and NOT EXISTS (
select 1
from clients cli0
inner join claims cla0 on cla0.clientid = cli0.id
inner join statuses s0 on s0.ID = cla0.statusID
WHERE cli0.ID = cli.ID
AND s0.description <> 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Exclusion join version:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID AND s.description = 'client - pack sent to customer'
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id
-- the "JOIN" part of an exclusion join
left join statuses s2 on cla.statusID = s2.ID AND s2.description <> 'client - pack sent to customer'

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

-- the "EXCLUSION" part of an exclusion join
and s2.ID IS NULL

order by [Client Ref], [Claim Number]


Note how I chose inner rather then left for some of the original joins. The way fields from those tables were used in the WHERE clause already made them effectively inner joins. Being honest about the join type helps you spot bugs and may allow Sql Server to build a better execution plan.



Also note I removed the status from the SELECT clause results, as that is now implied by the requirements.



Finally, note how I added table aliases to the query. It's good practice to always use table aliases with your queries. It's absolutely necessary to avoid ambiguity if you want to reference the same table more than once in a single query, as we do in both examples here. By convention, these aliases are often short — even single letter — mnemonics for the table names. So cli in this query is short for client, and I used 3 whole characters so it can be distinguished from claims. cli0 is used in the inner query to mean "client prime"... think of it as if the 0 were a subscript.






share|improve this answer


























  • Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

    – terryg
    Nov 27 '18 at 16:54











  • That works well. thank. I am going to pit this solution against @James solution for speed

    – terryg
    Nov 27 '18 at 17:10











  • I'm not sure James solution will actually do you what you asked for.

    – Joel Coehoorn
    Nov 27 '18 at 17:11











  • @James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

    – terryg
    Nov 27 '18 at 17:52
















1












1








1







The solution is to use the principle of exclusion. You write a query to get all clients that do have the status at least once. Great news: that part is already done :) Next you write a query to find clients that have any other status. Once you have both queries, you put them together to exclude the 2nd set from the first. You can do this in several ways: a NOT EXISTS() expression, a NOT IN() expression, an exclusion join, or the EXCEPT keyword can all work.



Personally I'm most comfortable with exclusion joins, but NOT EXISTS() is more common and tends to perform a little better:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id

where s.description = 'client - pack sent to customer'
and (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

and NOT EXISTS (
select 1
from clients cli0
inner join claims cla0 on cla0.clientid = cli0.id
inner join statuses s0 on s0.ID = cla0.statusID
WHERE cli0.ID = cli.ID
AND s0.description <> 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Exclusion join version:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID AND s.description = 'client - pack sent to customer'
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id
-- the "JOIN" part of an exclusion join
left join statuses s2 on cla.statusID = s2.ID AND s2.description <> 'client - pack sent to customer'

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

-- the "EXCLUSION" part of an exclusion join
and s2.ID IS NULL

order by [Client Ref], [Claim Number]


Note how I chose inner rather then left for some of the original joins. The way fields from those tables were used in the WHERE clause already made them effectively inner joins. Being honest about the join type helps you spot bugs and may allow Sql Server to build a better execution plan.



Also note I removed the status from the SELECT clause results, as that is now implied by the requirements.



Finally, note how I added table aliases to the query. It's good practice to always use table aliases with your queries. It's absolutely necessary to avoid ambiguity if you want to reference the same table more than once in a single query, as we do in both examples here. By convention, these aliases are often short — even single letter — mnemonics for the table names. So cli in this query is short for client, and I used 3 whole characters so it can be distinguished from claims. cli0 is used in the inner query to mean "client prime"... think of it as if the 0 were a subscript.






share|improve this answer















The solution is to use the principle of exclusion. You write a query to get all clients that do have the status at least once. Great news: that part is already done :) Next you write a query to find clients that have any other status. Once you have both queries, you put them together to exclude the 2nd set from the first. You can do this in several ways: a NOT EXISTS() expression, a NOT IN() expression, an exclusion join, or the EXCEPT keyword can all work.



Personally I'm most comfortable with exclusion joins, but NOT EXISTS() is more common and tends to perform a little better:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id

where s.description = 'client - pack sent to customer'
and (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

and NOT EXISTS (
select 1
from clients cli0
inner join claims cla0 on cla0.clientid = cli0.id
inner join statuses s0 on s0.ID = cla0.statusID
WHERE cli0.ID = cli.ID
AND s0.description <> 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Exclusion join version:



select cli.id as "Client Ref", cla.clientclaimid as "Claim Number", co.PhoneHome as "Mobile"

from dbo.claims cla

inner join statuses s on cla.statusID = s.ID AND s.description = 'client - pack sent to customer'
inner join clients cli on cla.clientid = cli.id
left join contacts co on cli.contactid = co.id
-- the "JOIN" part of an exclusion join
left join statuses s2 on cla.statusID = s2.ID AND s2.description <> 'client - pack sent to customer'

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > cli.DateJoined)
and cli.DateJoined > 01/01/2012
and cla.active=1

-- the "EXCLUSION" part of an exclusion join
and s2.ID IS NULL

order by [Client Ref], [Claim Number]


Note how I chose inner rather then left for some of the original joins. The way fields from those tables were used in the WHERE clause already made them effectively inner joins. Being honest about the join type helps you spot bugs and may allow Sql Server to build a better execution plan.



Also note I removed the status from the SELECT clause results, as that is now implied by the requirements.



Finally, note how I added table aliases to the query. It's good practice to always use table aliases with your queries. It's absolutely necessary to avoid ambiguity if you want to reference the same table more than once in a single query, as we do in both examples here. By convention, these aliases are often short — even single letter — mnemonics for the table names. So cli in this query is short for client, and I used 3 whole characters so it can be distinguished from claims. cli0 is used in the inner query to mean "client prime"... think of it as if the 0 were a subscript.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 17:29

























answered Nov 27 '18 at 16:49









Joel CoehoornJoel Coehoorn

310k96495730




310k96495730













  • Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

    – terryg
    Nov 27 '18 at 16:54











  • That works well. thank. I am going to pit this solution against @James solution for speed

    – terryg
    Nov 27 '18 at 17:10











  • I'm not sure James solution will actually do you what you asked for.

    – Joel Coehoorn
    Nov 27 '18 at 17:11











  • @James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

    – terryg
    Nov 27 '18 at 17:52





















  • Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

    – terryg
    Nov 27 '18 at 16:54











  • That works well. thank. I am going to pit this solution against @James solution for speed

    – terryg
    Nov 27 '18 at 17:10











  • I'm not sure James solution will actually do you what you asked for.

    – Joel Coehoorn
    Nov 27 '18 at 17:11











  • @James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

    – terryg
    Nov 27 '18 at 17:52



















Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

– terryg
Nov 27 '18 at 16:54





Thanks for solution and lesson :) Am looking over and will give a shot. Let you know soon

– terryg
Nov 27 '18 at 16:54













That works well. thank. I am going to pit this solution against @James solution for speed

– terryg
Nov 27 '18 at 17:10





That works well. thank. I am going to pit this solution against @James solution for speed

– terryg
Nov 27 '18 at 17:10













I'm not sure James solution will actually do you what you asked for.

– Joel Coehoorn
Nov 27 '18 at 17:11





I'm not sure James solution will actually do you what you asked for.

– Joel Coehoorn
Nov 27 '18 at 17:11













@James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

– terryg
Nov 27 '18 at 17:52







@James did not 'do the trick'. Exclusion took 4 secs vs NOT IN 17 secs. Thanks for details

– terryg
Nov 27 '18 at 17:52















0














Something like (totaly untested code!):



select  clients.id as "Client Ref", claims.clientclaimid as "Claim Number",
Contacts.PhoneHome as "Mobile",statuses.description as Status
from dbo.claims
left join clients on dbo.claims.clientid = clients.id
left join contacts on clients.contactid = contacts.id

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > clients.DateJoined)
and clients.DateJoined > 01/01/2012
and claims.active=1
and dbo.claims.clientID in (
select dbo.claims.clientID
from dbo.claims
left join statuses on dbo.claims.statusID = statuses.ID
where statuses.description = 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Should do the trick.






share|improve this answer
























  • Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

    – terryg
    Nov 27 '18 at 17:20


















0














Something like (totaly untested code!):



select  clients.id as "Client Ref", claims.clientclaimid as "Claim Number",
Contacts.PhoneHome as "Mobile",statuses.description as Status
from dbo.claims
left join clients on dbo.claims.clientid = clients.id
left join contacts on clients.contactid = contacts.id

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > clients.DateJoined)
and clients.DateJoined > 01/01/2012
and claims.active=1
and dbo.claims.clientID in (
select dbo.claims.clientID
from dbo.claims
left join statuses on dbo.claims.statusID = statuses.ID
where statuses.description = 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Should do the trick.






share|improve this answer
























  • Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

    – terryg
    Nov 27 '18 at 17:20
















0












0








0







Something like (totaly untested code!):



select  clients.id as "Client Ref", claims.clientclaimid as "Claim Number",
Contacts.PhoneHome as "Mobile",statuses.description as Status
from dbo.claims
left join clients on dbo.claims.clientid = clients.id
left join contacts on clients.contactid = contacts.id

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > clients.DateJoined)
and clients.DateJoined > 01/01/2012
and claims.active=1
and dbo.claims.clientID in (
select dbo.claims.clientID
from dbo.claims
left join statuses on dbo.claims.statusID = statuses.ID
where statuses.description = 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Should do the trick.






share|improve this answer













Something like (totaly untested code!):



select  clients.id as "Client Ref", claims.clientclaimid as "Claim Number",
Contacts.PhoneHome as "Mobile",statuses.description as Status
from dbo.claims
left join clients on dbo.claims.clientid = clients.id
left join contacts on clients.contactid = contacts.id

where (DateAdd(MM, -@joinedpremonthsago, GetDate()) > clients.DateJoined)
and clients.DateJoined > 01/01/2012
and claims.active=1
and dbo.claims.clientID in (
select dbo.claims.clientID
from dbo.claims
left join statuses on dbo.claims.statusID = statuses.ID
where statuses.description = 'client - pack sent to customer'
)

order by [Client Ref], [Claim Number]


Should do the trick.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 16:48









James AndersonJames Anderson

24.4k64169




24.4k64169













  • Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

    – terryg
    Nov 27 '18 at 17:20





















  • Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

    – terryg
    Nov 27 '18 at 17:20



















Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

– terryg
Nov 27 '18 at 17:20







Thanks - this solution took 4 seconds vs @Joel solution in at 17 seconds, but was returning clients with claims in other statuses

– terryg
Nov 27 '18 at 17:20




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53504103%2fsql-join-where-all-joined-meet-condition%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks