sql query in a performant way - sql

In my case there is a table called ServiceCarrier as below:
ServiceId CarrierId
1 1
2 1
4 1
1 2
2 2
5 2
1 20028
2 20028
5 20028
I want to group it by CarrierId and want to get the intersection in between grouped ones.
my expected result is as below
ServiceId
1
2

Assuming (serviceid, carrierid) is unique. GROUP BY serviceid and get the count(*). This will tell how many carriers do offer the service. Since you want only services offered by all carriers, limit the result in a HAVING clause to only keep the services, which have a count equal to the count of all carriers. That indicates the service is offered by all carriers.
SELECT serviceid
FROM elbat
GROUP BY serviceid
HAVING count(*) = (SELECT count(DISTINCT carrierid)
FROM elbat);
SQL Fiddle

To get what you wrote as 'expected' and 'intersection', perhaps try this?
select serviceID from TSO where serviceID=carrierID
If you would like an aggregation, for example, count or sum, perhaps try this?
select serviceID, count(carrierID) as count from TSO where
serviceID=carrierID group by serviceID

Related

How do I count how many emails each customer has received when there are multiple emails to count?

I am looking to count the number of emails each customer has received however am having trouble as there is more than one customer in the table that needs counting meaning a simple where clause isn't enough.
Here is an example of the Data:
CustomerID
EmailName
1
EmailA
1
EmailB
2
EmailA
2
EmailB
2
EmailC
3
EmailA
3
EmailB
I am able to count for a specific customer by using a where clause:
WHERE CustomerID = "1"
Which will return:
CustomerID
NumberOfEmailsSent
1
2
The issue I am having is I would like to get the following result:
CustomerID
NumberOfEmailsSent
1
2
2
3
3
2
The data set I am working with has thousands of email addresses so querying each email address separately is an unrealistic solution.
That is what GROUP BY is for.
SELECT CustomerID, COUNT(EmailName)
FROM YourTable
GROUP BY CustomerID
I think that you just need a GROUP BY clause:
SELECT CustomerID, COUNT(EmailName) as 'NumberOfEmailsSent'
FROM tbl
GROUP BY CustomerID
Output check here on DB<>FIDDLE

Optimize SQL query: how to check if an Id is assigned to more than other Id (it should not)

I have a simple table like
client_id , company_id
100 1
101 1
102 1
200 2
200 2
201 2
For each client_id I should have just one company_id. I need to make a query to check this.
What I was doing is:
SELECT
client_id,
count(DISTINCT company_id) as count
FROM table GROUP BY
client_id
HAVING count > 1;
If it's not empty, it should trigger an alert.
However, I was wondering if I could optimize it a little, because I don't really need to get every row, I just need to know if this query results in AT LEAST one row.
Is it possibe?
EXISTS might be faster:
select distinct client_id
from t
where exists (select 1
from t t2
where t2.client_id = t.client_id and t2.company_id <> t.company_id
);
I would expect a performance improvement under two conditions:
There is an index on (client_id, company_id).
Most clients have only one company.

How can i choose the Max Decision number of two different column?

The same client can have multiple decision numbers, I need to choose the max of multiple the decision number. Please help.
sample data
CNO DNO
1 1
1 2
3 3
You can use window function like:
max(decisionNumber) over (partition by ClientName) as 'MAXofDecisionNumber'
SELECT DECNO,MAX(CNO) FROM TABLE1 GROUP BY DECNO
FOR BOTH MAX
SELECT DECNO,MAX(CNO) FROM TABLE1
WHERE DECNO = (SELECT MAX(DECNO) FROM TABLE1)
GROUP BY DECNO
Live Demo
http://sqlfiddle.com/#!18/3cdd0/3

MS Access Query - Count using most recent inquiries

I am still new to this Access and am not sure how to do this. I have this prospective customer table:
my table and the expected result:
Or here is the data, not sure if it will show correctly:
ID Dates Status
1 12-Sep-15 Follow up
1 2-Jan-15 Request
1 15-Apr-14 Letter
2 1-Sep-15 Request
2 1-Apr-15 Letter
3 12-Dec-15 Follow up
3 11-Sep-14 Request
3 12-Mar-14 Letter
4 14-Jan-16 Letter
4 12-Dec-15 Email
5 12-Jan-16 Letter
5 1 Des 2015 Email
And the result would be like this:
Follow up 2
Request 1
Letter 2
I first tried this SQL:
SELECT id, status, Max(dates) AS TEST
FROM Sample
GROUP BY id, status;
which still would give me the original table. I was hoping it would return the id and status for most recent dates.
Any help would be deeply appreciated. Thank you very much !!
If you want id and status for the latest date, then you need to use a subselect to get the max dates by id and join your table on it by id and date:
SELECT s.id, s.status, t.maxdate
FROM status as s
INNER JOIN
(SELECT id, Max(dates) AS maxdate
FROM Sample
GROUP BY id) as t ON t.id=s.id and t.maxdate=s.dates

SQL Nested Select Statement

I have the following SQL Code which is not giving me my desired results.
SELECT
POLICIES.CLIENTS_ID,
POLICIES.CLIENTCODE,
COUNT(POLICIES.POLICIES_ID) as [Total Policies],
(
SELECT
COUNT(POLICIES.POLICIES_ID)
FROM
POLICIES
WHERE
POLICIES.COVCODE = 'AUT'
) as [Auto Policies]
FROM
POLICIES
LEFT JOIN CLIENTS
ON CLIENTS.CLIENTS_ID = POLICIES.CLIENTS_ID
WHERE
POLICIES.CNR IS NULL
GROUP BY
POLICIES.CLIENTS_ID,
POLICIES.CLIENTCODE
ORDER BY
POLICIES.CLIENTS_ID
I get a result like this:
ID CODE Total Auto
3 ABCDE1 1 999999
4 ABCDE2 1 999999
5 ABCDE3 2 999999
6 ABCDE4 2 999999
I would like for the last column to COUNT the total auto policies that exists for that clientid rather than all of the auto policies that exist. I believe I need a nested select statement that somehow groups all like results on the clientid, but it ends up returning more than 1 row and throws the error.
If I add:
GROUP BY
POLICIES.CLIENTS_ID
I get:
Subquery returned more than 1 value. This is not permitted when the....
Any help would be appreciated greatly!
Thank you
You can use a CASE statement to do this. Instead of your subquery in the SELECT clause use:
SUM(CASE WHEN POLICIES.COVCODE = 'AUT' THEN 1 ELSE 0 END) as [AUTO POLICIES]
As Martin Smith pointed out. If client_id has multiple client_codes then this will give you the count of records for each combination of client_id/client_code. If client_id is 1:1 with client_code then this will give you a count of records for each distinct client_id, which I suspect is the case from your example and question.
Unrelated: You have a LEFT JOIN to your Clients table, but you don't use your Clients table anywhere int he query. Consider removing it if you don't need to select or filter by any its fields, since it's just unused overhead.
What if you modify the inner query for getting count to something like
SUM(CASE WHEN POLICIES.COVCODE = 'AUT' THEN 1 ELSE 0 END) as [Auto Policies]