Arrange cities based on their ranking [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
I need to display ranking along with city based on city selection.
Suppose if someone select Nagpur, we need to show the predecessor city, selected city and followed by successor city with their respective ranking.
Criteria
If someone selects Mumbai then, No Data, Mumbai-1, Delhi-2
If someone selects Delhi then, Mumbai-1, Delhi-2, Lucknow-3
If someone selects Nagpur then, Lucknow-3, Nagpur-3, Pune-3 (based on city's sorting order alphabetically, if there are multiple cities having same ranking)
If someone selects Kanpur then, Patna-7, kanpur-8, No Data.
The data is in this format:
Can someone help to achieve this? I am using SQL Server 2016

It may not be the best solution, but it will be useful for now. I recommend that you always add an id to the tables and the names of the fields can be easily identified to which table they belong
DROP TABLE #TableTempRankingCity
DECLARE #Id INT;
SELECT ROW_NUMBER() OVER(
ORDER BY [City]) AS fila, *
INTO #TableTempRankingCity
FROM RankingCiudades
ORDER BY Rank,City
--SELECT * FROM #TableTempRankingCity
SELECT #Id = fila
FROM #TableTempRankingCity
WHERE City = 'Lucknow'
SELECT
City
,Rank
FROM #TableTempRankingCity
WHERE fila IN (#Id-1,#Id+1,#Id+2)

Related

Hi guys i'm an SQL beginner and i'm having a hard time understanding it could u explain this line for me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
SELECT DISTINCT CITY FROM STATION WHERE MOD(ID,2)=0 ORDER BY CITY ASC;
SELECT DISTINCT CITY FROM STATION
Primary command to select data from the database. It is asking to select CITY from the table STATION whose data is unique. Thus, no duplicates are produced in result.
WHERE MOD(ID, 2) = 0
Only select those that have an even number ID.
ORDER BY CITY
Sort the results with respect to CITY names.
ASC
Sort in ascending order; which means that cities with names starting with A will come before those that have Z as first letter.

SQL bring number of count, another column basis problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this table:
BANK - BRANCH_NAME - ADDRESS - DISTRICT - CITY - PHONE - FAX - OPENNING DATE
How do I bring branches on a city basis?
What I expect is a city knows how many branches it has.
From the comments:
what I expect is a city knows how many branches it has
You can use aggregation:
select city, count(*) no_branches from mytable group by city
If (city, branch) tuples are not unique in the table, then:
select city, count(distinct branch) no_branches from mytable group by city

SQL Server Temp Table to a Select Distinct Count Distinct quetsion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Ok, basically I've got a lot of temp tables I've created and I'm trying to create Validation for the ProvDiff table.
DROP TABLE #ProvDiff;
IF OBJECT_ID ('temp.dbo.#ProvDiff') IS NOT NULL
DROP TABLE #ProvDiff;
SELECT *
INTO #ProvDiff
FROM
(SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]) ProvDiff;
SELECT DISTINCT COUNT(DISTINCT ???) AS 'Unique EI NPIS'
FROM #ProvDiff
In my head it seems like the differences should be able to produce a result and I should be able to do a count on that. But for the life of me I can't figure out how to do that. If I do a count on rendering or pay to then those numbers wouldn't necessarily reflect the value for what are above. I know how many of each are produced for above validation.
Any help would be greatly appreciated
Is this what you want?
SELECT COUNT(*)
FROM (SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]
) ProvDiff;
I don't see why a temporary table would be used for this.
For better or worse, SQL Server does not support select count(distinct *), so you pretty much need a subquery.

How can I apply distinct on multiple columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi friends I have table from customer where in I want to get distinct values of its three columns CustomerType, CustomerReg,CustomerID.
Instead of doing three separate queries for the distinct
like select distinct CustomerType From Customer.. and then for CustomerReg..Can it be achieved in one query...
The reason is that i would attach each column distinct values to a specific drop down list box..
You can do this with grouping sets. If you want the values in three separate columns:
select CustomerType, CustomerReg, CustomerId
from Customer
group by grouping sets ((CustomerType), (CustomerReg), (CustomerId))
If I understand correctly, you want to show a distinct list of all the customer types, customerreg and customerIds in a list, in one column?
Try this...
select distinct CustomerType + ' : ' + CustomerReg + ' (' + CustomerId + ')' as Name
from Customer
This will return a string like 'External : 23423412 (2344)'
You should probably order it by something meaningful too.
Try adding
order by Name
Although, you shouldn't need the DISTINCT if a customer can only appear once in the customer table.
Reading your question again, it looks like you want to return a distinct list of each column in one query, not a distinct combination? Then the group by grouping sets mentioned above will probably get you the closest, although depending on the structure of your data, performance might become an issue here if you have lots of customers.
What language are you using in your UI?
If it's .Net, you could open a datareader using 3 separate queries, and then use the datareader.nextresult
There's an explanation here,
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.data.framework.datareader.nextresult.aspx
and an example here,
http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx
Hope that helps
You can even do it like this:
select distinct CustomerType, CustomerReg, CustomerId
from Customer

find student name less than 50 attendance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a student table where column is name, attendance, and date in which data is entered everyday for the students who attend the class. For example if a student is absent on a day, entry is not made for that particular student for that day.
Finally. I need to find out students name whose attendance less than 50.
You can use GROUP BY and HAVING statements for this.
SELECT name FROM student GROUP BY name HAVING COUNT(*) < 50;
Please note that above query is not tested.
You have to use GROUP BY clause to aggregate similar student in table and HAVING check your condition, to get your desired output.
SELECT name, count(name)
FROM student
GROUP BY column_name
HAVING count(name)<50;
I hope this will help to solved your problem.
SELECT name
FROM StudentsTable
WHERE COUNT(name) < 50