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;
Related
I have the following query generated in a Cognos report.
My problem is I need it to work, with the same logic / filter in SQL Server. Can the filter below (COGNOS syntax) be generated to work in the same way, like a WHERE in SQL Server?
select
*
from
dbo.ia_code
group by
client__iacode.ia_code,
client__iacode.ia_short_descr
------ here my problem
filter
(rank() over ( at client__iacode.ia_code order by XCOUNT(client.client_code at client__iacode.ia_code,client.client_id for client__iacode.ia_code ) desc nulls last) <= 25) and
(RCOUNT(rank() over ( at client__iacode.ia_code order by XCOUNT(client_document.client_document_id for client__iacode.ia_code ) desc nulls last) at client__iacode.ia_code order by rank() over ( at client__iacode.ia_code order by XCOUNT(client_document.client_document_id for client__iacode.ia_code ) desc nulls last) asc,client__iacode.ia_code asc,client__iacode.ia_short_descr asc ) <= 25)
Any help would be appreciated.
Use where before group by for filter
Group by just use for aggregate-function call in query like below
select
Sum(id),client__iacode.ia_code,client__iacode.ia_short_descr
from
dbo.ia_code
group by
client__iacode.ia_code,
client__iacode.ia_short_descr
Use having for functionality filter in query
Read this statement for how to use :
Where : w3schools
Group by: w3shoolcs
Having : w3shoolcs
in finally :
I tried change your code to sql server but i don't now about out put of this code. please add table sample data and sample of out put.
First try changing the query's (or data source's) "Rollup Processing" property to "Database". That should help converting extended aggregate functions (XCOUNT etc) to native SQL. Also check out "Use SQL parameters" property and set it to "Literal" and see if that will helps with the parameters in native SQL. Once you do those, paste the new generated native query here.
Screenshot of the properties window
This is a snippet from my answer to the following Stackoverflow question. Read more details there and also follow that question as it may help with your question too.
Convert IBM Cognos SQL which contains a filter to Microsoft SQL Server Query
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 must perform this operation under oracle 9 and sql server 9
I have on table with images inside and it can be quite heavy.
I want to select the ten first rows then the ten next etc... until the end of the table.
If possible I would like to have the same request to do that for oracle and sql server for maintenance reasons. I saw things like rownum and stuff but I didn't understand how it works and if it suits what I have.
Thx for your help.
You can do the following in both Oracle and SQL Server:
with cte as (
select t.*, row_number() over (order by id) as seqnum
from table t
)
select cte.*
from cte
where seqnum between X and Y;
You need to plug in the values of X and Y for each call.
This assumes that SQL Server 9 really means SQL Server 2005+. I don't recognize SQL Server 9 as a valid version (see here for a list of versions).
We cannot use TOP command in SQL in ORACLE to get the top n sorted rows,as it doesn't support it. But there are ways of getting the result using rank() and rownum() but that requires and inner query. I want to know if there is any other way apart from using rownum()/rank() with inner query.
I don't know what "rownum()" is. The best way is the answer that #PinnyM deleted . . .
select *
from (<your query here>
order by <your ordering>
) t
where rownum < xx
This is much better than using row_number() or rank(), because rownum requires minimal processing for the value.
By the way, TOP is specific to SQL Server and Sybase. Other databases using LIMIT.