I am trying to remove duplicates of multiple fields and the retain the original one. I am a newbie in MS Access, and don't know if this kind of thing is possible.
Here is my current data
id field 1 field2 field3 field4
1 mango mango apple apple
2 banana banana banana orange
3 grapes mango apple banana
And this is the desired output:
id field 1 field2 field3 field4
1 mango apple
2 banana orange
3 grapes mango apple banana
A UNION query can rearrange fields to records and eliminate duplicates.
SELECT ID, Field1 AS Data FROM Table1
UNION SELECT ID, Field2 FROM Table1
UNION SELECT ID, Field3 FROM Table1
UNION SELECT ID, Field4 FROM Table1;
Then that query can be source for a CROSSTAB to get similar to that desired output. The data will be alphabetically ordered on each row.
TRANSFORM First(Query1.Data) AS FirstOfData
SELECT Query1.ID
FROM Query1
GROUP BY Query1.ID
PIVOT DCount("*","Query1","ID=" & [ID] & " AND Data<'" & [Data] & "'")+1;
Related
I have table like this
id
name
1
Apple
1
Banana
1
Guava
2
Cassava
2
Carrot
2
Potato
3
Almond
3
Soybeans
3
Peanuts
I want to select only the first one from each id
id
name
1
Apple
2
Cassava
3
Almond
What's the query like?
you can try this way
SELECT id, name FROM table_name GROUP BY id ORDER BY id ASC;
You can achieve your goal by using row_number(). You can check my query in db-fiddle: https://www.db-fiddle.com/f/g4MrUTTTGDFfqjAKYnkFxn/1
WITH CTE as
(
SELECT id, name, ROW_NUMBER() OVER (ORDER BY null) as rowNumber FROM Fruits
)
SELECT id, name FROM CTE WHERE rowNumber IN(SELECT min(rowNumber) FROM CTE Group by id)
I have a table with data that looks like this:
ID Name Field2 Field3
1 Steve Value1 <null>
1 Steve <null> Value2
How do I merge these rows into one row that looks like this:
1 Steve Value1 Value2
The row count for ID/Name might vary.
select id, name, max(field2), max(field3)
from your_table
group by id, name
I have a table like following
TABLE_A
ID PERSON_ID NAME GRADE
---------- ---------- ---------- ----------
1 1 NAME_1 10
2 1 NAME_1 20
3 2 NAME_2 30
4 2 NAME_2 40
...
in this table, for each name there is exactly two rows (two grades).
I want to make a query which results like following
RESULT
PERSON_ID NAME GRADE1 GRADE_2
---------- ---------- ---------- ----------
1 NAME_1 10 20
2 NAME_2 30 40
What is the best way for this.
I can use self join but I think this is not correct method
You can use GROUP BY as the other person suggested.
Or you can make a join.
select t1.person_id, t1.grade as grade1, t2.grade as grade2
from TABLE_A t1 join TABLE_A t2 on t1.person_id=t2.person_id and t1.id!=t2.id
This JOIN joins all the rows with the same person, but not the rows with the same id so you filter out the duplicates.
If you are using MySQL, and if all names have exactly two records, and the first grade is the one with the lower id and the second grade the other, then you could use a query like this one:
select
person_id,
name,
group_concat(grade order by id) as grades
from
table_a
group by
person_id,
name
then if you want to separate grades into two new columns you could use SUBSTRING_INDEX:
select
person_id,
name,
substring_index(group_concat(grade order by id), ',', 1) as grade1,
substring_index(group_concat(grade order by id), ',', -1) as grade2
from
table_a
group by
person_id,
name
Try this for SQL SERVER
; WITH CTE AS(
SELECT PERSON_ID, NAME, GRADE AS GRADE_1,
LEAD(GRADE) OVER(PARTITION BY NAME ORDER BY NAME) AS GRADE_2
FROM TABLE_A
)
SELECT * FROM CTE
WHERE GRADE_2 IS NOT NULL
RESULT SET:
PERSON_ID NAME GRADE_1 GRADE_2
---------- ---------- ---------- ----------
1 NAME_1 10 20
2 NAME_2 30 40
As soon as the topic starter didn't mention the ORDER of grades and "there is exactly two rows (two grades)" I think the easiest way:
SELECT PERSON_ID,
MAX(NAME) as NAME,
MIN(GRADE) as GRADE1,
MAX(GRADE) as GRADE2
FROM TABLE_A
GROUP BY PERSON_ID
I found best answer for PostgreSQL in this link
https://www.postgresql.org/docs/9.2/static/tablefunc.html
In PostgreSQL there is the crosstab(text) function.
The crosstab function is used to produce "pivot" displays, wherein data is listed across the page rather than down. For example, we might have data like
row1 val11
row1 val12
row1 val13
...
row2 val21
row2 val22
row2 val23
...
which we wish to display like
row1 val11 val12 val13 ...
row2 val21 val22 val23 ...
...
The crosstab function takes a text parameter that is a SQL query producing raw data formatted in the first way, and produces a table formatted in the second way.
The sql parameter is a SQL statement that produces the source set of data.
This statement must return one row_name column, one category column, and one value column. N is an obsolete parameter, ignored if supplied (formerly this had to match the number of output value columns, but now that is determined by the calling query).
For example, the provided query might produce a set something like:
row_name cat value
----------+-------+-------
row1 cat1 val1
row1 cat2 val2
row1 cat3 val3
row1 cat4 val4
row2 cat1 val5
row2 cat2 val6
row2 cat3 val7
row2 cat4 val8
The crosstab function is declared to return setof record, so the actual names and types of the output columns must be defined in the FROM clause of the calling SELECT statement, for example:
SELECT * FROM crosstab('...') AS ct(row_name text, category_1 text, category_2 text);
This example produces a set something like:
<== value columns ==>
row_name category_1 category_2
----------+------------+------------
row1 val1 val2
row2 val5 val6
For more read section F.35.1.2. in URL entered top of the answer
just think I have a table1 like shown below
table1
id product
2 chocolate
1 chocolate
2 pepsi
3 fanta
2 pepsi
4 chocolate
5 chips
3 pizza
1 coke
2 chips
6 burger
7 sprite
0 pepsi
and want to arrange the above table in the manner shown below using only mysql
table2
id product
0 pepsi
1 chocolate, coke
2 chocolate,fanta,chips
3 fanta,pizza
4 chocolate
5 chips
6 burger
7 sprite
the above thing can be done by using
select id, group_concat(distinct product) as products
from table1
group by id
order by id
and after that i want the product column from table2 to be updated in another table which is named table3, which is shown below, here i want to update the products column from table2 as things column in table3 and the number in table3 = id from table2
table3
number name things
0 hi null
1 hello null
2 hehe null
3 wow null
4 hi null
5 hi null
6 hi null
7 hi null
and i want the final output to be shown as in table2
table3
number name things
0 hi pepsi
1 hello chocolate, coke
2 hehe chocolate,fanta,chips
3 wow fanta,pizza
4 hi chocolate
5 hi chips
6 hi burger
7 hi sprite
Use update from select syntax. Check here for more info
UPDATE table3
JOIN (SELECT id,
Group_concat(DISTINCT product) AS products
FROM table1
GROUP BY id) b
ON table3.number = b.id
SET table3.things = b.products
You have a nice normalized data structure and you want to start putting in comma-separated lists. That is a bad idea. Doable, but probably a bad idea.
You would use an update with a join:
update table3 t3 join
(select id, group_concat(distinct product) as products
from table1
group by id
) tt
on t3.number = tt.id
set t3.things = tt.products;
I want to merge rows in SQL server as follows.
Table
PK F1 F2 F3 Order
A NULL NULL Grapes 2
B NULL Fig NULL 1
C Apple Orange Banana 0
The input and the expected result is as follows.
Input Expected Result
2 Apple Fig Grapes
1 Apple Fig Banana
0 Apple Orange Banana
How can I do this in sql stored procedure? I prefer to do in Sql other than in code.
Thanks in Advance
If I understand you input right then maybe something like this:
Test data
DECLARE #T TABLE
(
PK VARCHAR(5),
F1 VARCHAR(10),
F2 VARCHAR(10),
F3 VARCHAR(10),
[Order] INT
)
INSERT INTO #T
VALUES
('A',NULL,NULL,'Grapes',2),
('B',NULL,'Fig',NULL,1),
('C','Apple','Orange','Banana',0)
Query
;WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY [Order]) AS RowNbr,
T.*
FROM
#T AS T
)
, CTE2
AS
(
SELECT
CTE.RowNbr,
CTE.PK,
CTE.F1,
CTE.F2,
CTE.F3,
CTE.[Order]
FROM
CTE
WHERE
CTE.RowNbr=1
UNION ALL
SELECT
CTE.RowNbr,
CTE.PK,
ISNULL(CTE.F1,CTE2.F1),
ISNULL(CTE.F2,CTE2.F2),
ISNULL(CTE.F3,CTE2.F3),
CTE.[Order]
FROM
CTE
JOIN CTE2 ON CTE.RowNbr=CTE2.RowNbr+1
)
SELECT
CTE2.[Order],CTE2.F1,CTE2.F2,CTE2.F3
FROM
CTE2
ORDER BY
CTE2.RowNbr DESC
Result
2 Apple Fig Grapes
1 Apple Fig Banana
0 Apple Orange Banana