I have a table with basic employee details as below:
Table: tblEmployees
EmpID Name Contact Sex
100 John 55555 M
200 Kate 44444 F
300 Sam 88888 M
I would like to get my query result as follows of a particular employee where EmpID = 200
Col1 Col2
EmpID 200
Name Kate
Sex F
You can use cross apply:
select t.*
from employees e
cross apply (values
('empid', cast(empid as varchar(100))),
('name', name),
('sex', sex)
) t(attr, value)
where e.empid = 200
Presumably, empid is a number, so explicit casting is needed (otherwise sql server will try to cast the name and sex to numbers, which will fail).
Demo on DB Fiddle:
attr | value
:---- | :----
empid | 200
name | Kate
sex | F
Or a less sophisticated solution involving 3 UNIONs, assuming the field names are predetermined in advance. This might perform better on large tables.
If you have performance issues, analyze the execution plan and make sure indexes are utilized optimally.
Since you are only looking for one particular employee at a time:
SELECT 'empid', convert(varchar(12), EmpID)
FROM tblEmployees
WHERE EmpID = 200
UNION ALL
SELECT 'name', name
FROM tblEmployees
WHERE EmpID = 200
UNION ALL
SELECT 'sex', sex
FROM tblEmployees
WHERE EmpID = 200
The first line does convert(varchar(12) under the assumption that EmpID is an int field.
Another option is with a little XML
Full Disclosure: Not as performant as GMB's CROSS APPLY (+1) or UNPIVOT. BUT it will dynamically unpivot virtually any row, table, view or ad-hoc query without actually using dynamic SQL.
Example
Declare #YourTable Table ([EmpID] varchar(50),[Name] varchar(50),[Contact] varchar(50),[Sex] varchar(50)) Insert Into #YourTable Values
(100,'John',55555,'M')
,(200,'Kate',44444,'F')
,(300,'Sam',88888,'M')
Select A.EmpID
,C.*
From #YourTable A
Cross Apply ( values (convert(xml,(select a.* for XML Raw ))) ) B(XMLData)
Cross Apply (
Select Item = xAttr.value('local-name(.)', 'varchar(100)')
,Value = xAttr.value('.','varchar(max)')
From XMLData.nodes('//#*') xNode(xAttr)
Where xAttr.value('local-name(.)', 'varchar(100)') not in ('EmpID','Other','Columns2Exclude')
) C
Returns
EmpID Item Value
100 Name John
100 Contact 55555
100 Sex M
200 Name Kate
200 Contact 44444
200 Sex F
300 Name Sam
300 Contact 88888
300 Sex M
EDIT - If Interested Here a TVF approach
Select A.EmpID
,B.*
From #YourTable A
Cross Apply [dbo].[tvf-XML-UnPivot-Row]((Select A.* for XML RAW)) B
The TVF
CREATE FUNCTION [dbo].[tvf-XML-UnPivot-Row](#XML xml)
Returns Table
As
Return (
Select Item = xAttr.value('local-name(.)', 'varchar(100)')
,Value = xAttr.value('.','varchar(max)')
From #XML.nodes('//#*') xNode(xAttr)
)
Related
How can I display each column in separate row and at the end add additional field.
For example I have this result:
ID ArticleName Brend1 Brend2 Brend3
== =========== ======== ======== ========
1 TestArticle 10001 20002 30003
I want to achieve this:
ID ArticleName BrandNo BrandName
== =========== ======= =========
1 TestArticle 10001 if column name = Brand1 Then Nike
1 TestArticle 20002 if column name = Brand2 Then Adidas
1 TestArticle 30003 if column name = Brand3 Then Mercedes
I can show each column in separate row, but how can I add additional column to the end of the result BrandName
Here is what I've done:
DECLARE #temTable TABLE
(
Id INT,
ArticleName VARCHAR(20),
Brand1 VARCHAR(20),
Brand2 VARCHAR(20),
Brand3 VARCHAR(20)
);
INSERT INTO #temTable
(
Id,
ArticleName,
Brand1,
Brand2,
Brand3
)
VALUES
(1, 'TestArticle', '10001', '20002', '30003');
SELECT Id,
ArticleName,
b.*
FROM #temTable a
CROSS APPLY
(
VALUES
(Brand1),
(Brand2),
(Brand3)
) b (Brand)
WHERE b.Brand IS NOT NULL;
You could use CROSS APPLY as
SELECT Id, ArticleName, Br BrandNo, Val BrandName
FROM #TemTable TT
CROSS APPLY(
VALUES
(Brand1, 'Nike'),
(Brand2, 'Adidas'),
(Brand3, 'Mercedes')
) T(Br, Val)
db-fiddle
I assume the brand is stored in another table, so you just need to add another column in your VALUES operator, and then join to the Brand Table:
SELECT Id,
ArticleName,
V.Brand
FROM #temTable a
CROSS APPLY (VALUES (1,Brand1),
(2,Brand2),
(3,Brand3)) V (BrandID,Brand)
JOIN dbo.Brand B ON V.BrandID = B.BrandID
WHERE V.Brand IS NOT NULL;
You can use UNPIVOT to achieve this. You can use either a case statement or another table variable to switch column names with brand names, I would prefer a table variable with a join it would make adding new column a bit easier.
DECLARE #d TABLE (ColNames VARCHAR(128) , BrandName VARCHAR(100))
INSERT INTO #d VALUES ('Brand1', 'Nike'),('Brand2', 'Adidas'),('Brand3', 'Mercedes')
SELECT up.Id
, up.ArticleName
, up.BrandNo
, d.BrandName
FROM #temTable
UNPIVOT (BrandNo FOR ColNames IN (Brand1,Brand2,Brand3)) up
INNER JOIN #d d ON d.ColNames = up.ColNames
If I have the following rows in a table:
clientId settings
1 {"franchises":[1,7,9,11,14,20,23,26,27,29,33,34,35,45,49,50,57,58,72,73]}
2 {"franchises":[1]}
3 {"franchises":[50]}
How would I query that to pull clientIds who have the franchises 1 or 50?
I tried doing
SELECT clientId FROM clientSettings WHERE JSON_VALUE(settings, '$.franchises') IN (1,50)
but that didn't work.
You can use json functions:
select distinct c.clientId
from clientSettings c
cross apply openjson(c.settings, '$.franchises') with (franchise integer '$')
where franchise in (1, 50);
Wrapping this in exists might perform better, since it avoids the need for aggregation:
select c.clientId
from clientSettings c
where exists (
select 1
from openjson(c.settings, '$.franchises') with (franchise integer '$')
where franchise in (1, 50)
)
Demo on DB Fiddle (I added a non-matching line to your dataset, with id 4):
| clientId |
| -------: |
| 1 |
| 2 |
| 3 |
Another way that requires no sorting or aggregating.
DECLARE #clientSettings TABLE (ClientId INT IDENTITY, Settings VARCHAR(8000));
INSERT #clientSettings(settings)
VALUES
('{"franchises":[0,7,9,11,14,20,23,26,27,29,33,34,35,45,49,55,57,58,72,73,1,50]}'),
('{"franchises":[1]}'),
('{"franchises":[50]}');
SELECT c.clientId
FROM #clientSettings c
CROSS APPLY
(
SELECT TOP (1) f.franchise
FROM OPENJSON(c.settings, '$.franchises') WITH (franchise INT '$') AS f
WHERE f.franchise IN (1, 50)
) AS x;
Please, try with below query using "CROSS APPLY OPENJSON"
(SQL-FIDDLE):
SELECT c.clientId
FROM clientSettings c
CROSS APPLY OPENJSON(c.settings, '$.franchises') with (franchise integer '$') as f
WHERE f.franchise in (1, 50)
GROUP BY c.clientId;
I want to convert a row in sql table to multiple rows in other table.
example: say if i'm having a table 'UserDetail' and he has 2 address's (home, office...) then the table looks like...
I wand the result to be in the second table as shown in the image
We can use Cross Apply
;WITH CTE(UseriD,Address1Line,Address1City,Address1State,Address2Line,Address2City,Address2State )
AS
(
SELECT 1,'Line1','City1','State1','Line2','City2','State2'
)
SELECT UseriD,[Address],City,[State]
FROM CTE
CROSS APPLY ( VALUES (Address1Line,Address1City,Address1State ),
(Address2Line,Address2City,Address2State )
)AS Dt([Address],City,[State])
Result
UseriD Address City State
-----------------------------
1 Line1 City1 State1
1 Line2 City2 State2
Demo:http://rextester.com/KHFUM28227
You could use "union all" to do that like:
select * into newTable
from
(
select UserId, Address1Line as Address, Address1City as City, Address1State as State
from myTable
union all
select UserId, Address2Line as Address, Address2City as City, Address2State as State
from myTable
) tmp
If you use just UNION instead of UNION ALL you would also be removing the duplicates where Address1 and Address2 is same.
You can CROSS JOIN or CROSS APPLY the table to a list of numbers.
Then use IIF or CASE to get the correspondent numbered fields.
Or CROSS APPLY on the address values directly on the table.
Example snippet:
declare #UserTable table (UserId int, Address1Line varchar(30), Address1City varchar(30), Address1State varchar(30), Address2Line varchar(30), Address2City varchar(30), Address2State varchar(30));
insert into #UserTable (UserId, Address1Line, Address1City, Address1State, Address2Line, Address2City, Address2State) values
(1,'Wonder Lane 42','WonderTown', 'WonderState', 'Somewhere 1 B', 'Nowhere', 'Anywhere'),
(2,'Backstreet 69','Los Libros', 'Everland', 'Immortal Cave 777', 'Ghost City', 'The Wild Lands');
-- Cross Join on numbers
select UserId,
case n when 1 then Address1Line when 2 then Address2Line end as [Address],
case n when 1 then Address1City when 2 then Address2City end as [City],
case n when 1 then Address1State when 2 then Address2State end as [State]
from #UserTable u
cross join (values (1),(2)) as nums(n);
-- Cross Apply on Adress values
select UserId, [Address], [City], [State]
from #UserTable Usr
cross apply (values
(1, Address1Line, Address1City, Address1State),
(2, Address2Line, Address2City, Address2State)
) AS Addr(n, [Address], [City], [State]);
Both return:
UserId Address City State
------ ----------------- ---------- --------------
1 Wonder Lane 42 WonderTown WonderState
1 Somewhere 1 B Nowhere Anywhere
2 Backstreet 69 Los Libros Everland
2 Immortal Cave 777 Ghost City The Wild Lands
I have a SQL statement that is the following:
SELECT A.ID, A.Name
FROM Properties A
WHERE A.ID IN (110, 105, 104, 106)
When I run this SQL statement, the output is ordered according to the IN list by ID automatically and returns
104 West
105 East
106 North
110 South
I want to know if it is possible to order by the order the parameters are listed within the IN clause. so it would return
110 South
105 East
104 West
106 North
I think the easiest way in SQL Server is to use a JOIN with VALUES:
SELECT p.ID, p.Name
FROM Properties p JOIN
(VALUES (110, 1), (105, 2), (104, 3), (106, 4)) ids(id, ordering)
ON p.id = a.id
ORDER BY ids.ordering;
Sure...
just add an Order clause with a case in it
SELECT A.ID, A.Name
FROM Properties A
WHERE A.ID IN (110,105,104,106)
Order By case A.ID
when 110 then 0
when 105 then 1
when 104 then 2
when 106 then 3 end
With the help of a parsing function which returns the sequence as well
SELECT B.Key_PS
, A.ID
, A.Name
FROM Properties A
Join (Select * from [dbo].[udf-Str-Parse]('110,105,104,106',',')) B on A.ID=B.Key_Value
WHERE A.ID IN (110,105,104,106)
Order by Key_PS
The UDF if you need
CREATE FUNCTION [dbo].[udf-Str-Parse] (#String varchar(max),#Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
-- Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
-- Select * from [dbo].[udf-Str-Parse]('id26,id46|id658,id967','|')
-- Select * from [dbo].[udf-Str-Parse]('hello world. It. is. . raining.today','.')
Returns #ReturnTable Table (Key_PS int IDENTITY(1,1), Key_Value varchar(max))
As
Begin
Declare #XML xml;Set #XML = Cast('<x>' + Replace(#String,#Delimeter,'</x><x>')+'</x>' as XML)
Insert Into #ReturnTable Select Key_Value = ltrim(rtrim(String.value('.', 'varchar(max)'))) FROM #XML.nodes('x') as T(String)
Return
End
The Parser alone would return
Select * from [dbo].[udf-Str-Parse]('110,105,104,106',',')
Key_PS Key_Value
1 110
2 105
3 104
4 106
What you could potentially do is:
Create a TVF that would split string and keep original order.
This questions seems to have this function already written: MS SQL: Select from a split string and retain the original order (keep in mind that there might be other approaches, not only those, covered in this question, I just gave it as an example to understand what function should do)
So now if you'd run this query:
SELECT *
FROM dbo.Split('110,105,104,106', ',') AS T;
It would bring back this table as a result.
items rownum
------------
110 1
105 2
104 3
106 4
Following that, you could simply query your table, join with this TVF passing your IDs as a parameter:
SELECT P.ID, P.Name
FROM Properties AS P
INNER JOIN dbo.Split('110,105,104,106', ',') AS T
ON T.items = P.ID
ORDER BY T.rownum;
This should retain order of parameters.
If you need better performance, I'd advice to put records from TVF into hash table, index it and then join with actual table. See query below:
SELECT T.items AS ID, T.rownum AS SortOrder
INTO #Temporary
FROM dbo.Split('110,105,104,106', ',') AS T;
CREATE CLUSTERED INDEX idx_Temporary_ID
ON #Temporary(ID);
SELECT P.ID, P.Name
FROM Properties AS P
INNER JOIN #Temporary AS T
ON T.ID = P.ID
ORDER BY T.SortOrder;
This should work better on larger data sets and equally well on small ones.
Here is a solution that does not rely on hard codes values or dynamic sql (to eliminate hard coding values).
I would build a table (maybe temp or variable) with OrderByValue and OrderBySort and insert from the application.
OrderByValue OrderBySort
110 1
105 2
104 3
106 4
Then I would join on the value and sort by the sort. The join will be the same as the In clause.
SELECT A.ID, A.Name
FROM Properties A
JOIN TempTable B On A.ID = B.OrderByValue
Order By B.OrderBySort
Another solution for this problem is prepare a temporary table for IN clause like
declare #InTable table (ID int, SortOrder int not null identity(1,1));
We can fill this temp table with your data in order you want
insert into #InTable values (110), (105), (104), (106);
At last we need to modify your question to use this temp table like this
select A.ID, A.Name
from Properties A
inner join #InTable as Sort on A.ID = Sort.ID
order by Sort.SortOrder
On the output you can see this
ID Name
110 South
105 East
104 West
106 North
In this solution you don't need to provide order in special way. You just need to insert values in order you want.
I try to get all top managers of the user.
For example.
Dan king(Specialist) --> Adam Lion(Product Manager) --> Reina Ken (Head of IT) --> Michael Lorren (Director)--> Pat Deep (CEO)
I want to show top managers of Dan King on one column the below:
Adam
Reina
Michael
Pat
NOTE:
Get all employees under manager with CTE
I used this link and I decided to take users of manager.
I tried to convert my code according the sample code but it didn't work:
;WITH OS_MANAGERS AS
(
SELECT USERID, MANAGERUSERID
from OSMANAGERS
), Manager AS
(
SELECT USERID, MANAGERUSERID
from OS_MANAGERS
where [userID]='mbalasubrama'
union all
SELECT B.USERID, B.MANAGERUSERID
from Manager A
inner join OS_MANAGERS B ON A.MANAGERUSERID= B.USERID
)
select A.USERID , a.MANAGERUSERID
from OS_MANAGERS A
left join Manager B on A.USERID=B.USERID
where A.[userID]='mbalasubrama'
only I see the manager of user, other top ones don't come
RESULT:
Your code seems a bit too complicated... Try this:
DECLARE #tbl TABLE(USERID INT,MANAGERUSERID INT,Name VARCHAR(100));
INSERT INTO #tbl VALUES
(1,NULL,'CEO Pat')
,(2,1,'Director Michael')
,(3,2,'HIT Reina')
,(4,3,'PM Adam')
,(5,4,'Dan King');
WITH ManagerRecursive AS
(
SELECT USERID, MANAGERUSERID,Name
from #tbl
where [userID]=5
union all
SELECT tbl.USERID, tbl.MANAGERUSERID,tbl.Name
from ManagerRecursive AS MR
INNER JOIN #tbl AS tbl ON tbl.USERID=MR.MANAGERUSERID
)
SELECT * FROM ManagerRecursive ;
The result
USERID MANAGERUSERID Name
5 4 Dan King
4 3 PM Adam
3 2 HIT Reina
2 1 Director Michael
1 NULL CEO Pat
You can see my code:
WITH ManagerRecursive AS
(
SELECT [USERID],[MANAGERUSERID]
FROM [EBA].[dbo].[OSMANAGERS]
where [userID]='dking'
union all
SELECT tbl.USERID, tbl.MANAGERUSERID
from ManagerRecursive AS MR
INNER JOIN [EBA].[dbo].[OSMANAGERS] AS tbl ON tbl.USERID=MR.MANAGERUSERID
)
SELECT * FROM ManagerRecursive ;
Result:
Userid manageruserid
dking tkir
tkir skara