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'
Related
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 have a SQL table with "name" as one column, date as another, and location as a third. The location column supports null values.
I am trying to write a query to determine the number of times a null value occurs in the location column for each distinct value in the name column.
Can someone please assist?
One method uses conditional aggregation:
select name, sum(case when location is null then 1 else 0 end)
from t
group by name;
Another method that involves slightly less typing is:
select name, count(*) - count(location)
from t
group by name;
use count along with filters, as you only requires Null occurrence
select name, count(*) occurances
from mytable
where location is null
group by name
From your question, you'll want to get a distinct list of all different 'name' rows, and then you would like a count of how many NULLs there are per each name.
The following will achieve this:
SELECT name, count(*) as null_counts
FROM table
WHERE location IS NULL
GROUP BY name
The WHERE clause will only retrieve records where the records have NULL as their location.
The GROUP BY will pivot the data based on NAME.
The SELECT will give you the name, and the COUNT(*) of the number of records, per name.
I want to return all the rows from a table which are unique. I.e. if a certain field in two rows contain the same name, that name shouldn't be shown.
Since you want only the uniques names (and not an unique row for every names like you could have with DISTINCT), you have to use a GROUP BY and a HAVING (instead of a WHERE, because your parameter is the result of a function, not a variable) :
SELECT name FROM myTable GROUP BY name HAVING COUNT(name) = 1
SELECT DISTINCT column_name FROM table
If you want the complete rows, then use row_number() or distinct on:
select distinct on (name) t.*
from table t
order by name;
I have a complex query and which may return more than one record per group. There is a field that has a numeric sequential number. If in a group there is more than one record returned I just want the record with the highest sequential number.
I’ve tried using the SQL MAX function, but if I try to add more than one field it returns all records, instead of the one with the highest sequential field in that group.
I am trying to accomplish this in MS Access.
Edit: 4/5/11
Trying to create a table as an example of what I am trying to do
I have the following table:
tblItemTrans
ItemID(PK)
Eventseq(PK)
ItemTypeID
UserID
Eventseq is a number field that increments for each ItemID. (Don’t ask me why, that’s how the table was created.) Each ItemID can have one or many Evenseq’s. I only need the last record (max(Eventseq)) PER each ItemTypeID.
Hope this helps any.
SELECT A.*
FROM YourTable A
INNER JOIN (SELECT GroupColumn, MAX(SequentialColumn) MaxSeq
FROM YourTable
GROUP BY GroupColumn) B
ON A.GroupColumn = B.GroupColumn AND A.SequentialColumn = B.MaxSeq
If your SequentialNumber is an ID (unique across the table), then you could use
select *
from tbl
where seqnum in (
select max(seqnum) from tbl
group by groupcolumn)
If it is not, an alternative to Lamak's query is the Access domain function DMAX
select *
from tbl
where seqnum = DMAX("seqnum", "tbl", "groupcolumn='" & groupcolumn & "'")
Note: if the groupcolumn is a date, use # instead of single quotes ' in the above, if it is a numeric, remove the single quotes.
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