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
Related
I have 2 tables Denials_Scrub_Final and Claims_Final
I want to get the percent for all the invoice.
That would be (Denials_Scub_Final/Claims_Final) *100
This would display a new table Percentage as a new table.
This should look like the excel screenshot which was manually done.
Last block Denial Rate.
No final solution, but the approach should be clear.
Since we do not have the data from Denials_Scrub_Final or Claims_Final I generated some sample data. I am using a single table variable #data with some duplicates to generate the different numbers.
-- create sample data
declare #data table
(
Yr int,
Mth int,
Center nvarchar(10),
Invoice int
);
insert into #data (Yr, Mth, Center, Invoice) values
(2020, 1, 'ABC', 101),
(2020, 1, 'ABC', 101),
(2020, 1, 'ABC', 102),
(2020, 1, 'DEF', 201),
(2020, 1, 'DEF', 202),
(2020, 2, 'ABC', 103),
(2020, 2, 'ABC', 104),
(2020, 2, 'DEF', 203),
(2020, 3, 'ABC', 105),
(2020, 3, 'ABC', 105),
(2020, 3, 'ABC', 106),
(2020, 2, 'DEF', 204),
(2020, 2, 'DEF', 204),
(2020, 2, 'DEF', 205);
The first data set:
-- reproduce first query
select *
from ( select distinct Invoice, Center, Yr, Mth from #data ) BaseData
pivot ( count(Invoice) for Center in ([ABC], [DEF]) ) p;
gives
Yr Mth ABC DEF
----------- ----------- ----------- -----------
2020 1 2 2
2020 2 2 3
2020 3 2 0
The second data set:
-- reproduce second query
select *
from ( select Invoice, Center, Yr, Mth from #data ) BaseData
pivot ( count(Invoice) for Center in ([ABC], [DEF]) ) p;
gives
Yr Mth ABC DEF
----------- ----------- ----------- -----------
2020 1 3 2
2020 2 2 4
2020 3 3 0
Final solution: move the data sets in common table expressions (CTE's) and join them to apply the formula (use case to avoid division by zero and convert to format the percentages):
-- combine queries in CTE's and apply formula
with cte_denial as
(
select p.Yr, p.Mth, p.ABC, p.DEF
from ( select distinct Invoice, Center, Yr, Mth from #data ) BaseData
pivot ( count(Invoice) for Center in ([ABC], [DEF]) ) p
),
cte_claim as
(
select p.Yr, p.Mth, p.ABC, p.DEF
from ( select Invoice, Center, Yr, Mth from #data ) BaseData
pivot ( count(Invoice) for Center in ([ABC], [DEF]) ) p
)
select c1.Yr,
c1.Mth,
convert(numeric(5,2), case when c2.ABC > 0 then c1.ABC*100.0/c2.ABC else 0 end) as 'ABC_%',
convert(numeric(5,2), case when c2.DEF > 0 then c1.DEF*100.0/c2.DEF else 0 end) as 'DEF_%'
from cte_denial c1
join cte_claim c2
on c2.Yr = c1.Yr
and c2.Mth = c1.Mth;
gives
Yr Mth ABC_% DEF_%
----------- ----------- ---------- ----------
2020 1 66.67 100.00
2020 2 100.00 75.00
2020 3 66.67 0.00
I have the following Table1 in SQL Server 2016:
SELECT Year, Type, Value From Table1
Year Type Value
2010 1 10
2010 2 15
2010 3 20
2011 1 100
2011 2 150
2011 3 200
I would like to convert it to the following table:
Year Type1 Type2 Type3
2010 10 15 20
2011 100 150 200
I think we can do either self join or pivot table to achieve this. What is the best way to achieve this?
CREATE TABLE #myTable (
[Year] int, [Type] int, [Value] int, [ExtraColumn] varchar(10));
INSERT INTO #myTable ([Year], [Type], [Value], [ExtraColumn])
VALUES (2010, 1, 10, 'I'),
(2010, 2, 15, 'G'),
(2010, 3, 20, 'N'),
(2011, 1, 100, 'O'),
(2011, 2, 150, 'R'),
(2011, 3, 200, 'E');
select Year, [1] as Type1, [2] as Type2, [3] as Type3
from (
select [Year], [Type], [Value]
from #myTable
) t
PIVOT ( SUM(Value) FOR [Type] IN ( [1], [2], [3] ) ) pvt;
-- OR
with myData as
(
select [Year], [Type], [Value]
from #myTable
)
select Year, [1] as Type1, [2] as Type2, [3] as Type3
from myData
PIVOT ( SUM(Value) FOR [Type] IN ( [1], [2], [3] ) ) pvt;
drop table #myTable;
Assuming you always have 3 types using conditional aggregation is a simple way to tackle this.
select [Year]
, Type1 = Max(case when [Type] = 1 then Value end)
, Type2 = Max(case when [Type] = 2 then Value end)
, Type3 = Max(case when [Type] = 3 then Value end)
from Table1
group by [Year]
order by [Year]
select *
from myTable PIVOT ( SUM(Value) FOR [Type] IN ( [1], [2], [3] ) ) pvt;
DbFiddle demo
Assuming you always have 3 types, you can use PIVOT in SQL.
Here is an example based on your example:
if object_id('tempdb..#temp1') is not null
drop table #temp1
create table #temp1 (
Year int
,Type int
,Value int
)
insert into #temp1 values
(2010,1,10),
(2010,2,15),
(2010,3,20),
(2011,1,100),
(2011,2,150),
(2011,3,200)
SELECT
Year
, [1] AS Type1
, [2] AS Type2
, [3] AS Type3
FROM
#temp1 p
PIVOT
(
sum(value)
FOR type IN
( [1], [2], [3])
) AS pvt
ORDER BY pvt.Year
Here are the results:
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'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
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');
}