SQL - Sort Select from table - sql

I have the following row from a table in MSSQL
Person | value1 | value2 | value3 | value4
John | 4 | 7 | 1 | 2
I want this row sorted according to the values as shown below.
Person | value2 | value1 | value4 | value3
John | 7 | 4 | 2 | 1
May be I'm asking a stupid question , I want the column names to be sorted according to the values in those column.
I know that this can be achieved using query
SELECT *
FROM tableName
ORDER BY value2, value1, value4, value3 DESC
But when I change the any column value, I have to change the query to get the result sorted.
Please help...

-- sample data
declare #t table
(
person varchar(50),
value1 int,
value2 int,
value3 int,
value4 int
)
insert into #t select 'John', 4, 7, 1, 2
insert into #t select 'Name1', 14, 7, 1, 16
insert into #t select 'Name2', 24, 8, 1, 2
-- query
select
person, v1 = [1], v2 = [2], v3 = [3], v4 = [4]
from
(
select person, rn = row_number() over(partition by person order by a desc), a
from #t t1
unpivot
(
a for b in (value1, value2, value3, value4)
) t2
) t1
pivot
(
max(a) for rn in ([1], [2], [3], [4])
) t2

unless I'm missing something, you should just append 'order by person, value2, value1, value4, value3' at the end of your select statement.

I think you want:
SELECT Person, Value2, Value1, Value4, Valuer
FROM YourTable

try this:
SELECT * FROM tableName ORDER BY value2, value1, value4, value3 DESC

Related

How to select a value on the first row and last on SQL?

I have a table like this:
ID | persid | value1 | value2
---+--------+---------+---------
1 | A | string1 | string2
2 | A | string3 | string4
3 | A | string5 | string6
I need to return something like this using GROUP BY:
Field1 | field2 | field3
-------+---------+----------
A | string1 | string6
I'm using SQL server 2008,
Is this possible?
I would use conditional aggregation:
select persid,
max(case when seqnum_asc = 1 then value1 end) as value1,
max(case when seqnum_desc = 1 then value2 end) as value2
from (select t.*,
row_number() over (partition by persid order by id) as seqnum_asc,
row_number() over (partition by persid order by id desc) as seqnum_desc
from t
) t
group by persid;
You can try something like this:
create table tbl (id int, persid varchar(10), value1 varchar(10), value2 varchar(10));
insert into tbl (id, persid, value1, value2) values (1, 'A', 'string1', 'string2');
insert into tbl (id, persid, value1, value2) values (2, 'A', 'string3', 'string4');
insert into tbl (id, persid, value1, value2) values (3, 'A', 'string5', 'string6');
select tbl.persid, tbl.value1, persmax.value2
from tbl
inner join tbl persmax on persmax.id = (select max(id) from tbl where persid='A')
where tbl.persid='A'
and tbl.id = (select min(id) from tbl where persid='A')
I think you can use a query like this:
select top(1) yourTable.persid field1, yourTable.value1 field2, t.value2 field3
from yourTable
cross join (
select top(1) value2
from yourTable
order by ID desc
) t
order by ID;
How about this?
select distinct a, b, c from
(select mytbl.persid as a, mytbl.value1 as b from mytbl,
(select persid, min(id) as id from mytbl group by persid) as t1
where mytbl.id = t1.id and mytbl.persid = t1.persid) as t2,
(select mytbl.persid as d, mytbl.value2 as c from mytbl,
(select persid, max(id) as id from mytbl group by persid) as t3
where mytbl.id = t3.id and mytbl.persid = t3.persid) as t4
where t2.a = t4.d

SQL server query: only certain column values into row, iterating

I need to build a SQL query in SQL SERVER 10.50.1600.1. I have the following model situation:
id1 | value1
id1 | value2
id2 | value1
id3 | value1
id3 | value2
...
and would like to end up in a situation as
id1 | value1 | value2
id2 | value1 | null
id3 | value1 | value2
...
I only know that for each id[n] there are at most 4 values recorded.
EDIT: I know there are at most 4 values for each, but they could be anything. They can be any number included in [0, 9000] or a string (about 10 possibilities). My bad, I didn't explain well.
If you know the values in advance, and the values are unique per ID (as they seem to be from your question statement) it's fairly straightforward:
-- Setup
declare #a table (id nvarchar(50), value nvarchar(50))
insert #a(id, value) values
('id1', 'value1'),
('id1', 'value2'),
('id2', 'value1'),
('id3', 'value1'),
('id3', 'value2')
;
SELECT id,
MAX(CASE value WHEN 'value1' THEN value END) AS value1,
MAX(CASE value WHEN 'value2' THEN value END) AS value2,
MAX(CASE value WHEN 'value3' THEN value END) AS value3,
MAX(CASE value WHEN 'value4' THEN value END) AS value4
FROM #a
GROUP BY id
Below query will help you :-
declare #test table (id nvarchar(50), value nvarchar(50))
insert #test(id, value) values
('id1', 'value1'),
('id1', 'value2'),
('id1', 'value3'),
('id1', 'value4'),
('id2', 'value1'),
('id3', 'value1'),
('id3', 'value2'),
('id3', 'value3');
select ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS ROWNUM,id,value into #t1 from #test
SELECT distinct id,
(select value from #t1 b where b.id=a.id AND b.ROWNUM=1) AS Value1,
(select value from #t1 b where b.id=a.id AND b.ROWNUM=2) AS Value2,
(select value from #t1 b where b.id=a.id AND b.ROWNUM=3) AS Value3,
(select value from #t1 b where b.id=a.id AND b.ROWNUM=4) AS Value4
FROM #t1 a
drop table #t1
Output :-
id Value1 Value2 Value3 Value4
id1 value1 value2 value3 value4
id2 value1 NULL NULL NULL
id3 value1 value2 value3 NULL
Assumtion: TABLE: table_name(id_column, value_column). With each value of id_column there are at most 4 values of value_column
Then you could use this
WITH tmp as
(SELECT
*
,ROW_NUMBER() over (PARTITION BY id_column order by value_column) rwn
FROM
table_name
)
SELECT
id_column
,MAX(CASE WHEN rwn = 1 THEN value_column END) value1
,MAX(CASE WHEN rwn = 2 THEN value_column END) value2
,MAX(CASE WHEN rwn = 3 THEN value_column END) value3
,MAX(CASE WHEN rwn = 4 THEN value_column END) value4
FROM
tmp
GROUP BY
id_column;

Transfer Rows to columns

I have something like this in the xml
c1.1 test
c1.2 10
c1.3 100
c2.1 test1
c2.2 10
c2.3 1000
and i want to transform into like this
test 10 100
test1 10 1000
Please help .I tried with pivot and could not crack it .Need to mention here that c1.1 ,c1.2,c1.3 is a series and these 3 has to be in i row
Pivot will do exactly what you want, something like this:
select * from (
select left(type,2) as row, right(type, 1) as col, value
from Table1
) S pivot (
max(value) for col in ([1], [2], [3])
) P
Example in SQL Fiddle
SQL Fiddle
MS SQL Server 2014 Schema Setup:
CREATE TABLE Table1
([Code] varchar(20), [Value] varchar(20))
;
INSERT INTO Table1
([Code], [Value])
VALUES
('c1.1', 'test'),
('c1.2', '10'),
('c1.3', '100'),
('c2.1', 'test1'),
('c2.2', '10'),
('c2.3', '1000')
;
Query 1:
select
Value1, Value2, Value3
from (
select
Value as Value1
, lead(Value,1) over(partition by left(t1.code,charindex('.',t1.code)-1)
order by substring(t1.code,charindex('.',t1.code)+1,len(t1.code))) as Value2
, lead(Value,2) over(partition by left(t1.code,charindex('.',t1.code)-1)
order by substring(t1.code,charindex('.',t1.code)+1,len(t1.code))) as Value3
from table1 t1
) as derived
where Value3 is not null
Results:
| Value1 | Value2 | Value3 |
|--------|--------|--------|
| test | 10 | 100 |
| test1 | 10 | 1000 |
In SqlServer 2008 Schema Set Up we can do using Row_number. basing on the sample data i have given this code.
DECLARE #Table1 TABLE
(val varchar(4), col varchar(5))
;
INSERT INTO #Table1
(val, col)
VALUES
('c1.1', 'test'),
('c1.2', '10'),
('c1.3', '100'),
('c2.1', 'test1'),
('c2.2', '10'),
('c2.3', '1000')
;
Select [1] as [Value1],[2] as [Value2],[3] as [Value3]from (
select SUBSTRING(reverse(val),0,CHARINDEX('.',Reverse(val))) R,
col
,ROW_Number()OVER(PARTITION BY SUBSTRING(reverse(val),0,CHARINDEX('.',reverse(val))) Order by val) RN
from #Table1)T
PIVOT(max(col) for R IN ([1],[2],[3]))PVT

Find distinct number for a group of columns partitioned by another column in table in SQL Server

I am working with SQL Server 2008 on Windows 7.
I need to find distinct number of a group of columns partitioned by another column.
Table:
ID value1 value2 value3
--------------------------
358 32169 31984 9716
358 9441 94 97897
235 32169 31984 9716
235 8464 8454 36197
235 8646 97 36879
Result:
ID distinct_num_of_value1_value2_value3
------------------------------------------
358 2 // 358 has two distinct rows with v1 v2 v3
235 3 // 235 has 3 distinct rows with v1 v2 v3
Any help would be appreciated.
How about
select
id,
count(distinct cast(value1 as varchar(32)) + '_' + cast(value2 as varchar(32)) + '_' + cast(value3 as varchar(32))) as distinct_num_of_value1_value2_value3
from
YourTable
group by
ID
Test table:
DECLARE #t table(ID int, value1 int, value2 int, value3 int)
INSERT #t values
(358,32169,31984,9716),(358,9441,94,97897),
(235,32169,31984,9716),(235,8464,8454,36197),
(235,8646,97,36879)
Query:
--use group by
;WITH CTE AS
(
SELECT
id, value1, value2, value3
FROM
#t
GROUP BY
id, value1, value2, value3
)
SELECT
count(*) cnt,
id
FROM
CTE
GROUP BY
id
--or use distinct
;WITH CTE AS
(
SELECT
distinct id, value1, value2, value3
FROM
#t
)
SELECT
count(*) cnt,
id
FROM
CTE
GROUP BY
id
Result:
cnt id
3 235
2 358

SQL Transpose multiple columns into rows [duplicate]

This question already has an answer here:
SQL transpose full table
(1 answer)
Closed 9 years ago.
Hello I am new to SQL and would really appreciate some help with transposing a table as follows. Looking at similar posts it seems that this may be accomplished with pivot/unpivot, but I am not sure since the examples I read about have much fewer columns. Any suggestions would be great!
Table.
CATEGORY Value1 Value2 Value3 Value4 ... Value15
Hot 18 17 9 17 ... 18
Warm 5 3 0 1 ... 3
Cold 20 2 1 2 ... 2
Desired Result.
CATEGORY Hot Warm Cold
Value1 18 5 20
Value2 17 3 2
Value3 9 0 1
Value4 17 1 2
… … … …
Value15 18 3 2
select *
from tbl
unpivot (value for name in (Value1, Value2, Value3, Value4, ... Value15)) unpiv
pivot (max(value) for category in ([hot],[warm],[cold])) piv
The following is a simple code i tried out. Just run and see if it meets your requirement!!
declare #Tbl1 table(Category Varchar(10), Value1 Int, Value2 Int, Value3 Int, Value4 Int, Value5 Int)
insert into #Tbl1(Category, Value1, Value2, Value3, Value4, Value5)
Values ('Hot', 12, 23, 43, 5, 6)
insert into #Tbl1(Category, Value1, Value2, Value3, Value4, Value5)
Values ('Warm', 41, 28, 4, 45, 16)
insert into #Tbl1(Category, Value1, Value2, Value3, Value4, Value5)
Values ('Cold', 1, 3, 543, 15, 26)
select * from #Tbl1
Declare #TblUP Table(Category Varchar(100), Value Int, [Types] Varchar(10))
Insert into #TblUP(Category, Value, Types)
select * from #Tbl1 unpivot ([values] for Types in (Value1, Value2, Value3, Value4, Value5)) as UNPIV
select * from #TblUP
Select Types, Hot, Warm, Cold
From
(Select Types, Category, Value from #TblUP) as TUP
PIVOT (SUM(Value) for [Category] in ([Hot], [Warm], [Cold]))
as PT