How can I transpose a SQL Server row of data to multiple columns? I have data something like show below
[STOCK NO] [PN1] [PN2] [PN3] [PN4] [PN4] [PN5] [PN6] [PN7] [PN8] [PN9] [PN10] [PN11] [PN12] [Qty]
700349L 600446 201743 100208 300219 400533 100280 100218 100222 100220 100221 1
I would like to see something like this
[STOCK NO] [bomRev] [bomEntry] [partId] [qty] [cmnt] [srcLoc] [dType] [lead] [lineNbr]
700349L A 1 600446 1 `TEST` TEST 0 0 1
700349L A 2 201743 1 `TEST` TEST 0 0 2
700349L A 3 100208 1 `TEST` TEST 0 0 3
700349L A 4 300219 1 `TEST` TEST 0 0 4
700349L A 5 400533 1 `TEST` TEST 0 0 5
700349L A 6 100218 1 `TEST` TEST 0 0 6
700349L A 7 100222 1 `TEST` TEST 0 0 7
700349L A 8 100220 1 `TEST` TEST 0 0 8
700349L A 9 100221 1 `TEST` TEST 0 0 9
Then I want to use insert from select statement as shown below but I like to transpose the row to multiple columns
INSERT INTO [DT]([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt],[srcLoc], [dType], [lead], [lineNbr])
SELECT
[STOCK NO], 'A', [bomEntry], [partid], [qty], 'TEST', 'TEST', '0', '0', [lineNbr]
FROM
[ST]
This uses the columns and example data prior to additional edits in the question.
using cross apply() to unpivot your columns and row_number() to number only those rows that have a value:
--INSERT INTO [DT]([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt],[srcLoc], [dType], [lead], [lineNbr])
select
st.[CALC STOCK NO]
, bomRev = 'A'
, bomEntry = row_number() over (order by u.ordinal)
, u.Partid
, st.Qty
, cmnt = 'Test'
, srcLoc = 'Test'
, dType = '0'
, lead = '0'
, lineNbr = row_number() over (order by u.ordinal)
from st
cross apply (values
(1,[BOM WHEEL PN])
,(2,[BOM TIRE PN])
,(3,[BOM VALVE PN])
,(4,[BOM - SECONDARY DISC PN])
,(5,[BOM - DISC])
,(6,[BOM - RIM])
,(7,[BUY WHEEL])
,(8,[COLOR PN])
,(9,[WHL BOM PART 1 PN])
,(10,[WHL BOM PART 2 PN])
,(11,[WHL BOM PART 3 PN])
,(12,[WHL BOM PART 4 PN])
,(13,[WHL BOM PART 5 PN])
) u (ordinal, partId)
where nullif(u.partId,'') is not null
rextester demo: http://rextester.com/XNGB57562
returns:
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+
| CALC STOCK NO | bomRev | bomEntry | Partid | Qty | cmnt | srcLoc | dType | lead | lineNbr |
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+
| 700349L | A | 1 | 600446 | 1 | Test | Test | 0 | 0 | 1 |
| 700349L | A | 2 | 201743 | 1 | Test | Test | 0 | 0 | 2 |
| 700349L | A | 3 | 100208 | 1 | Test | Test | 0 | 0 | 3 |
| 700349L | A | 4 | 300219 | 1 | Test | Test | 0 | 0 | 4 |
| 700349L | A | 5 | 400533 | 1 | Test | Test | 0 | 0 | 5 |
| 700349L | A | 6 | 100280 | 1 | Test | Test | 0 | 0 | 6 |
| 700349L | A | 7 | 100218 | 1 | Test | Test | 0 | 0 | 7 |
| 700349L | A | 8 | 100222 | 1 | Test | Test | 0 | 0 | 8 |
| 700349L | A | 9 | 100220 | 1 | Test | Test | 0 | 0 | 9 |
| 700349L | A | 10 | 100221 | 1 | Test | Test | 0 | 0 | 10 |
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+
Pivot should work for you.
Check out this link: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
If you post enough dummy data someone would be able to help you along with the query. You can utilize something like http://rextester.com/ to share dummy data.
Good Luck!
Related
I want to flag only the first duplicate ID-VL combination in the dataset shown below. Column FirstOccurence is what I want the end result to be.
ID VL FirstOccurence
1 a 1
1 b 1
2 a 1
2 a 0
3 a 1
3 a 0
4 a 1
4 a 0
5 a 1
5 b 1
5 a 0
There is currently not a unique index available in the original table.
Is there any way to do this with for instance the LAG-functionality? I cannot find any examples online that result in the flagging of duplicates. Any suggestions are much appreciated!
Kind regards,
Igor
One method is with ROW_NUMBER() along with a CASE expression:
SELECT
ID
,VL
,CASE ROW_NUMBER() OVER(PARTITION BY ID, VL ORDER BY ID, VL) WHEN 1 THEN 1 ELSE 0 END AS FirstOccurance
FROM dbo.example
ORDER BY
ID
,VL
,FirstOccurance;
Results:
+----+----+----------------+
| ID | VL | FirstOccurance |
+----+----+----------------+
| 1 | a | 1 |
| 1 | b | 1 |
| 2 | a | 0 |
| 2 | a | 1 |
| 3 | a | 0 |
| 3 | a | 1 |
| 4 | a | 0 |
| 4 | a | 1 |
| 5 | a | 0 |
| 5 | a | 1 |
| 5 | b | 1 |
+----+----+----------------+
Note that this result order differs from your end result. If there are one or more columns present in the table that provide the same ordering as the results in you question, specify that in the ORDER BY clause instead.
I have a table
/---------------------------------------\
|Region | Product | 1 | 2 | 3 | 4 |
|-------|---------|---|-----|-----|-----|
| A | ABC | 0 | 120 | 421 | 520 |
| B | ABC | 0 | 0 | 0 | 670 |
| C | DEF | 0 | 0 | 0 | 125 |
| D | PQR | 0 | 0 | 780 | 560 |
| E | PQR | 0 | 0 | 0 | 340 |
| F | XYZ | 0 | 0 | 0 | 780 |
| G | XYZ | 0 | 0 | 0 | 900 |
\---------------------------------------/
In this table, I need to find the name of products that were launched in quarter 4.
The result that query should give is DEF and XYZ
I will be grateful if someone could help
You need to group by product and aggregate (sum) the values for each quarter per product, regardless of region:
select
Product
from #table
group by Product
having sum([4]) > 0
and sum([3]) = 0
and sum([2]) = 0
and sum([1]) = 0
With sample data to illustrate:
create table #table
(
Region varchar(1),
Product varchar(3),
[1] int,
[2] int,
[3] int,
[4] int
)
insert into #table
values
('A','ABC',0,120,421,520),
('B','ABC',0,0,0,670),
('C','DEF',0,0,0,125),
('D','PQR',0,0,780,560),
('E','PQR',0,0,0,340),
('F','XYZ',0,0,0,780),
('G','XYZ',0,0,0,900)
select
Product
from #table
group by Product
having sum([4]) > 0
and sum([3]) = 0
and sum([2]) = 0
and sum([1]) = 0
drop table #table
Output:
/---------\
| Product |
|---------|
| DEF |
| XYZ |
\---------/
try this
select *
from yourTableName a
where a.field4 > 0
and a.field3 = 0
and a.field2 = 0
and a.field1 = 0
and a.product not in (select b.product
from yourTableName b
where b.field3 >0
or b.field2>0
or b.field1>0)
and if you just want the product use below
select a.product
from yourTableName a
where a.field4 > 0
and a.field3 = 0
and a.field2 = 0
and a.field1 = 0
and a.product not in (select b.product
from yourTableName b
where b.field3 >0
or b.field2>0
or b.field1>0)
here field4 as quarter 4
field3 as quarter 3 and so on.
I have a table with these data
+------------+----------------+------------+
| Department | ProgressStatus | TasksCount |
+------------+----------------+------------+
| A | Completed | 1 |
| C | Completed | 4 |
| D | Completed | 1 |
| B | Pending | 8 |
| A | Pending | 10 |
| C | Pending | 12 |
| D | Pending | 2 |
| C | Progress | 4 |
+------------+----------------+------------+
I need to write a query to get these outputs (It looks like a simple pivot table).
+-------------+-----------+---------+----------+--------------+
| Departments | Completed | Pending | Progress | Total Tasks |
+-------------+-----------+---------+----------+--------------+
| A | 1 | 10 | 0 | 11 |
| B | 0 | 8 | | 8 |
| C | 4 | 12 | 4 | 20 |
| D | 1 | 2 | | 3 |
+-------------+-----------+---------+----------+--------------+
Using conditional SUM and GROUP BY
select
department,
sum(case when ProgressStatus = 'Completed' then TasksCount end) Completed,
sum(case when ProgressStatus = 'Pending' then TasksCount end) Pending,
sum(case when ProgressStatus = 'Progress' then TasksCount end) Progress,
sum(TasksCount) Total
from your_table
group by department;
BY using pivot i tried like this
SELECT Department,isnull(Completed,0) Completed,isnull([Pending],0) [Pending],isnull([Progress],0) [Progress]
,isnull(Completed,0)+isnull([Pending],0)+isnull([Progress],0) as 'total'
FROM #Table2
PIVOT ( sum([TasksCount])
for [ProgressStatus] in ([Completed], [Pending], [Progress])) AS pvt
output
Department Completed Pending Progress total
A 1 10 0 11
B 0 8 0 8
C 4 12 4 20
D 1 2 0 3
I have two tables
1) Document: which represent a document
+----+----------+------+
| ID | Body | Type |
+----+----------+------+
| 1 | Ramesh | 1 |
| 2 | Khilan | 1 |
| 3 | kaushik | 4 |
| 4 | Chaitali | 2 |
| 5 | Hardik | 2 |
+----+----------+------+
2) Destination: which represent a party of the document
+--------+------------+--------+
| UserId | DocumentId | Status |
+--------+------------+--------+
| 6 | 3 | 4 |
| 4 | 5 | 5 |
| 89 | 2 | 0 |
| 15 | 4 | 3 |
| 89 | 1 | 0 |
+--------+------------+--------+
The status column represent a folder for the user, i want to get the count for each type for each folder, even if the folder is empty for a specifi user,
however if want them in this from,
+--------+--------+--------------+--------------+--------------+
| UserId | Status | Type 1 Count | Type 2 Count | Type 4 Count |
+--------+--------+--------------+--------------+--------------+
| 89 | 0 | 2 | 0 | 0 |
| 89 | 3 | 0 | 0 | 0 |
| 89 | 4 | 0 | 0 | 0 |
| 89 | 5 | 0 | 0 | 0 |
+--------+--------+--------------+--------------+--------------+
the issue I'm facing is I can't find a way to get the types the user does not have by join, i can get them using CASE but not in the form i want
my query is:
`SELECT dd.[Status],
SUM(CASE WHEN d.[Type] = 1 THEN 1 ELSE 0 END) AS 'Type1Count'
SUM(CASE WHEN d.[Type] = 2 THEN 1 ELSE 0 END) AS 'Type2Count'
SUM(CASE WHEN d.[Type] = 4 THEN 1 ELSE 0 END) AS 'Type4Count'
FROM [User] u LEFT JOIN [Destination] dd ON u.[Id] = dd.[UserId]
LEFT JOIN [Document] d ON dd.[DocumentId] = d.[Id]
WHERE u.[Id] = #UserId`
the result is
+--------+--------+--------------+--------------+--------------+
| UserId | Status | Type 1 Count | Type 2 Count | Type 4 Count |
+--------+--------+--------------+--------------+--------------+
| 89 | 0 | 2 | 0 | 0 |
+--------+--------+--------------+--------------+--------------+
So join all users onto a table of all statuses (I have named this Folder as per you description in the question) before you then join to Document and Destination:
SELECT u.UserId, st.Status,
SUM(CASE WHEN doc.Type = 1 THEN 1 ELSE 0 END) AS [Type 1 Count],
SUM(CASE WHEN doc.Type = 2 THEN 1 ELSE 0 END) AS [Type 2 Count],
SUM(CASE WHEN doc.Type = 4 THEN 1 ELSE 0 END) AS [Type 4 Count]
FROM User u
CROSS JOIN Folder st
LEFT OUTER JOIN Destination d
ON d.UserId = u.UserId
AND d.Status = st.Status
LEFT OUTER JOIN Document doc
ON doc.ID = d.DocumentId
GROUP BY u.UserId, st.Status
ORDER BY u.UserId
I have an exported table from accounting software like below.
AccountID AccountName
--------- -----------
11 Acc11
12 Acc12
13 Acc13
11/11 Acc11/11
11/12 Acc11/12
11/111 Acc11/111
11/11/001 Acc11/11/001
11/11/002 Acc11/11/002
12/111 Acc12/111
12/112 Acc12/112
I want to convert it to tree query in MS-SQL Server 2008 to use it as a Treelist datasource in my win aaplication.
I raised this question before and it's answered with a way that it was very very slow for my big table with more than 5000 records (Create Tree Query From Numeric Mapping Table in SQL). But I think counting "/" and separating AccountID field with "/" can solve my problem easier and very faster.
Anyway, My expected result must be like below:
AccountID AccountName ID ParentID Level HasChild
--------- ----------- --- --------- ------ --------
11 Acc11 1 Null 1 1
12 Acc12 2 Null 1 1
13 Acc13 3 Null 1 0
11/11 Acc11/11 4 1 2 1
11/12 Acc11/12 5 1 2 0
11/111 Acc11/111 6 1 2 0
11/11/001 Acc11/11/001 7 4 3 0
11/11/002 Acc11/11/002 8 4 3 0
12/111 Acc12/111 9 2 2 0
12/112 Acc12/112 10 2 2 0
Please Help Me.
I modified my answer given in the first question...
It would be best, if your table would keep the relation data directly in indexed columns. Before you change your table's structure you might try this:
A table with test data
DECLARE #tbl TABLE ( AccountID VARCHAR(100), AccountName VARCHAR(100));
INSERT INTO #tbl VALUES
('11','Acc11')
,('12','Acc12')
,('13','Acc13')
,('11/11','Acc11/11')
,('11/12','Acc11/12')
,('11/111','Acc11/111')
,('11/11/001','Acc11/11/001')
,('11/11/002','Acc11/11/002')
,('12/111','Acc12/111')
,('12/112','Acc12/112');
This will get the needed data into a newly created temp table called #tempHierarchy
SELECT AccountID
,AccountName
,ROW_NUMBER() OVER(ORDER BY LEN(AccountID)-LEN(REPLACE(AccountID,'/','')),AccountID) AS ID
,Extended.HierarchyLevel
,STUFF(
(
SELECT '/' + A.B.value('.','varchar(10)')
FROM Extended.IDsXML.nodes('/x[position() <= sql:column("HierarchyLevel")]') AS A(B)
FOR XML PATH('')
),1,2,'') AS ParentPath
,Extended.IDsXML.value('/x[sql:column("HierarchyLevel")+1][1]','varchar(10)') AS ownID
,Extended.IDsXML.value('/x[sql:column("HierarchyLevel")][1]','varchar(10)') AS ancestorID
INTO #tempHierarchy
FROM #tbl
CROSS APPLY(SELECT LEN(AccountID)-LEN(REPLACE(AccountID,'/','')) + 1 AS HierarchyLevel
,CAST('<x></x><x>' + REPLACE(AccountID,'/','</x><x>') + '</x>' AS XML) AS IDsXML) AS Extended
;
The intermediate result
+-----------+--------------+----+----------------+------------+-------+------------+
| AccountID | AccountName | ID | HierarchyLevel | ParentPath | ownID | ancestorID |
+-----------+--------------+----+----------------+------------+-------+------------+
| 11 | Acc11 | 1 | 1 | | 11 | |
+-----------+--------------+----+----------------+------------+-------+------------+
| 12 | Acc12 | 2 | 1 | | 12 | |
+-----------+--------------+----+----------------+------------+-------+------------+
| 13 | Acc13 | 3 | 1 | | 13 | |
+-----------+--------------+----+----------------+------------+-------+------------+
| 11/11 | Acc11/11 | 4 | 2 | 11 | 11 | 11 |
+-----------+--------------+----+----------------+------------+-------+------------+
| 11/111 | Acc11/111 | 5 | 2 | 11 | 111 | 11 |
+-----------+--------------+----+----------------+------------+-------+------------+
| 11/12 | Acc11/12 | 6 | 2 | 11 | 12 | 11 |
+-----------+--------------+----+----------------+------------+-------+------------+
| 12/111 | Acc12/111 | 7 | 2 | 12 | 111 | 12 |
+-----------+--------------+----+----------------+------------+-------+------------+
| 12/112 | Acc12/112 | 8 | 2 | 12 | 112 | 12 |
+-----------+--------------+----+----------------+------------+-------+------------+
| 11/11/001 | Acc11/11/001 | 9 | 3 | 11/11 | 001 | 11 |
+-----------+--------------+----+----------------+------------+-------+------------+
| 11/11/002 | Acc11/11/002 | 10 | 3 | 11/11 | 002 | 11 |
+-----------+--------------+----+----------------+------------+-------+------------+
And now a similar recursive approach takes place as in my first answer. But - as it is using a real table now and all the string splitting has taken place already - it should be faster...
WITH RecursiveCTE AS
(
SELECT th.*
,CAST(NULL AS BIGINT) AS ParentID
,CASE WHEN EXISTS(SELECT 1 FROM #tempHierarchy AS x WHERE x.ParentPath=th.AccountID) THEN 1 ELSE 0 END AS HasChild
FROM #tempHierarchy AS th WHERE th.HierarchyLevel=1
UNION ALL
SELECT sa.AccountID
,sa.AccountName
,sa.ID
,sa.HierarchyLevel
,sa.ParentPath
,sa.ownID
,sa.ancestorID
,(SELECT x.ID FROM #tempHierarchy AS x WHERE x.AccountID=sa.ParentPath)
,CASE WHEN EXISTS(SELECT 1 FROM #tempHierarchy AS x WHERE x.ParentPath=sa.AccountID) THEN 1 ELSE 0 END AS HasChild
FROM RecursiveCTE AS r
INNER JOIN #tempHierarchy AS sa ON sa.HierarchyLevel=r.HierarchyLevel+1
AND r.AccountID=sa.ParentPath
)
SELECT r.AccountID
,r.AccountName
,r.ID
,r.ParentID
,r.HierarchyLevel
,r.HasChild
FROM RecursiveCTE AS r
ORDER BY HierarchyLevel,ParentID;
And finally I clean up
DROP TABLE #tempHierarchy;
And here's the final result
+-----------+--------------+----+----------+----------------+----------+
| AccountID | AccountName | ID | ParentID | HierarchyLevel | HasChild |
+-----------+--------------+----+----------+----------------+----------+
| 11 | Acc11 | 1 | NULL | 1 | 1 |
+-----------+--------------+----+----------+----------------+----------+
| 12 | Acc12 | 2 | NULL | 1 | 1 |
+-----------+--------------+----+----------+----------------+----------+
| 13 | Acc13 | 3 | NULL | 1 | 0 |
+-----------+--------------+----+----------+----------------+----------+
| 11/11 | Acc11/11 | 4 | 1 | 2 | 1 |
+-----------+--------------+----+----------+----------------+----------+
| 11/111 | Acc11/111 | 5 | 1 | 2 | 0 |
+-----------+--------------+----+----------+----------------+----------+
| 11/12 | Acc11/12 | 6 | 1 | 2 | 0 |
+-----------+--------------+----+----------+----------------+----------+
| 12/111 | Acc12/111 | 7 | 2 | 2 | 0 |
+-----------+--------------+----+----------+----------------+----------+
| 12/112 | Acc12/112 | 8 | 2 | 2 | 0 |
+-----------+--------------+----+----------+----------------+----------+
| 11/11/001 | Acc11/11/001 | 9 | 4 | 3 | 0 |
+-----------+--------------+----+----------+----------------+----------+
| 11/11/002 | Acc11/11/002 | 10 | 4 | 3 | 0 |
+-----------+--------------+----+----------+----------------+----------+