SQL Query to select one field order by other field - sql

I want to select a field in select statement and order by with another field but sql server doesn't allows this as it says order by item must appear in the select statement if select distinct is specified.
This is what I tried :
select DISTINCT format_type
from Labels_Add_Label
where external_group_id= 2826
order by group_sequence
What changes are required to do in this query?
Please provide the changed query

You can rewrite your query this way (equivalent to distinct):
SELECT format_type
FROM Labels_Add_Label
WHERE external_group_id= 2826
GROUP BY format_type;
and you can't use ORDER BY group_sequence here. There may be more than one row with same format_type but different group_sequence. SQL server doesn't know which one should be used for the ordering.
You can however use aggregate functions with a GROUP BY query:
SELECT format_type
FROM Labels_Add_Label
WHERE external_group_id= 2826
GROUP BY format_type;
ORDER BY MIN(group_sequence) ; -- or MAX(group_sequence)

I just took this question to test my knowledge and have come with the following solution (using CTE in MS SQL Server), please correct me if I'm wrong - Using the NORTHWIND Database (Employees Table) on MS SQL Server, I have written this query - This could be one other option that could be used, if there be a need to!
WITH CTE_Employees(FirstName, LastName, BirthDate)
AS
(
SELECT FirstName, LastName, BirthDate
FROM Employees
WHERE Region IS NOT NULL
)
SELECT FirstName FROM CTE_Employees ORDER BY BirthDate DESC
As mentioned above, there can be a same Employee FirstName but with a different LastName, hence SQL Server imposes a condition where we can't use DISTINCT in conjunction with ORDER BY...
Hope this helps!

Related

Oracle query mistake

I need to know where the mistake is in this oracle query?
SELECT(KEY1),COUNT(*) FROM TABLE1 GROUP BY AGE
SELECT KEY1,COUNT(*) FROM TABLE1 GROUP BY KEY1
There are two problems. First one: You cannot close the parenthesis after the first keyword. Second: You have to group by all keys that are in the query that are not all row dependend. In that case "KEY1". If you want to order by age you have to query age as parameter.
SELECT AGE,COUNT(*) FROM TABLE1 GROUP BY AGE
Your table naming is not very good. I assume you should have a look at group by tutorials like https://www.w3schools.com/sql/sql_groupby.asp or the sql tutorial https://www.w3schools.com/sql/
Your query had an issue. You have to modify your query as below
SELECT KEY1,COUNT(*) FROM TABLE1 GROUP BY KEY1.
Observation:
All the columns that are added in the select statement alongside the aggregate functions, should be included the group by columns.
Your first column does have the bracket in it which should be removed.

Group By with count doesn't work

I am trying to use group by with count function but this doesn't work.
SELECT
projects.AgencyId,
projects.ProgramId,
count(projects.ProjStatusByMin) as status,
projects.ProjStatusByMin
from
projects
where
projects.AgencyId=40
group by
projects.ProjStatusByMin
This above code works in MySQL perfectly now i want to achieve the same thing in SQL Server.
Select
ProjStatusByMin,
COUNT(ProjStatusByMin) [projstatus]
from
Projects
where
AgencyId=40
group by
ProjStatusByMin,AgencyId,ProjId
However if i select a single column like the bellow code then group by work
Select
ProjStatusByMin,
COUNT(ProjStatusByMin) [projstatus]
from
Projects
where
AgencyId=40
group by
ProjStatusByMin
Now how can I achieve group by with multi column selected?
You have to add all the columns (except for the one in the count function) to the group by clause.
SELECT projects.AgencyId,projects.ProgramId,count(projects.ProjStatusByMin) as status,
projects.ProjStatusByMin
from projects
where projects.AgencyId=40
group by projects.ProjStatusByMin, projects.AgencyId,projects.ProgramId
In Sql Server you have to write all the column names in group by clause which you are going to select in the select statement.

T-SQL Query to SELECT rows with same values of several columns (Azure SQL Database)

I need help with writing a T-SQL query on a table shown on the picture below. The table has ambiguous info about buildings, some of them appears more then one time, that is wrong. I need to select only rows that has the same street and building values, for I can manually delete bad rows then. So I want to select rows 1,2,4,5 on the picture below. I use an Azure SQL Database, it has some limitations on T-SQL.
I'm pretty sure Azure supports subqueries and window functions. So, try this:
select t.*
from (select t.*, count(*) over (partition by street, building) as cnt
from table t
) t
where cnt > 1;

How to SELECT columns without including them in GROUP BY access sql

My sample sql query
SELECT EID,p,p1,p2,p3 FROM table 1 GROUP BY EID;
Giving error not part of aggregate function.I wanted to group by only EID not all other p,p1,p2,p3. How do i specify that in sql query.
In most dialects of SQL, you have to specify which column you want, if the column is not in the group by clause. For instance, maybe you want the minimum value:
SELECT EID, min(p), min(p1), min(p2), min(p3)
FROM table 1
GROUP BY EID;
Or, if you wanted all the values from a particular record, use first or last:
SELECT EID, first(p), first(p1), first(p2), first(p3)
FROM table 1
GROUP BY EID;

Why shouldn’t you use DISTINCT when you could use GROUP BY?

According to tips from MySQL performance wiki:
Don't use DISTINCT when you have or could use GROUP BY.
Can somebody post example of queries where GROUP BY can be used instead of DISTINCT?
If you know that two columns from your result are always directly related then it's slower to do this:
SELECT DISTINCT CustomerId, CustomerName FROM (...)
than this:
SELECT CustomerId, CustomerName FROM (...) GROUP BY CustomerId
because in the second case it only has to compare the id, but in the first case it has to compare both fields. This is a MySQL specific trick. It won't work with other databases.
SELECT Code
FROM YourTable
GROUP BY Code
vs
SELECT DISTINCT Code
FROM YourTable
The basic rule : Put all the columns from the SELECT clause into the GROUP BY clause
so
SELECT DISTINCT a,b,c FROM D
becomes
SELECT a,b,c FROM D GROUP BY a,b,c
Example.
Relation customer(ssnum,name, zipcode, address) PK(ssnum). ssnum is social security number.
SQL:
Select DISTINCT ssnum from customer where zipcode=1234 group by name
This SQL statement returns unique records for those customer's that have zipcode 1234. At the end results are grouped by name.
Here DISTINCT is no not necessary. because you are selecting ssnum which is already unique because ssnun is primary key. two person can not have same ssnum.
In this case Select ssnum from customer where zipcode=1234 group by name will give better performance than "... DISTINCT.......".
DISTINCT is an expensive operation in a DBMS.