Inserting multiple rows with column into single row - sql

I am trying to write a query to copy set of rows from one table to another table in this format
Table 1
ColumnName ColumnValue RowId
Column1 Value1 1
Column2 Value2 1
Column3 Value3 1
Column1 Value4 2
Column2 Value5 2
Column3 Value6 2
Column1 Value7 3
Column2 Value8 3
Column3 Value9 3
Table2
Column1 Column2 Column3
Value1 Value2 Value3
Value4 Value5 Value6
Value7 Value8 Value9
Here basically table 1 is input and table 2 is the output which I am trying. I used the pivot and row number but none worked.
What will be the insert query to achieve this. Here all rows of rowId 1 from table 1 will form form one row of table2.

select rowID,
max(case when columnName = 'Column1' then value else null end) as column1,
max(case when columnName = 'Column2' then value else null end) as column2,
max(case when columnName = 'Column3' then value else null end) as column3
from table1
group by rowID

Related

Get count of common field among the same records, SQL Server?

Assume we have the following records:
Id
Column A
Column B
Column C
Common
1
Value1
Value2
Value3
12
2
Value1
Value2
Value3
13
3
Value1
Value2
Value3
08
4
Value1
Value2
Value3
10
5
Value4
Value5
Value6
18
6
Value4
Value5
Value6
22
7
Value4
Value5
Value6
37
How can we get the following result
Id
Column A
Column B
Column C
CommonCount
1
Value1
Value2
Value3
4
5
Value4
Value5
Value6
3
I wrote this query:
SELECT
jr.*,
CommonCount = (SELECT COUNT(1)
FROM la.JudicialReference AS jr1
WHERE ((jr.ColumnA = jr1.ColumnA)
OR ISNULL(jr.ColumnA, jr1.ColumnA) IS NULL)
AND ((jr.ColumnB = jr1.ColumnB)
OR ISNULL(jr.ColumnB, jr1.ColumnB) IS NULL)
AND ((jr.ColumnC = jr1.ColumnC)
OR ISNULL(jr.ColumnC, jr1.ColumnC) IS NULL))
FROM
la.JudicialReference AS jr
But it doesn't get me the first Id and I'm looking for a better query to save IO
Simply
SELECT MIN(ID), ColumnA, ColumnB, ColumnC, COUNT(*)
FROM la.JudicialReference
GROUP BY ColumnA, ColumnB, ColumnC
Optionally with HAVING COUNT(*) > 1

how to get rows which have max value2 when value1 is equal using django queryset

for example
rowid value1 value2
1 1 2
2 1 3
3 2 4
then select (1,1,3) and (3,2,4) because row1 and row2 have the same value1, and value2 of row2 is bigger than value2 of row1
thank you!
In sql this is a simple aggregation query using group by and min()/max()
select
min(rowid) as rowid
, value1
, max(value2) as value2
from t
group by value1

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;

SQL - Transpose one row into two columns

Mine SQL Query returns a few columns but only one row. I need to transpose this one row and column's names into two columns - first with column's names and second with row values. For some reasons I can't use crosstab.
Operation that I want to perform is shown below.
Before:
Column1 | Column2 | Column3 | Column4
---------+---------+---------+--------
Value1 | Value2 | Value3 | Value4
And I want to get this:
Cols | Vals
--------+---------
Column1 | Value1
Column2 | Value2
Column3 | Value3
Column4 | Value4
Does this do what you want?
SELECT
'Column1' AS Cols
, Column1 AS Vals
FROM Table
UNION ALL
SELECT
'Column2' AS Cols
, Column2 AS Vals
FROM Table
UNION ALL
SELECT
'Column3' AS Cols
, Column3 AS Vals
FROM Table
UNION ALL
SELECT
'Column4' AS Cols
, Column4 AS Vals
FROM Table
select 'Column1' as Cols, Column1 as Vals from YourTable
union all
select 'Column2', Column2 from YourTable
union all
select 'Column3', Column3 from YourTable
union all
select 'Column4', Column4 from YourTable
Try crosstab from the tablefunc extension. http://www.postgresql.org/docs/9.0/static/tablefunc.html
It should do exactly what you want.
You can install the extension like this:
CREATE EXTENSION tablefunc;

Return null row on select sql

I know this might seem a bit strange but I want to build a stored procedure that returns some values but I want the first row to be a null value. What I have for the query and returns are
SELECT CONCAT(id, ' ', item)As DisplayBox, id, item FROM table
DisplayBox id expense
1 Value2 1 Value2
2 Value3 2 Value3
3 Value4 3 Value4
4 Value5 4 Value5
But I am interested in having a result like
DisplayBox id Item
<BlankText> Null <BlankText>
1 Value2 1 Value2
2 Value3 2 Value3
3 Value4 3 Value4
4 Value5 4 Value5
You can use UNION ALL:
SELECT NULL AS DisplayBox, NULL AS id, NULL AS item
UNION ALL
SELECT CONCAT(id, ' ', item)As DisplayBox, id, item FROM table
SELECT null as displaybox, null as id, null as item from dual
union all
SELECT CONCAT(id, ' ', item)As DisplayBox, id, item FROM table
order by (case when id is null then 0 else 1 end)