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
Related
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;
I have a problem much like this one
SQL: selecting rows where column value changed from previous row
Although not in mysql in SQL Server. i tried ypercube's first answer jiri's answer and egor's as well. All of them just run for over 5 min with no results (one i let run over 10min). The table contains over a million records so i know this is a big part of the problem. I have a feeling ypercube's second answer might work well but don't know how to change this variable driven mysql query to SQL.
Any help would be appreciated.
SQL version 2008 r2
Basically i need to determine when a price has changed on table containing the price,productID, serialnumber and a datestamp.
I can get a quick list of what productids/serial numbers need to be checked to compare against. Sorry i did not include this earlier i was thinking i could just adapt a solution to fit it.
A common table expression should do it fairly efficiently;
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY system ORDER BY timestamp) rn
FROM TableX
)
SELECT a.timestamp, a.system, a.statusa, a.statusb
FROM cte a JOIN cte b ON a.system = b.system AND a.rn = b.rn+1
WHERE a.statusa <> b.statusa
An SQLfiddle to test with.
How can I use TOP function in SQL Server 2005 on a single column in a table along with count function?
I am getting only one count for this query, where I have 35 entries that should come.
this is my query
select top(1) room_no, count(room_no) from rooms
Seems like what you want is the following:
select room_no,count(room_no)
from rooms
group by room_no
BTW, I wonder why it would execute without the group by. Should throw an error.
COUNT function is evaluated after TOP. That is why you are only getting one for the count. What you want is something like this
SELECT TOP(1) dbo.Table.Column1, (SELECT COUNT(*) FROM dbo.Table)
FROM dbo.Table
Also as mentioned you really should be ordering by something.
Edit: This also works:
SELECT TOP(1) dbo.Table.Column1, COUNT(*) OVER() AS Total
FROM dbo.Table
Seems to be more efficient as well (only tested on small dataset though).
I have an existing app I can’t modify. It needs to execute a SQL GROUP BY, but cannot. However it can and does read a GroupNumber field from the same table.
What I’m doing now is executing the grouping SQL statement, processing it in code and writing back the GroupNumber to the table so that App can do its thing. What I’d like to do is execute a single SQL statement to do both the grouping and the writeback in a single step. I can’t figure out how to do this, if indeed it’s possible. Simple example:
SELECT FirstName, LastName, Age
FROM Persons
WHERE ....
GROUP BY Age
ORDER BY Age
I execute this, then do
for ( i = 1; i <= result_set.n; i++ )
Sql = “UPDATE Persons
SET GroupNumber = “ + fixed( i )
+ “WHERE Age = “ + fixed( result_set.Age[i] )
I need to do this every time a record gets added to the table (so yes, if someone younger than me gets added, my group number changes - don’t ask).
Clearly you want a trigger. However trigger definitions vary from database server to database server. I'll hazard a guess and say you are using some version of Microsoft SQL Server: the create trigger syntax and a couple of examples can be found at http://msdn.microsoft.com/en-us/library/ms189799.aspx. There might be some small complication with the trigger modifying the same table it is sourcing data from, but I believe you can generally do that in most SQL server databases (SQLite may be one of the few where that is difficult).
Try that and see if that helps.
I'm not really sure what you are after, here is my best guess:
;WITH AllRows AS (--get one row per age, and number them
SELECT
Age, ROW_NUMBER() OVER (PARTITION BY AGE ORDER BY Age) AS RowNumber
FROM Persons
WHERE ...
GROUP BY Age
)
UPDATE p --update all the people, getting their GroupNumber based on their Age's row number
SET GroupNumber=a.RowNumber
FROM Persons p
INNER JOIN AllRows a ON p.Age=a.Age
WHERE GroupNumber IS NULL OR GroupNumber!=a.RowNumber
I use SQL Server, but this is fairly standards based code.
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;