Select rows with same id but different value in another column
This ought to do it:
SELECT *
FROM YourTable
WHERE ARIDNR IN (
SELECT ARIDNR
FROM YourTable
GROUP BY ARIDNR
HAVING COUNT(*) > 1
)
The idea is to use the inner query to identify the records which have a ARIDNR
value that occurs 1+ times in the data, then get all columns from the same table based on that set of values.
select rows with same ID but different value in another column between two tables
select t1.*
from t1
join t2 on t1.id = t2.id
where t1.active <> t2.active
Select rows with same id and set same value in a row
Maybe try using max()
:
select ID,
max(NAME) over(partition by REKEY) as LIEFNR,
REKEY
from table_name;
Select rows with same id but same value in another column
I edited your query below and updated it such that it returns the expected results.
SELECT account_id, max(batch_source) AS id_group
FROM MyTable group by account_id
having count(distinct(batch_source)) = 1 ;
Select one row with same id and match value in another column from another table
With SQL you can do use subselects to compare the counts:
select t.*
from task t
join task_requirement tr on t.uuid = tr.task_id
join requirement r on tr.requirement_id = r.id
join requirement_answer ra1 on r.id = ra1.requirement_id
join wedding_requirement_answer wra1 on ra1.id = wra1.requirement_answer_id
where wra1.wedding_id = 1
and ( (select ra2.requirement_id
from requirement_answer ra2
join wedding_requirement_answer wra2 on ra2.id = wra2.requirement_answer_id
where wra2.wedding_id = wra1.wedding_id
and ra2.requirement_id = ra1.requirement_id))
=
(select ra3.requirement_id
from requirement_answer ra3
join wedding_requirement_answer wra3 on ra3.id = wra3.requirement_answer_id
where wra3.wedding_id = wra1.wedding_id
and ra3.requirement_id = ra1.requirement_id
and ra3.answer_value = 'true');
Excel: Multiple rows with the same ID, add column for different variables
Please refer this, you can easily transform the data, as you are expecting using both Excel Formula
as well as using Power Query
Approach Using Excel Formula --> Applicable to Excel 2021 & O365 Users
Formula used in cell A15
=UNIQUE(A2:E5)
Formula used in cell F15
=TRANSPOSE(FILTER($F$2:$F$5,A15=$A$2:$A$5))
Approach Using Power Query
- Select the data and from Data Tab, Under Get & Transform Data Group, Click From Table/Range
- On Clicking above you shall be prompted with Create Table window, click the check box on my table as headers and press ok
- On doing above you shall be directed to power query editor
- Select the first 5 columns by holding down the shift key and press Group By from Home Tab, under Transform group or From Transform tab
- A new window opens as shown below, enter the new column name as All, and the Operation will All Rows press Ok
Next from Add Column Tab --> Click Custom Column and enter the below in custom column formula, and name new column name as Provincia
Table.Column([All],"Provincia")
- Click on the dropdown from the Provincia column and select extract values, press Ok
- Now again select the column Provincia and Click on Split Column From Home Tab, delimiter is comma, split at each occurrence of the delimiter and press ok, refer the image below
- Once the column is split it shall show like below, and now remove the unwanted column All
- Last but not least from Home Tab, Click Close & Load To
- From Import Data Window --> Select Table, you can check Existing worksheet or New worksheet, if checking Existing worksheet then select the cell where you want to place the data
- Expected Output as desired, this is one time process, and whenever you add new data just refresh the imported data, it updated within in few, you are not hardcoding anything here with just few simple steps, its done,
You can also paste this M-Language only change the Table Name as per your workbook
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"ID", "Targa", "Modello", "Lordo Chiusura", "Lordo Apertura"}, {{"All", each _, type table [ID=text, Targa=text, Modello=text, Lordo Chiusura=text, Lordo Apertura=text, Provincia=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Provincia", each Table.Column([All],"Provincia")),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Provincia", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Provincia", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Provincia.1", "Provincia.2", "Provincia.3", "Provincia.4"}),
#"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"All"})
in
#"Removed Columns"
Related Topics
How to Represent a Data Tree in SQL
Export Table Data from One SQL Server to Another
Generate Default Values in a Cte Upsert Using Postgresql 9.3
SQL Server Query Time Out Depending on Where Clause
SQL Server for Xml Path Make Repeating Nodes
Local Temporary Table in Oracle 10 (For the Scope of Stored Procedure)
Function-Based Indexes in SQL Server
Cumulative Total in Ms SQL Server
Oracle- Split String Comma Delimited (String Contains Spaces and Consecutive Commas)
SQL Server Select into @Variable
Checking If a String Is Found in One of Multiple Columns in MySQL
How to Retrieve the Current Value of an Oracle Sequence Without Increment It
Why Are Foreign Keys More Used in Theory Than in Practice
Using Alias in When Portion of a Case Statement in Oracle SQL