GROUP BY for SELECT one column - sql

If I am selecting one column, something like
SELECT columnname FROM table
will the result always be in the same order as
SELECT columnname FROM table GROUP BY columnname
or does it depend on the DB management system?

No, it would not be the same, because the first query may return duplicates, while the second query wouldn't.
This query, on the other hand, will return the same results as the group by one:
SELECT DISTINCT columnname FROM table
The order of the results may be different.

Its not the same, if you grup oyu will get only one record per columnname, the other you can have more than one.

The order is not defined by any of the queries you posted...
To set the order you must put the ORDER BY clause, like this:
SELECT columnname FROM table ORDER BY columnname;
The result may come into duplicated rows or not... it depends if you have duplicated values on your table. The GROUP BY clause will only select one of each kind, which could be set as it follows:
SELECT columnname FROM table GROUP BY columnname ORDER BY columnname;
Or, as Dasblink said earlier, using DISTINCT clause...
SELECT DISTINCT columnname FROM table ORDER BY columnname;
Note that you may use only ORDER BY and the argumment or put a ASC or DESC condition, so the order will come from the higher to lower value.

Related

How to pick first record from the duplicates, With only duplicate column values

Here is the situation where I have a table in bigquery like following.
As in the table we have record 1 and 3 with the same id but different first_name (Say the person with the id one changed his first_name) all other fields are same in both of the records (1 and 3) Now I need to select one records out of those 2 how can I do that. I tried self join but that is discarding both of the records, group_by will not work because the records is not duplicate only the Id is duplicate same with the distinct.
Thanks!!!!
The query I am using right now is
select * from table t group by 1,2,3,4,5;
You Can use ROW_NUMBER function to assign row numbers to each of your records in the table.
select *
from(
select *, ROW_NUMBER() OVER(PARTITION BY t.id) rn
from t)
Where rn = 1
ROW_NUMBER does not require the ORDER BY clause. Returns the sequential row ordinal (1-based) of each row for each ordered partition. If the ORDER BY clause is unspecified then the result is non-deterministic.
If you have record created date or modified dates you can use those in the ORDER BY clause to alway pick up the latest records.
SQL tables represent unordered sets. There is no first row unless you have a column that specifies the ordering. Let me assume you have such a column.
If you want a particular row, you can use aggregation with an order by:
select array_agg(t order by ? asc limit 1)[ordinal(1)].*
from t
group by id;
? is the column that specifies the ordering.
You can also leave out the order by:
select array_agg(t limit 1)[ordinal(1)].*
from t
group by id;

SQL Query Multiple Columns Using Distinct on One Column Only and Using Order By

I am trying to select all the distinct names from a column and order them by another column, sort_order.
I've tried several things:
select distinct ( name ), sort_order from table1 where active=1 order by sort_order
The above code outputs two columns, however, some repeat names have different sort_order values and still appear.
select name, sort_order from table1 where
name in (Select min(name) FROM table1 where active=1 group by sort_order )
The above code produces the error message:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
I tried replacing the order by with a group by, but this produces the list in the wrong order.
select distinct name as flavors from table1 where active=1 order by sort_order
The above code produces the error message:
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
I need the name column to display all distinct names and the sort_order column should display all the corresponding sort_order numbers (some may repeat).
Use aggregation:
select name, max(sort_order)
from z_mflavs
where active = 1
group by name
order by max(sort_order); -- or min() or avg()
Note that in your query, the parentheses around (name) are utterly superfluous. SELECT DISTINCT is a clause in the SQL language and it applies to all columns being selected, regardless of whether any are expressions in parentheses.
Use this query...if you want values of name, sort_order column
SELECT name, MAX(sort_order) as sort_order FROM table1
Group by name ORDER BY MAX(sort_order) DESC

Remove a column after selection with SQL

I want my result set to include only one column, but I'm using a different column to group by and order by.
Can I somehow, after selecting and order by removing the column from the result set?
Using MSSQL2008
Just add another SELECT around your query, like so:
SELECT
sum_columnB
FROM
(SELECT
columnA
, SUM(columB) sum_columnB
FROM Table
GROUP BY columnA
ORDER BY columnA
, sum_columnB) resultset
But if you would post your query, my answer could be more specific and maybe clearer.
You do not have to select all the columns your order or group by, you can just select the column you want.
SELECT A
FROM dbo.Table
GROUP BY A,B
ORDER BY A,B

SQL Order By using concat

I'm concatenating two fields and I only want to order by the second field (p.organizationname). Is that possible?
I'm displaying this field so I need a solution that doesn't include me having to select the fields separately.
Here is what i have so far:
SELECT distinct Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
FROM PEOPLE p,FOLDER f,FOLDERPEOPLE fp,folderinfo fi...
Order By concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
Use GROUP BY and ORDER BY an aggregate instead of DISTINCT:
SELECT Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
FROM PEOPLE p,FOLDER f,FOLDERPEOPLE fp,folderinfo fi...
GROUP BY Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
Order By MAX(p.ORGANIZATIONNAME)
The problem can be illustrated with an example:
ID Col1
1 Dog
1 Cat
2 Horse
Distinct ID? Easy: 1,2
Distinct ID Order by Col1... wait.. which value of Col1 should SQL use? SQL is confused and angry.
Since you are using a concatenation of two fields and want to sort by one of those fields, you could also include the sort field in a DISTINCT subquery and then ORDER BY the sort field without including it in your SELECT list.
Since you have a DISTINCT your ORDER BY clause should be specified in the SELECT, you can use a subquery to achieve the same result in your case since the Distinct values will be the same when you add P.ORGANIZATIONNAME
SELECT col
FROM( SELECT distinct Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME) a,
p.ORGANIZATIONNAME b
FROM PEOPLE p,FOLDER f,FOLDERPEOPLE fp,folderinfo fi... ) t
order by b

How can I resolve the distinct issue in SQL Server 2005?

I am trying to get distinct values for my query. I tried like below, but I am not getting proper result, will any one suggest me how to do resolve the issue.
Here the I want to distinct part_id.
http://tinypic.com/view.php?pic=9scx21&s=8#.UupFqT2SzyQ
Thanks in advance.
Why do you think the result is not correct, the rows returned are distinct.
DISTINCT is applied to all the columns, there's nothing like give me a DISTINCT(p.part_id) and don't care about other columns.
What you probably want is a single row for each part.id
If you don't have any rules which row you want to be returned you can go with a ROW_NUMBER:
select *
from
(
select all your columns
, row_number() over (partition by p.partid order by p.part_id) as rn
from ....
where ...
) as dt
where rn = 1
If there are some rules to determine which row should be returned (oldest/newest/whatever) you simply ORDER BY this column DESC instead of ORDER BY p.part
order by part_id;
Change SELECT DISTINCT P.PART_ID FROM.. at begining and add GROUP BY p.part_id at end.
Distinct must be applied for all columns which values are the same so you can add columns but remenber to add thet to GROUP BY also