Excuse me can anyone tell me how these two queries works?
1- SELECT last_name,department_id,salary FROM employees ORDER BY department_id,salary
2- SELECT last_name,department_id,salary FROM employees ORDER BY department_id,salary DESC
I'm trying to execute them on Oracle 10g Express Edition in user HR, the second one assumed to order the data according to the 'department_id' but it isn't !
Related
Is group by 1 a standard SQL?
The SQL like this:
select c_name, count(1) as number from table_one group by 1
This SQL is right in MySQL. But I want to know that can it work in SQL server or Oracle? I do not have SQL server or oracle service, please help me.
I have the following query which works great on SQL Server 2012:
SELECT Name,
WeekNumber,
SUM(NumberOfSecondsWorked) AS totalDaily,
StartTime,
EndTime,
SUM(SUM(NumberOfSecondsWorked)) OVER (PARTITION BY WeekNumber ORDER BY EndTime) AS totalWeekly
FROM #temp AS T1
But unfortunately I get the following error when running this query on a SQL Server 2008 DB:
Incorrect syntax near 'order'.
My desired outcome of the above query is to add the NumberOfSecondsWorked by Day for each week. Here is my desired output:
But without the ORDER BY, I just get a total for each week without the increment by day:
Anybody know how to run the above query in SQL Server 2008? Or a mechanism to get the same result? Thanks!
SQL Server 2008 doesn't support cumulative sums. My recommendation is to upgrade to a supported version of SQL Server.
That said, you can implement this using a correlated subquery or lateral join (i.e. apply):
SELECT t.*,
(SELECT SUM(t2.totalDaily)
FROM #temp t2
WHERE t2.WeekNumber = t.WeekNumber AND
t2.EndTime <= t.EndTime
) as running_weekly
FROM #temp t2
I suggest you to read the following article : https://dba.stackexchange.com/questions/47116/in-microsoft-sql-server-2008-syntax-generates-the-error-the-parallel-data-ware
It explains that what you want to do is not supported until SQL2012.
You can use the workaround suggested by the collegue but the best solution is to migrate to sql server version that supports what you want to do.
Bye,
Simone
SQL Fiddle is currently down regarding MS SQL Server code, so here is a dropbox link to a .txt containing the DDL to create the schema I'm using:
https://www.dropbox.com/s/6si4r37449q3ajb/DDL.txt?dl=0
I'm studying for an exam and I know there's a more efficient way to code this, I just don't know what it is.
5. Find out the department which has the highest number of personal computers installed.
select top(1) pc.location, count(pc.location) as number_of_comps_in_dept
from pc
group by location
order by number_of_comps_in_dept desc
My code featured above works, but what if I wanted to just get the department name (labeled location in this case)? I can't really call a single value back via my current code - which isn't friendly to procedures, functions, and triggers down the road.
Thank you for your help.
You can just remove the other columns in your SELECT statement. However, you need to replace the column in the ORDER BY clause with the aggregate:
select top(1) pc.location
from pc
group by location
order by count(pc.location) desc
ONLINE DEMO
Use result from subquery to get depratment name only
SELECT Dept_with_Max_Computers
FROM
(
select top(1) pc.location Dept_with_Max_Computers, count(pc.location) as number_of_comps_in_dept
from pc
group by location
order by number_of_comps_in_dept desc
) Z
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!
Actually, I have the task of finding the employees based on the salary rank.
So I used Dense_Rank() and got the answer.
Initially I was told to solve in SQL SERVER 2005.
Later on they changed the requirement and is saying that the query should run in SQL SERVER 2000 also.
I solved that using a while loop.
But they are saying that they will accept the solution using SET BASED approach.
How to do that?
Thanks in advance
Refer to this article, Ranking In SQL Server 2000
The author talks about how to implement Dense_Rank()
You may try something like this:
SELECT * ,
( SELECT COUNT(C1)
FROM T1
WHERE P1.C1>= T.C
) AS Rnk
FROM T
ORDER BY C DESC;