In Ms.Access 2010, I have a similar query table like one below where its displaying duplicate records. Problem is that even though I have unique ID's, one of the field has different data than other row since I have combined two seperate tables in this query. I just want to display one row per ID and eliminate other rows. It doesn't matter which row I pick. See below:
ID - NAME - FAVCOLOR
1242 - John - Blue
1242 - John - Red
1378 - Mary - Green
I want to just pick any of the the row with same ID. It doesn't matter which row I pick as long as I am displaying one row per ID is what matters.
ID - NAME - FAVCOLOR
1242 - John - Red
1378 - Mary - Green
Use the SQL from your current query as a subquery and then GROUP BY ID and NAME. You can retrieve the minimum FAVCOLOR since you want only one and don't care which.
SELECT sub.ID, sub.NAME, Min(sub.FAVCOLOR)
FROM
(
SELECT ID, [NAME], FAVCOLOR
FROM TABLE1
UNION ALL
SELECT ID, [NAME], FAVCOLOR
FROM TABLE2
) AS sub
GROUP BY sub.ID, sub.NAME;
Note NAME is a reserved word. Bracket that name or prefix it with the table name or alias to avoid confusing the db engine.
Try selecting union without the ALL parameter and see if you get the desired result.
Your new query would look like
"SELECT ID, NAME, FAVCOLOR FROM TABLE1; UNION SELECT ID, NAME, FAVCOLOR FROM TABLE2;"
If you just want the IDs, why is the color in the query? Maybe I'm missing something.
The only thing I could suggest is to use some aggregate function (min, max) to get one color.
Select
id,
name,
max(favcolor)
from (
(select * from table1) t1
union (select * from table2) t2 )t
group by
id,
name
Related
Hi I want to get all field
for example
select * from Stock where MemberId=3430
without same name value but I need Id
select distinct name,id,MemberId where MemberId=3430
Id is unique so distinct is not working correctly for me
Result is like
Id Name
1, Stock1
2, Stock2
3, Stock2
4, Stock1
It doesn't work because id is unique
You didn't specify the flavor of SQL that you're using. I've produced an answer for you that works in Postgres, SQL Server, and MySQL.
To be clear, my understanding of what you're looking for is the ability to return 1 record for "Stock2" with both of the id's associated with that value, rather than 2 separate records. To do this you need some sort of string aggregation capability. Here are three examples that will produce results you are looking for:
Postgres - dbfiddle link - https://www.db-fiddle.com/f/cWoFG13QYyi52Vww5HxoDi/1
SELECT array_agg(id), name
from sample
group by name;
SELECT array_to_string(array_agg(id), ','), name
from sample
group by name
MySQL
SELECT group_concat(id), name
from sample
group by name
SQL Server - sqlfiddle link - http://sqlfiddle.com/#!18/111ba7/4/0
SELECT string_agg(id, ','), name
from sample
group by name;
I have in database records
My sql:
SELECT
DISTINCT name, date(mod_wr)
FROM
test.object_stg
WHERE
ir = '4552724'
GROUP BY
name, date(mod_wr)
ORDER BY name
The last record is the same as the last but one. It has only a different date.
Is it possible to somehow query to return all records where there has been a change in the "name" column?
For record 4 and 5 there is the same name, only a different date. I would like it to return only a record of 4 and 5, because there was no change.
If you don't want to remove rows where values are resused. E.g. your line #2, you can use LAG() and then only include rows where the value is different to the previous. E.g.
select name, date(mod_wr) from
(
SELECT
name, mod_wr, lag(name) over(order by mod_wr) as prev_name
FROM
test.object_stg
WHERE
ir = '4552724'
)
WHERE prev_name IS NULL OR name <> prev_name
From your sample data, you have 3 distinct names. However, you cannot use distinct in your select statement because it applies to every field listed and none of the dates would provide an exact match.
However, you can use a group by statement in order to collate your titles together.
// MySQL 5.6 Statement
select name, date(mod_wr) from object_stg group by name;
// MSSQL 2017 Statement
select name, max(mod_wr) from object_stg group by name;
Both statements return 3 lines with just the BMW, 1.0 GL and 1.0 GLS showing with a single date.
SQL Fiddle
i need help concerning a sql query. first of all, i have a database with the following structure (example):
ID NAME VALUE
123 ABC_A Text Text Text
123 ABC_A Some more Text
123 ABC_A Even more Text
123 ABC_B some other text
now, i want to get all the different values of rows with the name "ABC_A". i tried to get those via group by and having, without success.
IS this what you want?
SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
but if the value of the ID and Name are unique then you can omit distinct (to avoid overkilling the server)
SELECT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
Additional to John, if you use the keyword Distinct you onle get DIFFERENT Values, therefore
SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND
Name = 'ABC_A'
i want to get all the different values of rows with the name "ABC_A"
This would be for example:
SELECT value, count(value) FROM tbl WHERE name = 'ABC_A' GROUP BY value;
If you do not need the count of times one value appears, remove it, or use DISTINCT:
SELECT DISTINCT value FROM tbl WHERE name = 'ABC_A';
If you want the different values of rows by ID also,
SELECT value, count(value)
FROM tbl
WHERE id = 123 AND name = 'ABC_A'
GROUP BY value;
Or if you want "ALL" the different values (with duplicates too) remove the GROUP BY (and you no longer can use the count(), which would be always 1):
SELECT value FROM tbl WHERE id = 123 AND name = 'ABC_A';
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
In above case why dont you use simple where clause
select * from <tableName> where name ='ABC_A'
Following problem:
I need to get the position of a record in the table. Let's say I have five record in the table:
Name: john doe, ID: 1
Name: jane doe, ID: 2
Name: Frankie Boy, ID: 4
Name: Johnny, ID: 9
Now, "Frankie Boy" is in the third position in the table. But How to get this information from the SQL server? I could count IDs, but they are not reliable, Frankie has the ID 4, but is in the third position because the record with the ID '3' was deleted.
Is there a way? I am aware of ROW_RANK but it would be costly, because I need to select basically the whole set first before I can rank row_rank them.
I am using MS SQL Server 2008 R2.
Tables don't have 'position'. Rows in a table (=set) are identified by their primary key value. Only result rows have 'position' which can be deterministic when a ORDER BY clause is present. Assuming that tables (=sets) have a position will lead only to problems and is the wrong mind set.
You can use row_number() to "label" rows. You've got to specify a way to order the rows:
select row_number() over (order by id) as PositionInTable
, *
from YourTable
If performance is an issue, you could store the position in a new column:
update yt1
set PositionInTable = rn
from YourTable yt1
join (
select row_number() over (order by id) as rn
, id
from YourTable
) yt2
on yt1.id = yt2.id
With an index on PositionInTable, this would be lightning fast. But you would have to update this after each insert on the table.
Tables are [conceptually] without order. Unless you specify ORDER BY in a select statement to order a results set, results may be returned in any order. Repeated executions of the exact same SQL may return the results set in different orders fro each execution.
To get the row number in a particular result set, use the row_number() function:
select row = row_number() over( order by id ) , *
from sysobjects
This will assign a row number to each row in sysobjects as if the table were ordered by id.
A simple way to do this without having to use ROW_NUMBER would be to simply count how many rows in the table have an index less or equal to the selected index, this would give the row number.
SELECT COUNT(*) FROM YourTable WHERE ID <= 4 -- Frankie Boy, Result = 3
This may not be the most efficient way to do it for your particular scenario, but it's a simple way of achieving it.
I have this situation in a certain table:
id | name
1 'Test'
2 'Test'
3 'Test'
How can I make a query to SELECT by distinct the name? I also need the ID column, even if I get the first occurrence of the element, e.g. "if the name column repeats, give me the first record with this repetition."
select name, MIN(ID)
from aCertainTable
group by name