I have a Details table which can be described by the following:
Create table #Details (
Id int identity,
HeaderId int,
ChannelId int,
Value float
)
We insert some seed data:
insert into #Details (HeaderId, ChannelId, Value) values(1, 0, 1019.51)
insert into #Details (HeaderId, ChannelId, Value) values(1, 1, 20.1)
insert into #Details (HeaderId, ChannelId, Value) values(1, 2, 21.2)
insert into #Details (HeaderId, ChannelId, Value) values(1, 3, 22.3)
insert into #Details (HeaderId, ChannelId, Value) values(1, 4, 23.4)
insert into #Details (HeaderId, ChannelId, Value) values(2, 0, 1020.62)
insert into #Details (HeaderId, ChannelId, Value) values(2, 1, 26.1)
insert into #Details (HeaderId, ChannelId, Value) values(2, 2, 27.2)
insert into #Details (HeaderId, ChannelId, Value) values(2, 3, 28.3)
insert into #Details (HeaderId, ChannelId, Value) values(2, 4, 29.4)
Select All produces the following:
I would like to transpose the column data to rows, to produce the output below:
Thanks in advance.
Here you go. Just select first HeaderId,value, ChannelId in a subquery and then pivot.
DECLARE #tbl TABLE (Id int identity,
HeaderId int,
ChannelId int,
Value float)
insert into #tbl (HeaderId, ChannelId, Value) values(1, 0, 1019.51)
insert into #tbl (HeaderId, ChannelId, Value) values(1, 1, 20.1)
insert into #tbl (HeaderId, ChannelId, Value) values(1, 2, 21.2)
insert into #tbl (HeaderId, ChannelId, Value) values(1, 3, 22.3)
insert into #tbl (HeaderId, ChannelId, Value) values(1, 4, 23.4)
insert into #tbl (HeaderId, ChannelId, Value) values(2, 0, 1020.62)
insert into #tbl (HeaderId, ChannelId, Value) values(2, 1, 26.1)
insert into #tbl (HeaderId, ChannelId, Value) values(2, 2, 27.2)
insert into #tbl (HeaderId, ChannelId, Value) values(2, 3, 28.3)
insert into #tbl (HeaderId, ChannelId, Value) values(2, 4, 29.4)
SELECT *
FROM
(
SELECT HeaderId,value, ChannelId
FROM
#tbl
)T
PIVOT
(
SUM(value)
FOR ChannelId IN ([0],[1], [2], [3], [4]
)
) AS PivotTable;
Result below:
This should work:
select HeaderId
, [0] as ChannelId0
, [1] as ChannelId1
, [2] as ChannelId2
, [3] as ChannelId3
, [4] as ChannelId4
from
(
select HeaderId, ChannelId,Value
from #Details
) x
pivot
(
max(Value)
for ChannelId in([0],[1],[2], [3],[4],[5])
)p
Related
We have a table without date or incremental int column as below.
create table test(
id uniqueidentifier DEFAULT newsequentialid(),
userid int,
type int,
value varchar(20)
);
We wanted userid with value based on following logic, first data in case of primary else last data in case of other type.
Available type column value
-------------
0 - None
1 - Primary
2 - Other
3 - Registered
Logic to retrieve value should be as below :
if Primary available :
take "first" Primary's Value
else if Registered available :
take "last" Registered's Value
else if Other available :
take "last" Other's Value
else
take "last" None's Value
Created few scenarios to understand requirement better.
Scenario 1 :
/*
Output should be as below
userid, value
-------------
1, User1 value1.1
2, User2 value4.2
*/
insert into test (userid, type, value) values(1, 1, 'User1 value1.1')
insert into test (userid, type, value) values(1, 1, 'User1 value1.2')
insert into test (userid, type, value) values(1, 2, 'User1 value2.1')
insert into test (userid, type, value) values(1, 2, 'User1 value2.2')
insert into test (userid, type, value) values(1, 4, 'User1 value4.1')
insert into test (userid, type, value) values(1, 4, 'User1 value4.2')
insert into test (userid, type, value) values(2, 0, 'User2 value0.1')
insert into test (userid, type, value) values(2, 0, 'User2 value0.2')
insert into test (userid, type, value) values(2, 4, 'User2 value4.1')
insert into test (userid, type, value) values(2, 4, 'User2 value4.2')
Scenario 2 :
/*
Output should be as below
userid, value
-------------
3, User3 value1.1
4, User4 value4.2
*/
insert into test (userid, type, value) values(3, 0, 'User3 value0.1')
insert into test (userid, type, value) values(3, 1, 'User3 value1.1')
insert into test (userid, type, value) values(3, 2, 'User3 value2.1')
insert into test (userid, type, value) values(3, 2, 'User3 value2.2')
insert into test (userid, type, value) values(4, 4, 'User4 value4.1')
insert into test (userid, type, value) values(4, 4, 'User4 value4.2')
insert into test (userid, type, value) values(4, 2, 'User4 value2.1')
insert into test (userid, type, value) values(4, 0, 'User4 value0.1')
Scenario 3 :
/*
Output should be as below
userid, value
-------------
5, User5 value2.1
6, User6 value0.3
*/
insert into test (userid, type, value) values(5, 2, 'User1 value1.1')
insert into test (userid, type, value) values(5, 2, 'User1 value2.1')
insert into test (userid, type, value) values(5, 0, 'User1 value0.1')
insert into test (userid, type, value) values(6, 0, 'User6 value0.1')
insert into test (userid, type, value) values(6, 0, 'User6 value0.2')
insert into test (userid, type, value) values(6, 0, 'User6 value0.3')
Highly appreciate any help.
Is this what you want?
select t.*
from (select t.*,
row_number() over (partition by user_id
order by (case when type = 1 then 1 when type = 3 then 2 when type = 2 then 3 else 4 end),
(case when type = 1 then id end) asc,
id desc
) as seqnum
from test t
) t
where seqnum = 1;
CREATE TABLE TABLE_1 (
ID NUMBER(10),
ID_DOCUMENT NUMBER(10),
ITEM_ID NUMBER(10),
SUPLLIER NUMBER(10)
);
Insert into TABLE_1 (ID, ID_DOCUMENT, ITEM_ID, SUPLLIER) Values (1, 1, 11, 25);
Insert into TABLE_1 (ID, ID_DOCUMENT, ITEM_ID, SUPLLIER) Values (2, 1, 87, 31);
Insert into TABLE_1 (ID, ID_DOCUMENT, ITEM_ID, SUPLLIER) Values (3, 1, 93, 31);
Insert into TABLE_1 (ID, ID_DOCUMENT, ITEM_ID, SUPLLIER) Values (4, 1, 41, 25);
Insert into TABLE_1 (ID, ID_DOCUMENT, ITEM_ID, SUPLLIER) Values (5, 1, 58, 40);
When I insert into table_1 I have to insert the result into two other tables:
create table doc
(
id number(10),
suplier number(10),
date_doc date
);
create table doc_rows
(
id number(10),
id_doc number(10), -- (references doc.id)
item_id number(10)
);
I want to create 3 new records in table doc (because into table_1 we have 3 unique suppliers) and for every new doc I have to insert his items into table doc_rows
-- Create tables
CREATE TABLE TABLE_1 (
ID int identity (1,1) ,
ID_DOCUMENT int,
ITEM_ID int,
SUPLLIER int
);
create table doc
(
id int identity (1,1),
suplier int,
date_doc date
);
create table doc_rows
(
id int identity (1,1),
id_doc int, -- (references doc.id)
item_id int
);
--Create triggers
GO
CREATE TRIGGER trgTABLE_1_Insert ON TABLE_1
FOR INSERT
AS
declare #SUPLLIER int
select #SUPLLIER = SUPLLIER from inserted
if not exists (select 1 from doc where suplier=#SUPLLIER )
Begin
insert into doc (suplier,date_doc)
select #SUPLLIER,GETDATE()
insert into doc_rows (id_doc,item_id)
SELECT (select id from doc where suplier = #SUPLLIER) id_doc ,
(SELECT top 1 ITEM_ID from TABLE_1 where SUPLLIER = #SUPLLIER) item_id
End
Go
-- insert statement
Insert into TABLE_1 ( ID_DOCUMENT, ITEM_ID, SUPLLIER) Values ( 1, 11, 25);
Insert into TABLE_1 ( ID_DOCUMENT, ITEM_ID, SUPLLIER) Values ( 1, 87, 31);
Insert into TABLE_1 ( ID_DOCUMENT, ITEM_ID, SUPLLIER) Values ( 1, 93, 31);
Insert into TABLE_1 ( ID_DOCUMENT, ITEM_ID, SUPLLIER) Values ( 1, 41, 25);
Insert into TABLE_1 ( ID_DOCUMENT, ITEM_ID, SUPLLIER) Values ( 1, 58, 40);
--fianl output
SELECT * from TABLE_1
SELECT * from doc
SELECT * from doc_rows
how can i convert this access statement to sql ?
DELETE [Find duplicates for tBDEDom].* FROM [Find duplicates for tBDEDom];
CREATE TABLE #tBDEDom( Id int, Name VARCHAR(10))
insert into #tBDEDom values(1, 'abc')
insert into #tBDEDom values(2, 'mpo')
insert into #tBDEDom values(3, 'atc')
insert into #tBDEDom values(4, 'xyz')
insert into #tBDEDom values(5, 'abc')
insert into #tBDEDom values(6, 'xyz')
insert into #tBDEDom values(7, 'abc')
SELECT * FROM #tBDEDom
;with cte
AS(
SELECT *, ROW_NUMBER()OVER(partition by Name order by Name) AS [rank] FROM #tBDEDom
)
DELETE FROM cte where [rank]>1
SELECT * FROM #tBDEDom
DROP TABLE #tBDEDom
I would like to populate ordinal column but don't want to loop through records. Is there any way to do it in single update?
CREATE TABLE #Sample
(PrimaryKey Int NOT NULL,
ParentKey Int NOT NULL,
Ordinal Int NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (1, 1, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (2, 1, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (3, 1, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (4, 2, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (5, 2, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (6, 3, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (7, 4, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (8, 4, NULL)
INSERT #Sample (PrimaryKey, ParentKey, Ordinal) VALUES (9, 5, NULL)
SELECT * FROM #Sample
DROP TABLE #Sample
Values in Ordinal column would be 1, 2, 3, 1, 2, 1, 1, 2, 1
I want to number within each group. Group defined by "ParentKey" and Ordinal should go sorted by "PrimaryKey"
Important! Can't rely on values in PrimaryKey and ParentKey. They have "holes" and not necessary increment by 1 as shown in my sample..
Assuming SQL Server 2005+:
WITH CTE AS
(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY ParentKey ORDER BY PrimaryKey )
FROM #Sample
)
UPDATE CTE
SET Ordinal = RN
If I have the following table:
CREATE TABLE #temp (
id int,
num int,
question varchar(50),
qversion int );
INSERT INTO #temp VALUES(1, 1, 'Question 1 v1', 1);
INSERT INTO #temp VALUES(2, 1, 'Question 1 v2', 2);
INSERT INTO #temp VALUES(3, 2, 'Question 2 v1', 1);
INSERT INTO #temp VALUES(4, 2, 'Question 2 v2', 2);
INSERT INTO #temp VALUES(5, 2, 'Question 2 v3', 3);
INSERT INTO #temp VALUES(6, 3, 'Question 3 v1', 1);
SELECT *
FROM #temp;
DROP TABLE #temp;
And I would like to get a table to display the three questions in their lastest version? This is in SQL Server 2005
CREATE TABLE #temp (
id int,
num int,
question varchar(50),
qversion int );
INSERT INTO #temp VALUES(1, 1, 'Question 1 v1', 1);
INSERT INTO #temp VALUES(2, 1, 'Question 1 v2', 2);
INSERT INTO #temp VALUES(3, 2, 'Question 2 v1', 1);
INSERT INTO #temp VALUES(4, 2, 'Question 2 v2', 2);
INSERT INTO #temp VALUES(5, 2, 'Question 2 v3', 3);
INSERT INTO #temp VALUES(6, 3, 'Question 3 v1', 1);
WITH latest AS (
SELECT num, MAX(qversion) AS qversion
FROM #temp
GROUP BY num
)
SELECT #temp.*
FROM #temp
INNER JOIN latest
ON latest.num = #temp.num
AND latest.qversion = #temp.qversion;
DROP TABLE #temp;
SELECT t1.id, t1.num, t1.question, t1.qversion
FROM #temp t1
LEFT OUTER JOIN #temp t2
ON (t1.num = t2.num AND t1.qversion < t2.qversion)
GROUP BY t1.id, t1.num, t1.question, t1.qversion
HAVING COUNT(*) < 3;
You're using SQL Server 2005, so it's worth at least exploring the over clause:
select
*
from
(select *, max(qversion) over (partition by num) as maxVersion from #temp) s
where
s.qversion = s.maxVersion
I would like to get a table to display the three latest versions of each question.
I assume that that qversion is increasing with time. If this assumption is backwards, remove the desc keyword from the answer.
The table definition does not have an explicit not null constraint on qversion. I assume that a null qversion should be excluded. (Note: Depending on settings, lack of an explicit null/not null in the declaration may result in a not null constraint.) If the table does have a not null contraint, than the text where qversion is not null should be removed. If qversion can be null, and nulls need to be included in the result set, then additional changes will need to be made.
CREATE TABLE #temp (
id int,
num int,
question varchar(50),
qversion int );
INSERT INTO #temp VALUES(1, 1, 'Question 1 v1', 1);
INSERT INTO #temp VALUES(2, 1, 'Question 1 v2', 2);
INSERT INTO #temp VALUES(3, 2, 'Question 2 v1', 1);
INSERT INTO #temp VALUES(4, 2, 'Question 2 v2', 2);
INSERT INTO #temp VALUES(5, 2, 'Question 2 v3', 3);
INSERT INTO #temp VALUES(7, 2, 'Question 2 v4', 4);
-- ^^ Added so at least one row would be excluded.
INSERT INTO #temp VALUES(6, 3, 'Question 3 v1', 1);
INSERT INTO #temp VALUES(8, 4, 'Question 4 v?', null);
select id, num, question, qversion
from (select *,
row_number() over (partition by num order by qversion desc) as RN
from #temp
where qversion is not null) T
where RN <= 3