Check the conditions on the rows having same ids in sql server - sql

I have a table as below
Id | CompanyName
--- | ---
100 | IT
100 | R&D
100 | Financial
100 | Insurance
110 | IT
110 | Financial
110 | Product Based
111 | R&D
111 | IT
The table contains the data like the above structure but contains thousands of ids like these
I want to find out all the ids in which all the company names are IT and R&D.If for any of the ids the company name is not in neither IT not R&D then dont consider those ids.
e.g. id 100 cannot be in this list because it has extra company name as Financial but id 111 will be considered because all the companies are IT and R&D
Any help?

select id,
sum(case when CompanyName='IT' OR CompanyName='R&D' then 1 else 0 end) as c1
from t
group by id
having count(*)=2 and c1=2

I would do this as:
select id
from t
having sum(case when CompanyName not in ('IT', 'R&D') then 1 else 0 end) = 0 and
sum(case when CompanyName in ('IT', 'R&D') then 1 else 0 end) = 2; -- use "> 0" if you want either one.
Note: This assumes that you don't have duplicates in the table.

SELECT DISTINCT Id
FROM companies
WHERE
Id NOT IN (
SELECT DISTINCT Id
FROM companies
WHERE
CompanyName != 'IT'
AND CompanyName != 'R&D'
)
;
Subquery: Select all records - only Id, distinctly - with CompanyName different from "IT" and "R&D".
Main query: Select all records - only Id, distinctly - which are not in the prior list.
Results: 111
Tested on sqlfiddle.com with option "MS SQL Server 2014" and schema syntax:
CREATE TABLE companies
([Id] int, [CompanyName] varchar(255))
;
INSERT INTO companies
([Id], [CompanyName])
VALUES
(100, 'IT'),
(100, 'R&D'),
(100, 'Financial'),
(100, 'Insurance'),
(110, 'IT'),
(110, 'Financial'),
(110, 'Product Based'),
(111, 'R&D'),
(111, 'IT')
;

Related

Daily record count based on status allocation

I have a table named Books and a table named Transfer with the following structure:
CREATE TABLE Books
(
BookID int,
Title varchar(150),
PurchaseDate date,
Bookstore varchar(150),
City varchar(150)
);
INSERT INTO Books VALUES (1, 'Cujo', '2022-02-01', 'CentralPark1', 'New York');
INSERT INTO Books VALUES (2, 'The Hotel New Hampshire', '2022-01-08', 'TheStrip1', 'Las Vegas');
INSERT INTO Books VALUES (3, 'Gorky Park', '2022-05-19', 'CentralPark2', 'New York');
CREATE TABLE Transfer
(
BookID int,
BookStatus varchar(50),
TransferDate date
);
INSERT INTO Transfer VALUES (1, 'Rented', '2022-11-01');
INSERT INTO Transfer VALUES (1, 'Returned', '2022-11-05');
INSERT INTO Transfer VALUES (1, 'Rented', '2022-11-06');
INSERT INTO Transfer VALUES (1, 'Returned', '2022-11-09');
INSERT INTO Transfer VALUES (2, 'Rented', '2022-11-03');
INSERT INTO Transfer VALUES (2, 'Returned', '2022-11-09');
INSERT INTO Transfer VALUES (2, 'Rented', '2022-11-15');
INSERT INTO Transfer VALUES (2, 'Returned', '2022-11-23');
INSERT INTO Transfer VALUES (3, 'Rented', '2022-11-14');
INSERT INTO Transfer VALUES (3, 'Returned', '2022-11-21');
INSERT INTO Transfer VALUES (3, 'Rented', '2022-11-25');
INSERT INTO Transfer VALUES (3, 'Returned', '2022-11-29');
See fiddle.
I want to do a query for a date interval (in this case 01.11 - 09.11) that returns the book count for each day based on BookStatus from Transfer, like so:
+────────────+────────+────────+────────+────────+────────+────────+────────+────────+────────+
| Status | 01.11 | 02.11 | 03.11 | 04.11 | 05.11 | 06.11 | 07.11 | 08.11 | 09.11 |
+────────────+────────+────────+────────+────────+────────+────────+────────+────────+────────+
| Rented | 2 | 1 | 2 | 2 | 0 | 2 | 3 | 3 | 1 |
+────────────+────────+────────+────────+────────+────────+────────+────────+────────+────────+
| Returned | 1 | 2 | 1 | 1 | 3 | 1 | 0 | 0 | 2 |
+────────────+────────+────────+────────+────────+────────+────────+────────+────────+────────+
A book remains rented as long as it was not returned, and is counted as 'Returned' every day until it is rented out again.
This is what the query result would look like for one book (BookID 1):
I see two possible solutions.
Dynamic solution
Use a (recursive) common table expression to generate a list of all the dates that fall within the requested range.
Use two cross apply statements that each perform a count() aggregation function to count the amount of book transfers.
-- generate date range
with Dates as
(
select convert(date, '2022-11-01') as TransferDate
union all
select dateadd(day, 1, d.TransferDate)
from Dates d
where d.TransferDate < '2022-11-10'
)
select d.TransferDate,
c1.CountRented,
c2.CountReturned
from Dates d
-- count all rented books up till today, that have not been returned before today
cross apply ( select count(1) as CountRented
from Transfer t1
where t1.BookStatus = 'Rented'
and t1.TransferDate <= d.TransferDate
and not exists ( select 'x'
from Transfer t2
where t2.BookId = t1.BookId
and t2.BookStatus = 'Returned'
and t2.TransferDate > t1.TransferDate
and t2.TransferDate <= d.TransferDate ) ) c1
-- count all returned books for today
cross apply ( select count(1) as CountReturned
from Transfer t1
where t1.BookStatus = 'Returned'
and t1.TransferDate = d.TransferDate ) c2;
Result:
TransferDate CountRented CountReturned
------------ ----------- -------------
2022-11-01 1 0
2022-11-02 1 0
2022-11-03 2 0
2022-11-04 2 0
2022-11-05 1 1
2022-11-06 2 0
2022-11-07 2 0
2022-11-08 2 0
2022-11-09 0 2
2022-11-10 0 0
This result is not the pivoted outcome described in the question. However, pivoting this dynamic solution requires dynamic sql, which is not trivial!
Static solution
This will delivery the exact outcome as described in the question (including the date formatting), but requires the date range to be fully typed out once.
The essential building blocks are similar to the dynamic solution above:
A recursive common table expression to generate a date range.
Two cross apply's to perform the counting calculations like before.
There is also:
An extra cross join to duplicate the date range for each BookStatus (avoid NULL values in the result).
Some replace(), str() and datepart() functions to format the dates.
A case expression to merge the two counts to a single column.
The solution is probably not the most performant, but it does deliver the requested result. If you want to validate for BookID=1 then just uncomment the extra WHERE filter clauses.
with Dates as
(
select convert(date, '2022-11-01') as TransferDate
union all
select dateadd(day, 1, d.TransferDate)
from Dates d
where d.TransferDate < '2022-11-10'
),
PivotInput as
(
select replace(str(datepart(day, d.TransferDate), 2), space(1), '0') + '.' + replace(str(datepart(month, d.TransferDate), 2), space(1), '0') as TransferDate,
s.BookStatus as [Status],
case when s.BookStatus = 'Rented' then sc1.CountRented else sc2.CountReturned end as BookStatusCount
from Dates d
cross join (values('Rented'), ('Returned')) s(BookStatus)
cross apply ( select count(1) as CountRented
from Transfer t1
where t1.BookStatus = s.BookStatus
and t1.TransferDate <= d.TransferDate
--and t1.BookID = 1
and not exists ( select 'x'
from Transfer t2
where t2.BookId = t1.BookId
and t2.BookStatus = 'Returned'
and t2.TransferDate > t1.TransferDate
and t2.TransferDate <= d.TransferDate ) ) sc1
cross apply ( select count(1) as CountReturned
from Transfer t3
where t3.TransferDate = d.TransferDate
--and t3.BookID = 1
and t3.BookStatus = 'Returned' ) sc2
)
select piv.*
from PivotInput pivi
pivot (sum(pivi.BookStatusCount) for pivi.TransferDate in (
[01.11],
[02.11],
[03.11],
[04.11],
[05.11],
[06.11],
[07.11],
[08.11],
[09.11],
[10.11])) piv;
Result:
Status 01.11 02.11 03.11 04.11 05.11 06.11 07.11 08.11 09.11 10.11
Rented 1 1 2 2 1 2 2 2 0 0
Returned 0 0 0 0 1 0 0 0 2 0
Fiddle to see things in action.

SQLite query - filter name where each associated id is contained within a set of ids

I'm trying to work out a query that will find me all of the distinct Names whose LocationIDs are in a given set of ids. The catch is if any of the LocationIDs associated with a distinct Name are not in the set, then the Name should not be in the results.
Say I have the following table:
ID | LocationID | ... | Name
-----------------------------
1 | 1 | ... | A
2 | 1 | ... | B
3 | 2 | ... | B
I'm needing a query similar to
SELECT DISTINCT Name FROM table WHERE LocationID IN (1, 2);
The problem with the above is it's just checking if the LocationID is 1 OR 2, this would return the following:
A
B
But what I need it to return is
B
Since B is the only Name where both of its LocationIDs are in the set (1, 2)
You can try to write two subquery.
get count by each Name
get count by your condition.
then join them by count amount, which means your need to all match your condition count number.
Schema (SQLite v3.17)
CREATE TABLE T(
ID int,
LocationID int,
Name varchar(5)
);
INSERT INTO T VALUES (1, 1,'A');
INSERT INTO T VALUES (2, 1,'B');
INSERT INTO T VALUES (3, 2,'B');
Query #1
SELECT t2.Name
FROM
(
SELECT COUNT(DISTINCT LocationID) cnt
FROM T
WHERE LocationID IN (1, 2)
) t1
JOIN
(
SELECT COUNT(DISTINCT LocationID) cnt,Name
FROM T
WHERE LocationID IN (1, 2)
GROUP BY Name
) t2 on t1.cnt = t2.cnt;
| Name |
| ---- |
| B |
View on DB Fiddle
You can just use aggregation. Assuming no duplicates in your table:
SELECT Name
FROM table
WHERE LocationID IN (1, 2)
GROUP BY Name
HAVING COUNT(*) = 2;
If Name/LocationID pairs can be duplicated, use HAVING COUNT(DISTINCT LocationID) = 2.

Checking using nvl2 with multiple group by

I have a table like
------------------------
S.No Name Amount Imp_Num
1 A 10 12345
2 B 20
3 A 30
4 C 40 4555
5 B 50
--------------------------
and I want something like
---------------------------------------
Name Total_Amount Imp_Num Imp_Num_Present
A 40 12345 Y
B 70 null N
C 40 4555 Y
---------------------------------------
The important_number_present column should be Y if the important number is present for the particular name at least once and the important number should be captured. The important number for a particular name is assumed to be the same.If different the latest one should be displayed as imp_numb. (But this is of secondary priority).
I tried something like
Select sum(amount) as total_amount, imp_num, nvl2(imp_num,'Y','N') from sampletable group by imp_num;
But name can't be retrieved and the data doesn't make sense without the name. I might be doing something totally wrong. Can a feasible solution be done in SQL rather than in pl/sql.
Group by with name is returning the name with a null entry and imp_num entry.
I am cracking my head on this. Would be of great help, if someone solves it.
Thanks in advance
You could use a (fake) aggregation function on imp_num and group by name
Select Name, sum(amount) as total_amount, max(imp_num), nvl2( max(imp_num),'Y','N')
from sampletable
group by Name;
EDIT: Another solution with COUNT function. DEMO
SELECT name
,SUM(amount) AS total_amount
,MAX(imp_num) AS Imp_Num
,CASE
WHEN Count(imp_num) > 0
THEN 'Y'
ELSE 'N'
END AS Imp_Num_Present
FROM yourtable
GROUP BY name
You may also use a MAX( CASE ) block
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE yourtable
(S_No int, Name varchar2(1), Amount int, Imp_Num varchar2(5))
;
INSERT ALL
INTO yourtable (S_No, Name, Amount, Imp_Num)
VALUES (1, 'A', 10, '12345')
INTO yourtable (S_No, Name, Amount, Imp_Num)
VALUES (2, 'B', 20, NULL)
INTO yourtable (S_No, Name, Amount, Imp_Num)
VALUES (3, 'A', 30, NULL)
INTO yourtable (S_No, Name, Amount, Imp_Num)
VALUES (4, 'C', 40, '4555')
INTO yourtable (S_No, Name, Amount, Imp_Num)
VALUES (5, 'B', 50, NULL)
SELECT * FROM dual
;
Query 1:
SELECT Name,
SUM (amount) AS total_amount,
MAX (imp_num) AS Imp_Num,
CASE
WHEN MAX (CASE WHEN imp_num IS NOT NULL THEN 1 ELSE 0 END) = 1
THEN
'Y'
ELSE
'N'
END
AS Imp_Num_Present
FROM yourtable
GROUP BY Name
Results:
| NAME | TOTAL_AMOUNT | IMP_NUM | IMP_NUM_PRESENT |
|------|--------------|---------|-----------------|
| A | 40 | 12345 | Y |
| B | 70 | (null) | N |
| C | 40 | 4555 | Y |

List the names of people that have never scored above 3

From a table like this:
Name | Score
------ | ------
Bill | 1
Bill | 2
Bill | 1
Steve | 1
Steve | 4
Steve | 1
Return the names of people that have never scored above 3
Answer would be:
Name |
------ |
Bill |
The key is to get the maximum score for each person, then filter to those whose maximum is less than 3. To get the maximum you need to do an aggregate (GROUP BY and MAX). Then to apply filters to aggregates you must use HAVING rather than WHERE. So you would end up with:
SELECT Name, MAX(Score) AS HighScore
FROM Table
GROUP BY Name
HAVING MAX(Score) <= 3;
one solution would be:
SELECT DISTINCT name
FROM mytable
WHERE Name NOT IN
( SELECT Name
FROM mytable
WHERE score > 3
)
sample table :
DECLARE #Table1 TABLE
(Name varchar(5), Score int)
;
INSERT INTO #Table1
(Name, Score)
VALUES
('Bill', 1),
('Bill', 2),
('Bill', 1),
('Steve', 1),
('Steve', 4),
('Steve', 1)
;
Script :
;with CTE AS (
select Name,Score from #Table1
GROUP BY Name,Score
HAVING (Score) > 3 )
Select
NAME,
Score
from #Table1 T
where not EXISTS
(select name from CTE
where name = T.Name )
Result :
NAME Score
Bill 1
Bill 2
Bill 1
SELECT name
FROM table_name
WHERE score < 3

SQL query for sales report by date

I have a table of sales leads:
CREATE TABLE "lead" (
"id" serial NOT NULL PRIMARY KEY,
"marketer" varchar(500) NOT NULL,
"date_set" varchar(500) NOT NULL
)
;
INSERT INTO lead VALUES (1, 'Joe', '05/01/13');
INSERT INTO lead VALUES (2, 'Joe', '05/02/13');
INSERT INTO lead VALUES (3, 'Joe', '05/03/13');
INSERT INTO lead VALUES (4, 'Sally', '05/03/13');
INSERT INTO lead VALUES (5, 'Sally', '05/03/13');
INSERT INTO lead VALUES (6, 'Andrew', '05/04/13');
I want to produce a report that summarizes the number of records each marketer has for each day. It should look like this:
| MARKETER | 05/01/13 | 05/02/13 | 05/03/13 | 05/04/13 |
--------------------------------------------------------
| Joe | 1 | 1 | 1 | 0 |
| Sally | 0 | 0 | 2 | 1 |
| Andrew | 0 | 0 | 0 | 1 |
What's the SQL query to produce this?
I have this example set up on SQL Fiddle: http://sqlfiddle.com/#!12/eb27a/1
Pure SQL cannot produce such structure (it is two dimensional, but sql return plain list of records).
You could make query like this:
select marketer, date_set, count(id)
from lead
group by marketer, date_set;
And vizualise this data by your reporting system.
You can do it like this:
select
marketer,
count(case when date_set = '05/01/13' then 1 else null end) as "05/01/13",
count(case when date_set = '05/02/13' then 1 else null end) as "05/02/13",
count(case when date_set = '05/03/13' then 1 else null end) as "05/03/13",
count(case when date_set = '05/04/13' then 1 else null end) as "05/04/13"
from lead
group by marketer