update same rows - sql

I have a table like below:
Current
Column1 Column2 column3
-------------------------
abc cat1 1
efg cat1 3
hij cat1 2
klm cat2 1
nop car2 2
qrs cat2 3
I want to update column1 where all rows matching cat1 from rows matching cat2 , considering rows will be updated for id which matches column 3 ids
Expected
Column1 Column2 column3
-------------------------
klm cat1 1
qrs cat1 3
nop cat1 2
klm cat2 1
nop car2 2
qrs cat2 3

Use below query :
CREATE TABLE #Table (Column1 VARCHAR(100),Column2 VARCHAR(100), Column3
VARCHAR(100))
INSERT INTO #Table ( Column1 , Column2 , Column3 )
SELECT 'abc','cat1',1 UNION ALL
SELECT 'efg','cat1',3 UNION ALL
SELECT 'hij','cat1',2 UNION ALL
SELECT 'klm','cat2',1 UNION ALL
SELECT 'nop','cat2',2 UNION ALL
SELECT 'qrs','cat2',3
UPDATE #Table SET Column1 = A.Column1
FROM
(
SELECT Column1 , Column3
FROM #Table
WHERE Column2 = 'cat2'
)A WHERE #Table.Column2 = 'cat1' AND A.Column3 = #Table.Column3
SELECT * FROM #Table
http://rextester.com/QYHLTE44670

Please try with below code:
DECLARE #Table TABLE
(Column1 VARCHAR(100),Column2 VARCHAR(100), Column3 INT)
INSERT INTO #Table VALUES
('abc','cat1',1),
('efg','cat1',3),
('hij','cat1',2),
('klm','cat2',1),
('nop','cat2',2),
('qrs','cat2',3)
UPDATE
t
SET t.Column1 = d.Column1
FROM #Table t INNER JOIN #Table d
ON t.Column3 = d.Column3
WHERE d.Column2 ='cat2'

Related

select * using Group By Oracle SQL

I have this tables
column1 column2 colum3 column4 colum5 column6
a b 1 2 0 x
a b 1 2 0 y
a b 3 0 0 z
I want to group the same record
my sql code looks like this but it gives the same result (select * from employee where column1 = 'a' and column2= 'b')
SELECT a.column1,a.column2,a.column3,a.column4,a.column5
FROM employee a,
(SELECT column3, column4,column5
FROM employee
WHERE column1 = 'a' and column2= 'b'
GROUP BY column3 colum4 column5) b
WHERE a.column3 = b.column3,
and a.column4 = b.column4,
and a.column5 = b.column5,
the result should be like this:
column1 column2 colum3 column4 colum5
a b 1 2 0
a b 3 0 0
Try the following with window function row_number(), here is the demo.
select
column1,
column2,
colum3,
column4,
colum5
from
(
select
column1,
column2,
colum3,
column4,
colum5,
row_number() over (partition by column1, column2, colum3, column4, colum5 order by column1) as rnk
from myTable
) val
where rnk = 1
Output:
*---------------------------------------*
| COLUMN1 COLUMN2 COLUM3 COLUMN4 COLUM5|
*---------------------------------------*
| a b 1 2 0 |
| a b 3 0 0 |
*---------------------------------------*
For this dataset, a simple distinct on the first five columns would return the result that you expect:
select distinct column1, column2, column3, colum4, column5
from employee

SQL Aggregate function based on 1 column and display the remaining

I am trying to sum the values for 1 column and group it by only 1 column but show the other columns as well.
Basically, if the data looks like:
Column1 Column2 Column3 Column4
----------------------------------
Col1Data0 Test1 Test2 1
Col1Data0 Test1 Test2 3
Col1Data1 Test1 Test2 2
Col1Data1 Test1 Test2 5
The query should output:
Column1 Column2 Column3 Column4
Col1Data0 Test1 Test2 4
Col1Data0 Test1 Test2 4
Col1Data1 Test1 Test2 7
Col1Data1 Test1 Test2 7
You can use a SUM window function:
Test data:
declare #t table (Column1 varchar(10), Column2 varchar(10), Column3 varchar(10), Column4 int)
insert into #t values
('Col1Data0','Test1','Test2',1),
('Col1Data0','Test1','Test2',3),
('Col1Data1','Test1','Test2',2),
('Col1Data1','Test1','Test2',5)
Query:
SELECT Column1, Column2, Column3,
SUM(Column4) OVER (PARTITION BY Column1) AS SumColumn4
FROM #t
Returns:
Column1 Column2 Column3 SumColumn4
Col1Data0 Test1 Test2 4
Col1Data0 Test1 Test2 4
Col1Data1 Test1 Test2 7
Col1Data1 Test1 Test2 7
You can use a subquery to get the sum for column 1 and join it to get the output
Select a.Column1,a.Column2,a.Column3,b.summ
FROM Table a
INNER JOIN (Select column1,sum(Column4) summ from Table group by column1) b
on b.column1 = a.column1
use scalar subquery
select Column1,Column2,Column3,
(
select sum(Column4) from tablename b
where a.Column1=b.Column1 and a.Column2=b.Column2 and a.Column3=b.column3
) as column4
from tablename a

Creating resultset based on column name defined in another table in Oracle

In Oracle database, I have a table DATA_DUMP_TABLE which has generic column names
id column1 column2 column3 column4 column5 def_id
1 John Doe null null null 1
2 Tom Baker null null null 1
Then I have a metadata table COLUMN_DEF which defines the data column names. It could vary based on the definition id. Say in this case I just want to map the first 2 columns.
def_id mapped_column mapped_column_val`
1 column1 First Name
1 column2 Last Name
The resultset I am expecting is based on the mapped_column_val from COLUMN_DEF table
So, in this case the resultset would look like this
id First Name Last Name
1 John Doe
2 Tom Baker
How do I achieve this in oracle, whether this could be done via SQL or I have to use advance packages? Any help would be appreciated.
It Works with sql server.
DECLARE #new_names as nvarchar(MAX)
DECLARE #sql as nvarchar(MAX)
SELECT #new_names = STRING_AGG('[' + MAPPED_COLUMN_VAL + ']', ',') FROM COLUMN_DEF
SET #sql = 'SELECT * FROM (
SELECT t.ID, t.COLUMN_VALUES, t.DEF_ID, c.MAPPED_COLUMN_VAL FROM (
SELECT ID, COLUMN_NAME, COLUMN_VALUES, DEF_ID
FROM
(SELECT * FROM DUMP_DATA) d
UNPIVOT
(
COLUMN_VALUES FOR COLUMN_NAME IN
(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5)
) unpvt
) AS t LEFT OUTER JOIN
COLUMN_DEF c ON t.COLUMN_NAME = c.MAPPED_COLUMN
) b
PIVOT
(
MAX(COLUMN_VALUES) FOR MAPPED_COLUMN_VAL IN ( ' + #new_names + ' )
--IN ([FIRST NAME], [LAST NAME])
) AS pivot_table;'
EXEC dbo.SP_EXECUTESQL #sql
Unpivot Will remove columns with NULL values.
INPUT:
ID COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 DEF_ID
1 JOHN DOE NULL NULL NULL 1
2 TOM BAKER NULL NULL NULL 2
DEF_ID MAPPED_COLUMN MAPPED_COLUMN_VAL
1 COLUMN1 FIRST NAME
2 COLUMN2 LAST NAME
RESULT:
ID DEF_ID FIRST NAME LAST NAME
1 1 JOHN DOE
2 2 TOM BAKER
In general I think that this requires pl/sql and dynamic SQL as stated by #gordon-linoff
This is gets very close.
Create table temp_sgc1(id number, column1 Varchar2(20), column2 varchar2(20));
Insert into temp_sgc1 values(1 , 'John' , 'Doe');
Insert into temp_sgc1 values(2 , 'Tom' , 'Baker');
Create table temp_sgc2(def_id number, mapped_column Varchar2(20), mapped_column_va
l
varchar2(20));
Insert into temp_sgc2 values(1, 'column1', 'First Name');
Insert into temp_sgc2 values(1, 'column2', 'Last Name');
Select * from
(select * from temp_sgc2
pivot (
max(mapped_column_val) for mapped_column in ( 'column1' column1, 'column2' column2 )
)
order by def_id)
union all
Select id, column1, column2
from temp_sgc1
;
RESULT:
DEF_ID COLUMN1 COLUMN2
1 First Name Last Name
1 John Doe
2 Tom Baker

MS SQL count AS to new column

Good day
I have problem with my table and counting
TABLE1
COLUMN1 COLUMN2
3 jjd
5 jd
3 jjd
4 kg
5 jd
48 gjh
446 djj
… …
I need
TABLE1
COLUMN1 COLUMN2 COLUMN3
3 jj 2
5 jd 2
4 kg 1
48 gjh 1
446 djj 1
... ... …
Iam doing but not working well.
SELECT * , COUNT(Column1) as column3 FROM TABLE1
Thanks for help withh my counting
Use a GROUP BY and ORDER BY with DESC to put them in COUNT total order.
SELECT COLUMN1, COLUMN2, COUNT(Column1) AS COLUMN3
FROM Table1
GROUP BY COLUMN1, COLUMN2
ORDER BY COUNT(Column1) DESC
Output
COLUMN1 COLUMN2 COLUMN3
5 jd 2
3 jjd 2
4 kg 1
446 djj 1
48 gjh 1
SQL Fiddle: http://sqlfiddle.com/#!6/89f49/4/0
Try by using group by
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1) As COLUMN3 FROM cte_TABLE1
Group by COLUMN1 ,COLUMN2
Order by COLUMN1
By using Window function
SELECT DISTINCT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Result
COLUMN1 COLUMN2 column3
-----------------------
3 jjd 2
4 kg 1
5 jd 2
48 gjh 1
446 djj 1
Using OVER we can achieve it easily
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Group By COLUMN1,COLUMN2

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;