I'm looking for a query were i can derive a new column based on columns values as shown below in example
P X Y Z A B C
1 1.1 2.1 1.3 1 Null 3
2 Null 1.4 3.1 2 4.7 1
3 2.2 Null 4.6 4 3.5 1
4 Null 1.8 3.4 2 1.7 4
Which i want to show as shown below;
P Group X y Z A B C
1 Xgrp 1.1 - - - - -
1 Ygrp - 2.1 - - - -
1 Zgrp 1.3 - - -
1 Agrp 1
1 Bgrp Null
1 Cgrp 3
Please help me :)
Regards
Andy
I am not sure how you are defining these dashes, so they are ignored in the code below. There is some additional logic in the ORDER BY clause in order to show the results like in your example. Basically, we are performing UNPIVOT to define the group values and then performing again PIVOT:
DECLARE #DataSource TABLE
(
[P] TINYINT
,[X] DECIMAL(9,1)
,[Y] DECIMAL(9,1)
,[Z] DECIMAL(9,1)
,[A] DECIMAL(9,1)
,[B] DECIMAL(9,1)
,[C] DECIMAL(9,1)
);
INSERT INTO #DataSource ([P], [X], [Y], [Z], [A], [B], [C])
VALUES (1, 1.1, 2.1, 1.3, 1, NULL, 3)
,(2, NULL, 1.4, 3.1, 2, 4.7, 1)
,(3, 2.2, NULL, 4.6, 4, 3.5, 1 )
,(4, NULL, 1.8, 3.4, 2, 1.7, 4);
SELECT *
FROM
(
SELECT [P]
,[value]
,[column]
,[column] + 'grp'
FROM #DataSource
UNPIVOT
(
[value] FOR [column] IN ([X], [Y], [Z], [A], [B], [C])
) UNPVT
) DS ([P], [value], [column], [group])
PIVOT
(
MAX([value]) FOR [column] IN ([X], [Y], [Z], [A], [B], [C])
) PVT
ORDER BY [P]
,CASE [group]
WHEN 'Xgrp' THEN 1
WHEN 'Ygrp' THEN 2
WHEN 'Zgrp' THEN 3
WHEN 'Agrp' THEN 4
WHEN 'Bgrp' THEN 5
WHEN 'Cgrp' THEN 6
END
What gotqn posted is missing some rows - specifically ones where all columns should be NULL (such as the "Bgrp" record where P=1).
You can get what you're looking for without PIVOT or UNPIVOT, just a simple CROSS JOIN, GROUP BY and CASE statement like so:
-- sample data
DECLARE #DataSource TABLE
(
[P] TINYINT
,[X] DECIMAL(9,1)
,[Y] DECIMAL(9,1)
,[Z] DECIMAL(9,1)
,[A] DECIMAL(9,1)
,[B] DECIMAL(9,1)
,[C] DECIMAL(9,1)
);
INSERT INTO #DataSource ([P], [X], [Y], [Z], [A], [B], [C])
VALUES (1, 1.1, 2.1, 1.3, 1, NULL, 3)
,(2, NULL, 1.4, 3.1, 2, 4.7, 1)
,(3, 2.2, NULL, 4.6, 4, 3.5, 1 )
,(4, NULL, 1.8, 3.4, 2, 1.7, 4);
-- solution
SELECT
p, [group],
X = CASE [group] WHEN 'Xgrp' THEN MAX(X) END,
Y = CASE [group] WHEN 'Ygrp' THEN MAX(Y) END,
Z = CASE [group] WHEN 'Zgrp' THEN MAX(Z) END,
A = CASE [group] WHEN 'Agrp' THEN MAX(A) END,
B = CASE [group] WHEN 'Bgrp' THEN MAX(B) END,
C = CASE [group] WHEN 'Cgrp' THEN MAX(C) END
FROM (VALUES ('Xgrp'),('Ygrp'),('Zgrp'),('Agrp'),('Bgrp'),('Cgrp')) groups([group])
CROSS JOIN #DataSource d
GROUP BY p, [group]
-- Uncomment this ORDER BY for testing:
--ORDER BY [P]
-- ,CASE [group]
-- WHEN 'Xgrp' THEN 1
-- WHEN 'Ygrp' THEN 2
-- WHEN 'Zgrp' THEN 3
-- WHEN 'Agrp' THEN 4
-- WHEN 'Bgrp' THEN 5
-- WHEN 'Cgrp' THEN 6
-- END;
Results
Related
I have a table with the following values
UserID ParentID Levels Path
1 NULL 0 A1
5 1 1 A2
9 5 2 A3
43 9 3 A4
The output should be like followed :
UserID ParentID Levels FinalPath
1 NULL 0 A1/
5 1 1 A1/A2/
9 5 2 A1/A2/A3/
43 9 3 A1/A2/A3/A4/
Thanks in advance for any guidance on this.
Solution using a recusive common table expression.
Sample data
create table users
(
userid int,
parentid int,
levels int,
path nvarchar(100)
);
insert into users (userid, parentid, levels, path) values
(1, NULL, 0, 'A1'),
(5, 1, 1, 'A2'),
(9, 5, 2, 'A3'),
(43, 9, 3, 'A4');
Solution
with cte as
(
select u.userid, u.parentid, u.levels, u.path
from users u
where u.parentid is null
union all
select u.userid, u.parentid, u.levels, convert(nvarchar(100), c.path + '/' + u.path)
from users u
join cte c
on c.userid = u.parentid
)
select c.userid, c.parentid, c.levels, c.path + '/' as FinalPath
from cte c;
Fiddle
Here's a version that both calculates the Level and appends the Path.
Data
drop table if exists dbo.test_table;
go
create table dbo.test_table(
UserID int,
ParentID int,
[Path] varchar(5));
insert dbo.test_table([UserID], [ParentID], [Path]) values
(1,null, 'A1'),
(5,1, 'A2'),
(9,5, 'A3'),
(43,9, 'A4');
Query
;with recur_cte([UserId], [ParentID], h_level, [Path]) as (
select [UserId], [ParentID], 0, cast([Path] as varchar(100))
from dbo.test_table
where [ParentID] is null
union all
select tt.[UserId], tt.[ParentID], rc.h_level+1, cast(concat(tt.[Path], '/', rc.[Path]) as varchar(100))
from dbo.test_table tt join recur_cte rc on tt.[ParentID]=rc.[UserId])
select * from recur_cte;
Results
UserId ParentID h_level Path
1 NULL 0 A1
5 1 1 A1/A2
9 5 2 A1/A2/A3
43 9 3 A1/A2/A3/A4
I am a beginner in SQL. I want to convert some columns into rows inside a view, this is my view:
SLPRSNID ITEMNMBR 1 2 3 4 5
1 01-GOGUA-010 0 500 500 500 500
4 01-GOGUA-020 1850 3850 2350 4450 2450
I need:
SLPRSNID ITEMNMBR VOLUME
1 01-GOGUA-010 2000
4 01-GOGUA-020 14950
I used:
SELECT ITEMNMBR, VOLUMEN
FROM dbo.SOPPPTOVOL
UNPIVOT
(
VOLUMEN
FOR ITEMNMBR IN ([1], [2], [3], [4], [5])
) AS UnPvt
But it doesn't work.
using APPLY
DECLARE #table table (
SLPRSNID INT ,
ITEMNMBR VARCHAR(50),
[1] FLOAT,
[2] FLOAT,
[3] FLOAT,
[4] FLOAT,
[5] FLOAT
)
INSERT INTO #table
VALUES (1, '01-GOGUA-010', 0.00,500.00,500.00,500.00,500.00),
(4, '01-GOGUA-020', 1850.0,3850.00,2350.00,4450.00,2450.00)
SELECT T.SLPRSNID,T.ITEMNMBR, P.VOLUME
FROM #table T
CROSS APPLY
(
SELECT SUM(X.VAL)
FROM
(
VALUES ([1]),([2]),([3]),([4]),([5])
) X(VAL)
) P(VOLUME)
Using UNPIVOT
DECLARE #table table (
SLPRSNID INT ,
ITEMNMBR VARCHAR(50),
[1] FLOAT,
[2] FLOAT,
[3] FLOAT,
[4] FLOAT,
[5] FLOAT
)
INSERT INTO #table
VALUES (1, '01-GOGUA-010', 0.00,500.00,500.00,500.00,500.00),
(4, '01-GOGUA-020', 1850.0,3850.00,2350.00,4450.00,2450.00)
SELECT E.SLPRSNID , E.ITEMNMBR , SUM(E.VOL) VOLUME
FROM #table T
UNPIVOT
(
VOL FOR VALS IN ([1],[2],[3],[4],[5])
) E
GROUP BY E.SLPRSNID , E.ITEMNMBR
I have a table containing four rows: id(primary key, auto increment), value, type and time.
id value type time
1 1.2 1 2017-10-26 16:16:49.350
2 12.4 2 2017-10-26 16:16:49.350
3 0.6 3 2017-10-26 16:16:49.350
4 1.1 4 2017-10-26 16:16:49.350
5 1.8 1 2017-10-25 14:12:24.650
6 3.2 2 2017-10-25 14:12:24.650
7 0.2 3 2017-10-25 14:12:24.650
8 1.2 4 2017-10-25 14:12:24.650
Is it possible to convert these rows to columns based on type and time(either by query or stored procedure)? something like this:
(type)1 2 3 4 time
1.2 12.4 0.6 1.1 2017-10-26 16:16:49.350
1.8 3.2 0.2 1.2 2017-10-25 14:12:24.650
PS: Each four types share the same time.
Try this:
DECLARE #DataSource TABLE
(
[id] SMALLINT
,[value] DECIMAL(9,1)
,[type] TINYINT
,[time] DATETIME2
);
INSERT INTO #DataSource ([id], [value], [type], [time])
VALUES (1, 1.2, 1, '2017-10-26 16:16:49.350')
,(2, 12.4, 2, '2017-10-26 16:16:49.350')
,(3, 0.6, 3, '2017-10-26 16:16:49.350')
,(4, 1.1, 4, '2017-10-26 16:16:49.350')
,(5, 1.8, 1, '2017-10-25 14:12:24.650')
,(6, 3.2, 2, '2017-10-25 14:12:24.650')
,(7, 0.2, 3, '2017-10-25 14:12:24.650')
,(8, 1.2, 4, '2017-10-25 14:12:24.650');
SELECT [1], [2], [3], [4], [time]
FROM
(
SELECT [value], [type], [time]
FROM #DataSource
) DS
PIVOT
(
MAX([value]) FOR [type] IN ([1], [2], [3], [4])
) PVT
ORDER BY [time] DESC;
Here is another option using conditional aggregation or cross tab.
select Type1 = max(case when type = 1 then value)
Type2 = max(case when type = 2 then value)
Type3 = max(case when type = 3 then value)
Type4 = max(case when type = 4 then value)
, time
from YourTable
group by time
You can use PIVOT:
SELECT
[1] type1
, [2] type2
, [3] type3
, [4] type4
, time
FROM
(
SELECT
value
, type
, time
FROM table
) T
PIVOT
(
SUM (value)
FOR type IN
(
[1], [2], [3], [4]
)
) P
I have following 2 tables:
DECLARE #table1 TABLE (
[NO] int,
A1 float,
A2 float,
A3 float,
A4 float,
A5 float,
A6 float
)
DECLARE #table2 TABLE (
KOD nvarchar(100),
ID_QUANTITY int
)
INSERT INTO #table1 VALUES
(1, 4.1, 3, 3.5, 23.5, 12.2, 2.4),
(2, 4.2, 0, 1.9, 34.5, 31.2, 34.2),
(3, 1.5, 0, 2.3, 12.3, 12.3, 1.2),
(4, 3.7, 2.1, 5.9, 4.8, 10.2, 21.2),
(5, 3.9, 2.6, 1.9, 12.3, 2.4, 10.2)
INSERT INTO #table2 VALUES
('A1', 500),
('A2', 600),
('A3', 700),
('A4', 800),
('A5', 900),
('A6', 1000)
And I need create script in sql for transform value in first table to appearance as below
Thank you for your help
UNPIVOT then JOIN, use ROW_NUMBER() for ID column:
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as ID,
ID_QUANTITY,
[VALUES]
FROM (
SELECT *
FROM table1
) p
UNPIVOT (
[VALUES] FOR [COL] IN (A1,A2,A3,A4,A5,A6)
) as unpvt
LEFT JOIN table2 t
ON t.KOD = unpvt.[COL]
ORDER BY ID_QUANTITY, [NO]
Output:
ID ID_QUANTITY VALUES
1 500 4,1
2 500 4,2
3 500 1,5
4 500 3,7
5 500 3,9
6 600 3
7 600 0
8 600 0
9 600 2,1
10 600 2,6
...
25 900 2,4
26 1000 2,4
27 1000 34,2
28 1000 1,2
29 1000 21,2
30 1000 10,2
Use UNPIVOT
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
t2.ID_Quantity,
t1.Value
FROM Table2 t2
INNER JOIN
(
SELECT *
FROM Table1
UNPIVOT
(
[Values] FOR [Columns] IN ([A1], [A2],[A3], [A4], [A5],[A6])
) as pvt
) t1
ON t1.Columns = t2.KOD
try this :
create table3(ID_QUANTITY int(11),VALUE int(11));
for($i=1;$i<6;$i++){
$insert=mysql_query("insert into table3 select a.ID_QUANTITY,b.A$i from table2 a,table1 b where a.KOD='A$i');
}
I have a query in a stored procedure like below
select x,y from table
and the results will look like below
x y
1 a
1 b
2 a
2 b
3 a
3 b
i need to add a blank column or zeros when the value of x changes like below
x y
1 a
1 b
0 0
2 a
2 b
0 0
3 a
3 b
Can this be done by sql or since i'm using the data for birt reports can this be done with birt?
You need UNION ALL to add the extra rows, you also need to ORDER them, the DENSE_RANK is to get rid of the extra row.
here is how it could be done:
DECLARE #t table(x int, y char(1))
INSERT #t values
(1,'a'),(1,'b'),(2,'a'),
(2,'b'),(3,'a'),(3,'b')
;WITH CTE AS
(
SELECT
2 rn, x,y, x sort from #t
UNION ALL
SELECT
distinct dense_rank() over (order by x desc) rn, 0, '0', x+.1 sort
FROM #t
)
SELECT x,y
FROM CTE
WHERE rn > 1
ORDER BY sort, x
Result:
x y
1 a
1 b
0 0
2 a
2 b
0 0
3 a
3 b
This is working example:
DECLARE #DataSource TABLE
(
[x] TINYINT
,[y] CHAR(1)
);
INSERT INTO #DataSource ([x], [y])
VALUES (1, 'a')
,(1, 'b')
,(2, 'a')
,(2, 'b')
,(3, 'a')
,(3, 'b');
WITH DataSource AS
(
SELECT *
FROM #DataSource
UNION ALL
-- the NULL will be always displayed on the first position
SELECT DISTINCT [x]
,NULL
FROM #DataSource
)
SELECT IIF([Rank] = 1, 0, [x])
,IIF([Rank] = 1, 0, [x])
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY [x] ORDER BY [y]) AS [Rank]
,[x]
,[y]
FROM DataSource
) DS
ORDER BY [x]
,[Rank]
Few important notes:
the NULL values for each x will be with rank 1 always
the final result set is sorted by x and rank both
declare #t table (X varchar(1),Y varchar(1))
insert into #t(X,y) values (1,'A'),
(1,'B'),
(2,'A'),
(2,'B'),
(3,'A'),
(3,'B')
;with CTE As(
select X,Y,ROW_NUMBER()OVER(PARTITION BY X,Y ORDER BY X)RN
from #t
CROSS APPLY
(
values
('',NULL),
('',NULL)
) C(R, N)),
CTE2 AS(
Select CASE WHEN RN > 1 THEN 0 ELSE X END X ,
CASE WHEN RN > 1 THEN CAST(0 AS VARCHAR) ELSE Y END ID
,ROW_NUMBER()OVER(PARTITION BY X ORDER BY (SELECT NULL)) R
FROM CTE
)
select X,ID from cte2 where R <> 2