Sql request selection - sql

I' trying to get all the Missions getting the same contract.
Ex:
If this is the data of Mission
Mission Id Contract Id
-------------------------
1 1
2 2
3 2
4 3
How can I get 2-2 and 3-2 ?
I've tried many weird stuff but nothing works :(
Thanks for your help

The query below uses a Join which should be faster than a subquery.
Select t1.ContractId, t2.MissionId
From tab t1
Join tab t2 ON t1.ContractId = t2.ContractId
Group By t1.ContractId, t2.MissionId
Having Count(*) > 1

Related

SQL subquery to join the most recent plan for a client

In MS Access 365, I have two tables (TClients & TPlans) that I am trying to combine into a single Query (QClientsExtended). Each client could have many or no associated entries on the TPlan list. In my final query, I would like it to list every client regardless of wether or not they have a plan, and give me the date and the details of the most recent plan, if there is one.
I've read all the relevant W3C reference pages, and looked at so many possible solutions, and i've struggled to turn them into something that works for this. It feels like it should be SO simple, I appreciate I'm probably missing a fundamental aspect of SQL coding.
TClients
ClientID ClientFullName ExternalAppts
1 Testy McTestFace 1
2 Clemence Closed 0
3 Nancy New Ref 3
4 Juan One Appt 0
TPlans
PlanID ClientID PlanDetails PlanDate
1 3 Plan 1 05-Dec-22
2 3 Plan 2 10-Dec-22
3 1 plan 10-Dec-22
4 4 nil 05-Dec-22
Qclients Extended
ClientID ClientFullName PlanDetails PlanDate ExternalAppts
1 Testy McTestFace Plan 2 10-Dec-22 1
2 Clemence Closed 0
3 Nancy New Ref plan 10-Dec-22 3
4 Juan One Appt nil 05-Dec-22 0
I've found a solution, but it feels incredibly clunky. I have made two Queries, one to find the most recent date for each client, and the second to Left Join this with the Client Table. I'm sure this should be doable in a single query, but maybe it can't be done in Access. Here are my two Queries:
QlastPlan
SELECT t1.*
FROM TPlans AS t1
INNER JOIN (
SELECT [ClientID],
MAX(PlanDate) AS LastPlan
FROM TPlans
GROUP BY [ClientID])
AS t2 ON (t1.[PlanDate] = t2.LastPlan) AND (t1.[ClientID] = t2.[ClientID]);
QClients
SELECT
TC.*,
QLP.PlanDetails,
QLP.PlanDate,
FROM TClients TC
LEFT JOIN QlastPlan QLP on TC.ClientID = QLP.ClientID;
So perhaps there's another option someone can suggest, but this is what I will run with for now.
Try with a Left Join:
SELECT
TClients.ClientID,
TClients.ClientFullName,
TPlans.PlanDetails,
TPlans.PlanDate,
TClients.ExternalAppts
FROM
TClients
LEFT JOIN
TPlans ON TClients.ClientID = TPlans.ClientID
GROUP BY
TClients.ClientID,
TClients.ClientFullName,
TPlans.PlanDetails,
TPlans.PlanDate,
TClients.ExternalAppts
HAVING
TPlans.PlanDate=
(Select Max(PlanDate) From [TPlans] As T Where T.ClientID = TClients.[ClientID])
OR
TPlans.PlanDate Is Null

Return count id's value from multiple rows in one column Postgres

I'm having two tables (relation between themTest_case.id = Test_tag.test_id) like this:
Test_case table
id
name
1
Test name 1
2
Test name 2
3
Test name 3
4
Test name 4
Test_tag table
test_id
tag
1
feature:example1
1
package:Reports
1
QA
2
feature:example1
2
package:Reports
2
QA
3
feature:example1
3
package:Reports
3
QA
4
feature:newexample1
4
package:Charts
4
QA
The database tables and structure were already defined as I'm using a oublic library to push the results.
So, I need to return in the result the count of the id's and the value feature:example1
knowing that is a test that contains the tag package:Reports
So, it should return something like
Results
count(id)
tag
3
feature:example1
I already tried some different approaches without success.
How can I do that?
I think I'm as confused as everyone else, but this is a shot in the dark based on the various comments. There are much easier ways to arrive at this dataset, but I'm trying to read between the lines on your comments:
select
count (t.test_id), t.tag
from
test_case c
join test_tag t on c.id = t.test_id
where
t.tag like 'feature%' and
exists (
select null
from test_tag t2
where t2.test_id = t.test_id and t2.tag = 'package:Reports'
)
group by
t.tag

How can I show combined "article" which has the same amount of "bids"?

I want to show the combinations of "offers" that have the same number of "bids". The Both "OID" of the combination and the number of "bids" should be output.
I know the logic I want to show which article has the same bids in combination of other article, but I don't know how I can write it down. Some code can help me to understand this.
More information about the structure of the tables "offer" and "bid". Also a "Expected result" which is shown the result
I really want to learn SQL.
Try running this..
SELECT temp1.OID as OID1,temp2.OID as OID2,temp1.count1 as numberOfBids
FROM
(SELECT t1.OID,COUNT(t1.BID) as count1
FROM bid t1 group by t1.OID) temp1
JOIN
(SELECT t2.OID,COUNT(t2.BID) as count2
FROM bid t2 group by t2.OID) temp2
ON temp1.count1= temp2.count2
AND temp1.OID <> temp2.OID
But it will give output as below table
OID1 OID2 numberOfBids
1 2 2
1 5 2
2 5 2
5 1 2
5 2 2

Access "Not In" query not working while only In is working correctly

I have below given query which is working fine but I want to use "Not In" operator instead of "In" but its giving no results:
SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails
WHERE (((OrderProdDetails.Priority) In (SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails WHERE (((OrderProdDetails.OrdID)=[Forms]![UpdateOrder]![OdrID])))));
Desired Query:
SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails
WHERE (((OrderProdDetails.Priority) Not In (SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails WHERE (((OrderProdDetails.OrdID)=[Forms]![UpdateOrder]![OdrID])))));
Basically it is referencing a control on parent form and based on that in a subform I want to populate the priority numbers i.e 1,2,3 and if for that record 1 is entered I want to get only 2 and 3 as drop-down option.
ReocordID OrdID Brand Name Priority
2 1 Org 1 2
3 2 Org 2 1
4 1 Org 1 1
6 1 Org 1 3
7 3 Org 3 1
8 4 Org 1 1
9 5 Org 2 1
10 5 Org 2 2
11 6 Org 1 1
12 6 Org 2 2
If there is any other better approach for the same please suggest.
Thanks in advance for your help.
In all likelihood, your problem is that Priority can take on NULL values. In that case, NOT IN doesn't work as expected (although it does work technically). The usual advice is to always use NOT EXISTS with subqueries rather than NOT IN.
But, in your case, I would suggest conditional aggregation instead:
SELECT opd.Priority
FROM OrderProdDetails as opd
GROUP BY opd.Priority
HAVING SUM(IIF(opd.OrdID = [Forms]![UpdateOrder]![OdrID], 1, 0)) = 0;
The HAVING clause counts the number of times the forms OdrId is in the orders. The = 0 means it is never there. Plus, you no longer need a select distinct.
Thanks for your prompt answers however I figured out what the problem was and the answer to problem is.
SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails
WHERE (((OrderProdDetails.Priority) Not In (SELECT OrderProdDetails.Priority
FROM OrderProdDetails WHERE (((OrderProdDetails.OrdID)=[Forms]![UpdateOrder]![OdrID])
and ((OrderProdDetails.Priority) Is not null) ))));
I realized that the problem was happening only to those where there was a null value in priority so I puth the check of not null and it worked fine.
Thanks

Why does this query return "incorrect" results?

I have 3 tables:
'CouponType' table:
AutoID Code Name
1 CouT001 SunCoupon
2 CouT002 GdFriCoupon
3 CouT003 1for1Coupon
'CouponIssued' table:
AutoID CouponNo CouponType_AutoID
1 Co001 1
2 Co002 1
3 Co003 1
4 Co004 2
5 Co005 2
6 Co006 2
'CouponUsed' table:
AutoID Coupon_AutoID
1 2
2 3
3 5
I am trying to join 3 tables together using this query below but apparently I am not getting right values for CouponIssued column:
select CouponType.AutoID, Code, Name, Count(CouponIssued.CouponType_AutoID), count(CouponUsed.Coupon_AutoID)
from (CouponType left join CouponIssued
on (CouponType.AutoID = CouponIssued.CouponType_AutoID))
left join CouponUsed
on (couponUsed.Coupon_AutoID = CouponIssued.AutoID)
group by CouponType.AutoID, code, name
order by code
The expected result should be like:
**Auto ID Code Name Issued used**
1 CouT001 SunCoupon 3 2
2 CouT002 GdFriCoupon 3 1
3 CouT003 1for1Coupon 0 0
Thanks!
SELECT t.AutoID
,t.Code
,t.Name
,count(i.CouponType_AutoID) AS issued
,count(u.Coupon_AutoID) AS used
FROM CouponType t
LEFT JOIN CouponIssued i ON i.CouponType_AutoID = t.AutoID
LEFT JOIN CouponUsed u ON u.Coupon_AutoID = i.AutoID
GROUP BY 1,2,3;
You might consider using less confusing names for your table columns. I have made very good experiences with using the same name for the same data across tables (as far as sensible).
In your example, AutoID is used for three different columns, two of which appear a second time in another table under a different name. This would still make sense if Coupon_AutoID was named CouponIssued_AutoID instead.
change count(Coupon.CouponType_AutoID) to count(CouponIssued.CouponType_AutoID) and count(Coupon.Coupon_AutoID) to count(CouponUsed.Coupon_AutoID)