I need to select one column for each matching row in another table. Sounds simple, but there's a twist.
I have one table that has Company IDs, and defines Companies.
I have another table that defines Departments in those companies.
I have a third table that contains the various Department StatusCodes.
The code that I have below works just fine, and is exactly what I want it to do. But I have to hardcode into the query which departments to look for. I'd like it to sub-select based on department codes it finds in the Departments table, and not require me to hard code each possible department that might exist at that company.
I need to select the matching department status from DepartmentStatus based upon what Departments exist for a company as defined in DepartmentsStatus table.
I suspect it's a pivot table, but they are a bit above my level.
(Company Table)
Company_ID Company_Name
-------------------------
1 Home Office
2 Stanton Office
3 NYC Office
(Departments Table)
CompanyID Department_Code
----------------------------
1 Sales
1 Inventory
1 Retail
1 Maint
2 OtherDept
2 ThatDept
2 BobsDept
(DepartmentStatus Table)
Company_ID Department StatusCode
-----------------------------------------
1 Sales InReview
1 Inventory InReview
1 Retail Ready
1 Maint Done
2 OtherDept InReview
2 ThatDept Research
2 BobsDept InReview
Note: I use "TOP 1", even though there is a unique index on Company_ID+Department, so there will never be more than one matching row.
So, for Company_ID=1:
select Company_ID,
(select top 1 StatusCode from DepartmentStatus ds where ds.Company_ID = cm.Company_ID and ds.Department='Sales') as SalesStatus
(select top 1 StatusCode from DepartmentStatus ds where ds.Company_ID = cm.Company_ID and ds.Department='Inventory') as InvStatus
(select top 1 StatusCode from DepartmentStatus ds where ds.Company_ID = cm.Company_ID and ds.Department='Retail') as RetailStatus
(select top 1 StatusCode from DepartmentStatus ds where ds.Company_ID = cm.Company_ID and ds.Department='Main') as MaintStatus
from Company cm
Where cm.CompanyID=1
Results:
Company_ID SalesStatus InvStatus RetailStatus MaintStatus
--------------- --------------- ---------- ------------- ------------
1 InReview InReview Ready Done
Or, for CompanyID=2:
select Company_ID,
(select top 1 StatusCode from DepartmentStatus ds where ds.CompanyID = cm.Company_ID and ds.Department='OtherDept') as OtherDeptStatus
(select top 1 StatusCode from DepartmentStatus ds where ds.CompanyID = cm.Company_ID and ds.Department='ThatDept') as ThatDeptStatus
(select top 1 StatusCode from DepartmentStatus ds where ds.CompanyID = cm.Company_ID and ds.Department='BobsDept') as BobsDeptStatus
from Company cm
Where cm.CompanyID=2
Results:
Company_ID OtherDeptStatus ThatDeptStatus BobsDeptStatus
---------- ---------------- -------------- --------------
2 InReview Research InReview
Thus, For Company 1, I need to go get the status for Departments Sales, Inventory, Retail, and Maint.
But for Company 2, I need to get the status for Departments OtherDept, ThatDept, and BobsDept.
The steps that I think describe what I am trying to do is:
Get list of Departments for Company 1 from Departments Table.
For each of these Departments in Step 1, query DepartmentStatus table for Department equals that department.
For Company 1, query get StatusCode for Departments "Sales, Inventory, Retail, and Maint"
For Company 2, query get StatusCode for Departments "OtherDept, thatDept, and BobsDept."
etc etc
The problem is, I do not know ahead of time (at query time) which departments exist for each Company, so I need to sub-select based upon what departments actually exist for each company.
There are a few other S.O. answers already that are close what I'm asking for, suggesting the use of a JOIN to accomplish this, however, they all assume that you know the values you want to query ahead of time when writing the join statement.
I'm looking for a solution to this problem, not just a correction on my current attempt. If you have a better idea entirely on how to accomplish this, I'd love to see it.
You seem to be looking for conditional aggregation. Using this technique, your query can be simplified as follows, to avoid the need for multiple inline subqueries:
select Company_ID,
max(case when ds.Department='Sales' then ds.StatusCode end) as SalesStatus,
max(case when ds.Department='Inventory' then ds.StatusCode end) as InvStatus,
max(case when ds.Department='Retail' then ds.StatusCode end) as RetailStatus,
max(case when ds.Department='Main' then ds.StatusCode end) as MaintStatus
from
Company cm
inner join DepartmentStatus ds on ds.Company_ID = cm.Company_ID
Where cm.CompanyID=1
group by cm.CompanyID
this would be my (untested) caveman way of solving your problem.
the only thing you need to know in advance is how many different departments you have in the company with the most departments.
DROP TABLE IF EXISTS Iddepartmentstatus;
DECLARE #Result TABLE(
Companyid INT,
Department1 NVARCHAR(MAX),
Department2 NVARCHAR(MAX),
Department3 NVARCHAR(MAX),
Department4 NVARCHAR(MAX),
Department5 NVARCHAR(MAX));
CREATE TABLE Iddepartmentstatus(
Num INT IDENTITY(1, 1),
Department NVARCHAR(MAX),
Statuscode NVARCHAR(MAX));
DECLARE #IDCompany TABLE(
Num INT IDENTITY(1, 1),
Companyid INT,
Companyname NVARCHAR(MAX));
INSERT INTO #IDCompany (Companyid,
Companyname)
SELECT Company_Id,
Company_Name
FROM Company;
DECLARE #Departmentcount INT = 0,
#Departmentstatuscount INT = 0,
#Companycount INT = 0;
WHILE #Companycount <=
(
SELECT MAX(Num)
FROM #IDCompany)
BEGIN
INSERT INTO Iddepartmentstatus (Department,
Statuscode)
SELECT Department,
Statuscode
FROM Departmentstatus
WHERE Company_Id = #Companycount;
INSERT INTO #Result (Companyid)
SELECT #Companycount AS T;
INSERT INTO #Result (Department1)
SELECT IIF(1 <=
(
SELECT MAX(Num)
FROM #IDCompany), Department + ': ' + Departmentstatus, NULL)
FROM Iddepartmentstatus
WHERE Num = 1;
INSERT INTO #Result (Department2)
SELECT IIF(2 <=
(
SELECT MAX(Num)
FROM #IDCompany), Department + ': ' + Departmentstatus, NULL)
FROM Iddepartmentstatus
WHERE Num = 2;
INSERT INTO #Result (Department3)
SELECT IIF(3 <=
(
SELECT MAX(Num)
FROM #IDCompany), Department + ': ' + Departmentstatus, NULL)
FROM Iddepartmentstatus
WHERE Num = 3;
INSERT INTO #Result (Department4)
SELECT IIF(4 <=
(
SELECT MAX(Num)
FROM #IDCompany), Department + ': ' + Departmentstatus, NULL)
FROM Iddepartmentstatus
WHERE Num = 4;
INSERT INTO #Result (Department5)
SELECT IIF(5 <=
(
SELECT MAX(Num)
FROM #IDCompany), Department + ': ' + Departmentstatus, NULL)
FROM Iddepartmentstatus
WHERE Num = 5;
TRUNCATE TABLE Iddepartmentstatus;
SET #Companycount = #Companycount + 1;
END;
DROP TABLE IF EXISTS Iddepartmentstatus;
Related
I have a table 'Tasks' with the following structure
[TaskId],[CompanyId], [Year], [Month], [Value]
220,1,2018,1,50553.32
220,2,2018,2,222038.12
and another table where users have permissions to particular companies in table named 'UsersCopmpanies'
[UserId], [CompanyId]
1,1
and the thing is task no. 220 was moved between companies. In January task belonged to copmanyId=1 and than in February this task belonged to copmanyId = 2.
According to the table 'UsersCopmpanies' user does not have permision to compnayid = 2.
What I need to do is to get both rows from table 'Tasks' expect field Value, because user does not have persmission.
Expected result should be:
[TaskId], [CompanyId], [Year], [Month],[Value]
220,1,2018,1,50553.32
220,2,2018,2,(NULL or somenthing else for.example string 'lack of permission')
You can use a left join:
select t.TaskId, t.CompanyId, t.Year, t.Month,
(case when uc.CompanyId is not null then Value end) as Value
from tasks t left join
UsersCompanies uc
on uc.CompanyId = t.CompanyId and uc.UserId = 1;
I think this query using LEFT JOIN can be work at you expected :
CREATE TABLE #MyTasks
(TaskId int,
CompanyId int,
YearCol varchar(50),
MonthCol varchar(50),
SomeValue varchar(50)
);
GO
INSERT INTO #MyTasks
SELECT 220,1,2018,1,50553.32
UNION
SELECT 220,2,2018,2,222038.12
CREATE TABLE #MyUsersCopmpanies
(UserId int PRIMARY KEY,
CompanyId varchar(50)
);
GO
INSERT INTO #MyUsersCopmpanies
SELECT 1,1
DECLARE #MyUserParam INT = 1;
SELECT #MyTasks.TaskId, #MyTasks.CompanyId, #MyTasks.YearCol, #MyTasks.MonthCol,
CASE WHEN #MyUsersCopmpanies.UserId IS NOT NULL THEN #MyTasks.SomeValue ELSE 'lack of permission' END AS 'ValueTaskByPermissions'
FROM #MyTasks
LEFT JOIN #MyUsersCopmpanies ON #MyUsersCopmpanies.CompanyId = #MyTasks.CompanyId AND #MyUsersCopmpanies.UserId = #MyUserParam;
DROP TABLE #MyTasks
DROP TABLE #MyUsersCopmpanies
RESULT :
TaskId CompanyId YearCol MonthCol ValueTaskByPermissions
220 1 2018 1 50553.32
220 2 2018 2 lack of permission
Some code :
SELECT t.taskid,t.companyid,t.year,t.month,
(CASE WHEN u.companyid IS NOT NULL THEN t.value ELSE "lack of permission" end) AS ValueData
FROM `x_task` t LEFT JOIN x_userscopmpanies u ON u.companyid = t.companyid
I have two tables:
COUNTRY, with the two columns COUNTRY_ID and PERSON_REGION_ID.
PERSON, with many columns, in which PERSON_REGION_ID column is same as COUNTRY.PERSON_REGION_ID, and PERSON_ID is ID column of PERSON.
Query is as follows:
SELECT *
from COUNTRY
where PERSON_REGION_ID IN (
SELECT PERSON_REGION_ID
FROM PERSON
WHERE PERSON_ID IN (111, 888)))
AND COUNTRY_ID = 44;
The above query gives results if any one of the ID as matches (111 or 888).
I want the query to give results if both 111 and 888 has matches else return no results.
How this can be achieved?
I would prefer using joins here
EDIT: To answer your comment, it depends if it's procedure or just query. But you declare variables and go with that
By the way, just for the record ... this is T-SQL, not oracle's syntax
declare #CountryID int -- = 44? (if for some reason you keep CountryID as type other
-- then int, just change it to correct one)
declare #Person1 int -- = 111?
declare #Person2 int -- = 888?
select C.* from Country C
join Person P1 on C.Person_Region_ID = P1.PersionRegion_ID and P1.Country_ID = #CountryID
join Person P2 on C.Person_Region_ID = P2.PersionRegion_ID and P2.Country_ID = #CountryID
where P1.PersionID = #Person1 and P2.PersionID = #Person2
One option is to use group by with having in the subquery:
select *
from COUNTRY
where PERSON_REGION_ID IN (
select PERSON_REGION_ID
from PERSON
where PERSON_ID IN ( 111, 888))
group by PERSON_REGION_ID
having count(PERSON_ID) = 2)
and COUNTRY_ID= 44;
If there are duplicate person_id's per PERSON_REGION_ID , you'll need to use distinct with the count.
You can use EXISTS() like this:
SELECT * from COUNTRY t
WHERE EXISTS(SELECT 1 FROM PERSON s
WHERE t.PERSON_REGION_ID = s.PERSON_REGION_ID
AND PERSON_ID IN ( 111, 888)
GROUP BY PERSON_REGION_ID
HAVING COUNT(DISTINCT PERSON_ID) = 2)
AND COUNTRY_ID = 44
I want to show the pivot table(crosstab) for the given below table.
Table: Employee
CREATE TABLE Employee
(
Employee_Number varchar(10),
Employee_Role varchar(50),
Group_Name varchar(10)
);
Insertion:
INSERT INTO Employee VALUES('EMP101','C# Developer','Group_1'),
('EMP102','ASP Developer','Group_1'),
('EMP103','SQL Developer','Group_2'),
('EMP104','PLSQL Developer','Group_2'),
('EMP101','Java Developer',''),
('EMP102','Web Developer','');
Now I want to show the pivot table for the above data as shown below:
Expected Result:
Employee_Number TotalRoles TotalGroups Available Others Group_1 Group_2
---------------------------------------------------------------------------------------------------
EMP101 2 2 1 1 1 0
EMP102 2 2 1 1 1 0
EMP103 1 2 1 0 0 1
EMP104 1 2 1 0 0 1
Explanation: I want to show the Employee_Number, the TotalRoles which each employee has,
the TotalGroups which are present to all employees, the Available shows the employee available
in how many groups, the Others have to show the employee is available in other's also for which
the group_name have not assigned and finally the Group_Names must be shown in the pivot format.
SELECT * FROM crosstab(
$$SELECT grp.*, e.group_name
, CASE WHEN e.employee_number IS NULL THEN 0 ELSE 1 END AS val
FROM (
SELECT employee_number
, count(employee_role)::int AS total_roles
, (SELECT count(DISTINCT group_name)::int
FROM employee
WHERE group_name <> '') AS total_groups
, count(group_name <> '' OR NULL)::int AS available
, count(group_name = '' OR NULL)::int AS others
FROM employee
GROUP BY 1
) grp
LEFT JOIN employee e ON e.employee_number = grp.employee_number
AND e.group_name <> ''
ORDER BY grp.employee_number, e.group_name$$
,$$VALUES ('Group_1'::text), ('Group_2')$$
) AS ct (employee_number text
, total_roles int
, total_groups int
, available int
, others int
, "Group_1" int
, "Group_2" int);
SQL Fiddle demonstrating the base query, but not the crosstab step, which is not installed on sqlfiddle.com
Basics for crosstab:
PostgreSQL Crosstab Query
Special in this crosstab: all the "extra" columns. Those columns are placed in the middle, after the "row name" but before "category" and "value":
Pivot on Multiple Columns using Tablefunc
Once again, if you have a dynamic set of groups, you need to build this statement dynamically and execute it in a second call:
Selecting multiple max() values using a single SQL statement
You can use the crosstab function for this.
First of all you need to add the tablefunc extension if you haven't already:
CREATE EXTENSION tablefunc;
The crosstab functions require you to pass it a query returning the data you need to pivot, then a list of the columns in the output. (In other ways "tell me the input and the output format you want"). The sort order is important!
In your case, the input query is quite complicated - I think you need to do a load of separate queries, then UNION ALL them to get the desired data. I'm not entirely sure how you calculate the values "TotalGroups" and "Available", but you can modify the below in the relevant place to get what you need.
SELECT * FROM crosstab(
'SELECT employee_number, attribute, value::integer AS value FROM (with allemployees AS (SELECT distinct employee_number FROM employee) -- use a CTE to get distinct employees
SELECT employee_number,''attr_0'' AS attribute,COUNT(distinct employee_role) AS value FROM employee GROUP BY employee_number -- Roles by employee
UNION ALL
SELECT employee_number,''attr_1'' AS attribute,value from allemployees, (select count (distinct group_name) as value from employee where group_name <> '''') a
UNION ALL
SELECT employee_number,''attr_2'' AS attribute, COUNT(distinct group_name) AS value FROM employee where group_name <> '''' GROUP BY employee_number -- Available, do not know how this is calculate
UNION ALL
SELECT a.employee_number, ''attr_3'' AS attribute,coalesce(value,0) AS value FROM allemployees a LEFT JOIN -- other groups. Use a LEFT JOIN to avoid nulls in the output
(SELECT employee_number,COUNT(*) AS value FROM employee WHERE group_name ='''' GROUP BY employee_number) b on a.employee_number = b.employee_number
UNION ALL
SELECT a.employee_number, ''attr_4'' AS attribute,coalesce(value,0) AS value FROM allemployees a LEFT JOIN -- group 1
(SELECT employee_number,COUNT(*) AS value FROM employee WHERE group_name =''Group_1'' GROUP BY employee_number) b on a.employee_number = b.employee_number
UNION ALL
SELECT a.employee_number, ''attr_5'' AS attribute,coalesce(value,0) AS value FROM allemployees a LEFT JOIN -- group 2
(SELECT employee_number,COUNT(*) AS value FROM employee WHERE group_name =''Group_2'' GROUP BY employee_number) b on a.employee_number = b.employee_number) a order by 1,2')
AS ct(employee_number varchar,"TotalRoles" integer,"TotalGroups" integer,"Available" integer, "Others" integer,"Group_1" integer, "Group_2" integer)
I have two tables
STATUS
SNO | STATUS | DEPARTMENT_ID
1 In progress 1
2 Assigned 2
3 Quoted 2
4 Development 3
DEPARTMENTS
SNO | DEPARTMENT |
1 DESIGNING
2 MARKETING
3 PRODUCTION
Now I want a result like this using SQL stored procedure
Some Custom Column Name | DEPARTMENT_ID
DESIGNING -
In Progress 1
MARKETING -
Assigned 2
Quoted 2
PRODUCTION -
Development 3
The custom column will be used to populate a Telerik RadComboBox with DESIGNING, MARKETING and PRODUCTION acting as separators between statuses.
Select Department, -1 from Department_Table
Union
Select StatusName, Department_ID from Status_Table
Please elaborate your question so that we can provide better answer. Currently it seems you just want to return the joined data of both tables.
Often, this type of operation is more easily done at the application level. You can do it in SQL, using union all and order by, however:
select status as CustomColumnName, department
from ((select s.status, d.department, 1 as ordering
from status s join
departments d
on s.department_id = d.sno
) union all
(select d.department, NULL, 0 as ordering
from departments d
)
) dd
order by CustomColumnName, ordering;
Note: this treats the - as NULL.
Try this.Is it ok with other sample data ?
DECLARE #STATUS TABLE (
SNO INT
,[STATUS] VARCHAR(50)
,DEPARTMENT_ID INT
)
INSERT INTO #STATUS
VALUES (1,'In progress' ,1)
,(2,'Assigned',2)
,(3,'Quoted',2)
,(4,'Development',3)
DECLARE #DEPARTMENT TABLE (SNO INT,DEPARTMENT VARCHAR(50))
INSERT INTO #DEPARTMENT
VALUES ( 1,'DESIGNING'),(2,'MARKETING')
,(3,'PRODUCTION')
--select * from #STATUS
--select * from #DEPARTMENT
;
WITH CTE
AS (
SELECT DEPARTMENT [CustomeColumn]
,'-' DEPARTMENT_ID
,sno
FROM #DEPARTMENT
UNION ALL
SELECT [STATUS]
,cast(DEPARTMENT_ID AS VARCHAR(10))
,(
SELECT sno
FROM #DEPARTMENT
WHERE sno = a.DEPARTMENT_ID
)
FROM #STATUS A
)
SELECT *
FROM CTE
ORDER BY sno
Tablename: EntryTable
ID CharityName Title VoteCount
1 save the childrens save them 1
2 save the childrens saving childrens 3
3 cancer research support them 10
Tablename: ContestantTable
ID FirstName LastName EntryId
1 Neville Vyland 1
2 Abhishek Shukla 1
3 Raghu Nandan 2
Desired output
CharityName FullName
save the childrens Neville Vyland
Abhishek Shukla
cancer research Raghu Nandan
I tried
select LOWER(ET.CharityName) AS CharityName,COUNT(CT.FirstName) AS Total_No_Of_Contestant
from EntryTable ET
join ContestantTable CT
on ET.ID = CT.ID
group by LOWER(ET.CharityName)
Please advice.
Please have a look at this sqlfiddle.
Have a try with this query:
SELECT
e.CharityName,
c.FirstName,
c.LastName,
sq.my_count
FROM
EntryTable e
INNER JOIN ContestantTable c ON e.ID = c.EntryId
INNER JOIN (
SELECT EntryId, COUNT(*) AS my_count FROM ContestantTable GROUP BY EntryId
) sq ON e.ID = sq.EntryId
I assumed you actually wanted to join with ContestantTable's EntryId column. It made more sense to me. Either way (joining my way or yours) your sample data is faulty.
Apart from that, you didn't want repeating CharityNames. That's not the job of SQL. The database is just there to store and retrieve the data. Not to format it nicely. You want to work with the data on application layer anyways. Removing repeating data doesn't make this job easier, it makes it worse.
Most people do not realize that T-SQL has some cool ranking functions that can be used with grouping. Many things like reports can be done in T-SQL.
The first part of the code below creates two local temporary tables and loads them with data for testing.
The second part of the code creates the report. I use two common table expressions (CTE). I could have used two more local temporary tables or table variables. It really does not matter with this toy example.
The cte_RankData has two columns RowNum and RankNum. If RowNum = RankNum, we are on the first instance of charity. Print out the charity name and the total number of votes. Otherwise, print out blanks.
The name of the contestant and votes for that contestant are show on the detail lines. This is a typical report with sub totals show at the top.
I think this matches the report output that you wanted. I ordered the contestants by most votes descending.
Sincerely
John Miner
www.craftydba.com
--
-- Create the tables
--
-- Remove the tables
drop table #tbl_Entry;
drop table #tbl_Contestants;
-- The entries table
Create table #tbl_Entry
(
ID int,
CharityName varchar(25),
Title varchar(25),
VoteCount int
);
-- Add data
Insert Into #tbl_Entry values
(1, 'save the childrens', 'save them', 1),
(2, 'save the childrens', 'saving childrens', 3),
(3, 'cancer research', 'support them', 10)
-- The contestants table
Create table #tbl_Contestants
(
ID int,
FirstName varchar(25),
LastName varchar(25),
EntryId int
);
-- Add data
Insert Into #tbl_Contestants values
(1, 'Neville', 'Vyland', 1),
(2, 'Abhishek', 'Shukla', 1),
(3, 'Raghu', 'Nandan', 2);
--
-- Create the report
--
;
with cte_RankData
as
(
select
ROW_NUMBER() OVER (ORDER BY E.CharityName ASC, VoteCount Desc) as RowNum,
RANK() OVER (ORDER BY E.CharityName ASC) AS RankNum,
E.CharityName as CharityName,
C.FirstName + ' ' + C.LastName as FullName,
E.VoteCount
from #tbl_Entry E inner join #tbl_Contestants C on E.ID = C.ID
),
cte_SumData
as
(
select
E.CharityName,
sum(E.VoteCount) as TotalCount
from #tbl_Entry E
group by E.CharityName
)
select
case when RowNum = RankNum then
R.CharityName
else
''
end as rpt_CharityName,
case when RowNum = RankNum then
str(S.TotalCount, 5, 0)
else
''
end as rpt_TotalVotes,
FullName as rpt_ContestantName,
VoteCount as rpt_Votes4Contestant
from cte_RankData R join cte_SumData S
on R.CharityName = S.CharityName