I have three tables:
Table 1 : Emplyee Details
E_Id Name Department
1 A Manager
2 B Manager
3 C Manager
4 D Engineer
5 E Engineer
6 F Engineer
Table 2 : Salary Details
Sl_Id Name Amount
1 Bsaic_Mng 30000
2 Basic_ENG 20000
Table 3 : Employee Salary
ES_Id E_Id Sl_Id
1 1 1
2 2 1
3 4 2
4 5 2
Here in Table3 field E_ID is a reference to Table1.E_Id
and SL_ID is a reference to Table2.SL_id.
So I wanto Result Which Employee Has Not Salary Define Like E_Id 3 AND 6
You can achieve it as below:
Employee whose salary is not defined in EmployeeSalary table
SELECT *
FROM Emplyee
WHERE E_Id NOT IN (
SELECT E_Id
FROM EmployeeSalary
)
JOIN is not required here
You can do it using a LEFT JOIN like following.
SELECT ED.* FROM
[Emplyee Details] ED
LEFT JOIN [Employee Salary] ES ON ES.E_ID=ED.E_Id
WHERE ES.E_ID IS NULL
I feel NOT EXISTS should be a better option to achieve this for this case.
SELECT * FROM
[Emplyee Details] ED
WHERE NOT EXISTS
(
SELECT 1 FROM [Employee Salary] ES WHERE ES.E_ID=ED.E_Id
)
Related
We have some warehouses which we do inventory on a regular basis.
I need to make a report (the goal is to use Microsoft PowerBi) to show the discrepancies between different inventories for the same warehouse (location), but the only common column is the warehouse number.
Product column is dynamically: it may appear on an inventory and not on another (and that is exactly what we need to know)
Each inventory is on a different table, like this:
TABLE A
LOCATION PRODUCTS QTY
WH 1 PRODUCT NO. 1 10
WH 1 PRODUCT NO. 2 100
WH 1 PRODUCT NO. 333 5
WH 2 PRODUCT NO. YYY 22
TABLE B
LOCATION PRODUCTS QTY
WH 1 PRODUCT NO. 1 10
WH 1 PRODUCT NO. 2 100
WH 1 PRODUCT NO. 333 5
WH 1 PRODUCT NO. XXX 77
WH 2 PRODUCT NO. YYY 45
WH 1 PRODUCT NO. YYY 555
WH 2 PRODUCT NO. 1 14
Expected output should be like this:
Location products QTY A QTY B
WH 1 PRODUCT NO. 1 10 10
WH 1 PRODUCT NO. 2 100 100
WH 1 PRODUCT NO. 333 5 5
WH 1 PRODUCT NO. XXX - 77
WH 1 PRODUCT NO. YYY - 555
WH 2 PRODUCT NO. 1 - 14
WH 2 PRODUCT NO. YYY 22 45
I´ve read about FULL OUTER JOIN and also PIVOT TABLES, but I was unable to fully understand and use them (and I´m also not sure they are the goal here).
SQL version is Microsoft SQL Server 2016 Standard
One option is a Union ALL in concert with a PIVOT or Conditional Aggregation
Example
Select *
From (
Select Location
,Products
,Qty
,Col = 'Qty A'
From Table A
Union All
Select Location
,Products
,Qty
,Col = 'Qty B'
From Table B
) src
Pivot (sum(Qty) for Col in ([Qty A],[Qty B] ) ) pvt
Isn't this a simple full join? Am I missing something? Pivot seems unnecessary here.
select isnull(A.location,B.location) location, isnull(A.products, B.products) products,
A.QTY A_QTY, B.QTY B_QTY
from tableA A
full outer join tableB B
on A.location = B.location
and A.products = B.products
A_QTY or B_QTY will be null if it's in one table but not the other.
Try this:
;with tableA (LOCATION, PRODUCTS, QTY)
as
(
select 'WH 1','PRODUCT NO. 1',10
union all
select 'WH 1','PRODUCT NO. 2',100
union all
select 'WH 1','PRODUCT NO. 333',5
union all
select 'WH 2','PRODUCT NO. YYY',22
)
, tableB (LOCATION, PRODUCTS, QTY)
as
(
select 'WH 1','PRODUCT NO. 1',10
union all
select 'WH 1','PRODUCT NO. 2',100
union all
select 'WH 1','PRODUCT NO. 333',5
union all
select 'WH 1','PRODUCT NO. XXX',77
union all
select 'WH 2','PRODUCT NO. YYY',45
union all
select 'WH 1','PRODUCT NO. YYY',555
union all
select 'WH 2','PRODUCT NO. 1',14
)
select
LOCATION
, PRODUCTS
, isnull(QtyA,0) [Qty A]
, isnull(QtyB,0) [Qty B]
from
(
select
LOCATION
, PRODUCTS
, QTY
, 'QtyA' ColumnQty
from tableA
union all
select
LOCATION
, PRODUCTS
, QTY
, 'QtyB' ColumnQty
from tableB
) source
pivot
(
sum(QTY) for ColumnQty in ([QtyA],[QtyB])
) pvt
I've tested it and it works as can be seen here.
How to select the value from the table based on category_id?
I have a table like this. Please help me.
Table A
ID Name category_id
-------------------
1 A 1
2 A 1
3 B 1
4 C 2
5 C 2
6 D 2
7 E 3
8 E 3
9 F 3
How to get the below mentioned output from table A?
ID Name category_id
--------------------
1 A 1
2 A 1
4 C 2
5 C 2
7 E 3
8 E 3
Give a row number for each row based on group by category_id and sort by ascending order of ID. Then select the rows having row number 1 and 2.
Query
;with cte as (
select [rn] = row_number() over(
partition by [category_id]
order by [ID]
), *
from [your_table_name]
)
select [ID], [Name], [category_id]
from cte
where [rn] < 3;
Kindly run this query It really help You Out.
SELECT tbl.id,tbl.name, tbl.category_id FROM TableA as tbl WHERE
tbl.name IN(SELECT tbl2.name FROM TableA tbl2 GROUP BY tbl2.name HAVING Count(tbl2.name)> 1)
Code select all category_id from TableA which has Name entries more then one. If there is single entry of any name group by category_id then such data will be excluded. In above example questioner want to eliminate those records that have single Name entity like wise category_id 1 has name entries A and B among which A has two entries and B has single entry so he want to eliminate B from result set.
I have a temp table with the following structure:
StudentID VALUE
1 5
2 NULL
and need to map the values from it to the table below:
StudentID DEPT
1 1
1 2
2 3
2 4
So the output should be like this:
StudentID DEPT VALUE
1 1 5
1 2 5
2 3 NULL
2 4 NULL
Do I need to use join or merge my table consisits of million record?
I have tried using joins but i am not getting exact what i need?
a JOIN. like this:
SELECT S.StudentId, S.Dept, V.Value
FROM Student AS S
JOIN #TEMP AS V
ON V.StudentId = S.StudentId
ORDER BY V.StudentId
SELECT table2.StudentID
,table2.DEPT
,#TEMP.Value
FROM table2
LEFT JOIN #TEMP
ON table2.StudentID = #TEMP.StudentID
i am working on sql query please please check and give me the solution
please find the bellow task brief detail of tables
table 1 'tbl_SuperAdmin' having on filed
data like example
s_id name age gtc.......
1 abc 23 .........
2 cda 42 ..........
another table 2 having 'tbl_Renewal'
renewalid renname date supid Payed etc......
1 first - 1 1 ........
2 first - 2 1 ........
3 second - 1 0 ........
4 second - 1 1 ........
5 third- 1 1 ........
query is select *from tbl_superadmin as a inner join tbl_Renewal as b on a.s_id=b.supid
Result is
s_id name age gtc....... renewalid renname date supid Payed etc.......
1 abc 23 .............1 first - 1 1 ........
2 cda 42 .............2 first - 2 1 ........
1 abc 23 .............3 second - 1 0 ........
1 abc 23 .............4 second - 1 1 ........
1 abc 23 .............5 third - 1 1 ........
here i want how has payed=1 and renewalid desc and no need for s_id not repeat of 1 more the one time
i want to show my result like
s_id name age gtc....... renewalid renname date supid Payed etc.......
1 abc 23 .............5 third - 1 1 ........
2 cda 42 .............2 first - 2 1 ........
like this result i want please any body help it
thanks
pradeep
To remove the duplicates you need to use Windows Function.
With cte as
(
select *,row_number() over(partition by s_id order by renewalid desc )rn
from tbl_superadmin as a
inner join tbl_Renewal as b
on a.s_id=b.supid
)
Select * from cte where rn=1
Demo in SQL FIDDLE
Update
;With cte as
(
select *,row_number() over(partition by s_id order by renewalid desc )rn
from alz_SuperAdmin as a inner join alz_Renewal as b on a.s_id=b.supid
)
select top 2 CONVERT(varchar(6),a.lastdate,6) as lastdate,
(select name from alz_states where ID=a.joblocation ) as locat,
from alz_jobpost as a
inner join
(Select col1,col2,col3.... from cte where rn=1 )ct
on ct.commonColumn=a.CommonColumn and
ct.Product in
(Select query)
You should use a GROUP BY and ORDER in your query. Something like this:
SELECT
*
FROM
tbl_SuperAdmin AS a
INNER JOIN
tbl_Renewal AS b
ON
a.s_id = b.supid
GROUP BY
a.s_id
ORDER BY
b.renewalid
DESC
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
A real recursion with CTE?
Given a hierarchical table that references itself, such as an Employee table that has the following columns:
Table Employee
Column Id INT NOT NULL
Column ParentId INT NOT NULL (references Id)
Column Name NVARCHAR(60) NOT NULL
The following query will give me all records rooted at a given EmployeeId:
DECLARE #EmployeeId INT = <%insert EmployeeId here%>;
WITH CDE AS
(
SELECT
*,
0 AS Level
FROM
collaboration.Employee AS E
WHERE
Id = #EmployeeId
UNION ALL
SELECT
E.*,
CDE.Level + 1 AS Level
FROM
collaboration.Employee AS E
INNER JOIN
CDE ON E.ParentId = CDE.Id AND E.Id <> 0
)
SELECT DISTINCT
CDE.*
FROM
CDE
ORDER BY
CDE.Level
What I would like is to be able to sort by "branch" and then "level", if that makes sense. So given the following table:
1 0 John Smith
2 1 John Doe
3 1 Jane Williams
4 2 Ian Bond
5 2 James Fleming
I would like the result to look like:
1 0 John Smith
2 1 John Doe
4 2 Ian Bond
5 2 James Fleming
3 1 Jane Williams
I would like a solution that does not involve building up strings to facilitate sorting. If a solution is not possible, I would like to know why.
;WITH CDE AS
(
SELECT
*,
0 AS Level,
convert(nvarchar(50),id) as EPath
FROM
collaboration.Employee AS E
WHERE
Id = #EmployeeId
UNION ALL
SELECT
E.*,
CDE.Level + 1 AS Level ,
convert(nvarchar(50),Epath+'/'+CONVERT(nvarchar(5),e.id))
FROM
collaboration.Employee AS E
INNER JOIN
CDE ON E.ParentId = CDE.Id AND E.Id <> 0
)
SELECT DISTINCT
CDE.*
FROM
CDE
ORDER BY
EPath
By the way, SQL Server 2008 has a HierarchyID data type for just this kind of thing.