Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
The task sounds like this:
Choose the top 10 cities for the next store opening
Columns: City | A priority
Priority is defined as the number of buyers in the city
There should be no shop in the city.
Preliminary wrong decision:
SELECT
City,
COUNT(1) as [Priority]
FROM
Sales.vIndividualCustomer
GROUP BY City
EXCEPT
SELECT
City,
COUNT(1) as [Priority]
FROM
Purchasing.vVendorWithAddresses
GROUP BY City
ORDER BY [Priority] DESC
GO
Result:
Uniqueness disappears as soon as I start counting priority. Maybe there is another way?
PS: Used as a database AdventureWorks2016 from Microsoft.
You can use a NOT EXISTS subquery
SELECT
ic.City,
COUNT(1) as [Priority]
FROM
Sales.vIndividualCustomer ic
GROUP BY
ic.City
HAVING NOT EXISTS (SELECT 1
FROM
Purchasing.vVendorWithAddresses va
WHERE
va.City = ic.City)
ORDER BY [Priority] DESC;
Note that if you wish to refer to aggregated columns then the NOT EXISTS will have to be in a WHERE not a HAVING
Related
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)
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 1 year ago.
Improve this question
select distinct b.brid,
(select count(1) from account a
where a.brid= b.brid
and a.acctype='current'
) as num_accounts,
b.baddress.street, b.baddress.city, b.baddress.p_code
from branch b;
the table should display the number of current account,but nothing is showing
Query you posted doesn't make sense. What is b.baddress.street? The way you put it,
b is user
baddress is table
street is column
but what is b.brid, then?
You posted screenshot of ... what? Desired result? Can't be as it displays 2 columns, while query returns 5 of them.
Anyway: this is how it might be done. Try to adjust it to your table(s). Mind letter case (is it really 'current'? Maybe 'CURRENT'? Or ...?)
SELECT b.brid,
COUNT (*) num_accounts,
b.street,
b.city,
b.p_code
FROM branch b JOIN account a ON a.brid = b.brid
WHERE a.acctype = 'current'
GROUP BY b.brid,
b.street,
b.city,
b.p_code;
You can use a correlated subquery for this purpose:
select b.*, -- or whatever columns you want
(select count(1)
from account a
where a.brid = b.brid and a.acctype = 'current'
) as num_accounts
from branch b;
In other words, you appear to have an error in how your are referencing columns in b, but the actual part of the query that does the count is correct.
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.
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 7 years ago.
Improve this question
select
author.name,
max(count(paper.pid))
from
paper,
author
where
paper.aid=author.aid
group by
author,.name
Max of count does not make sense. Do you mean this:
select top 1 name, paper_count
from
(
select author.name,count(paper.pid) as paper_count
from paper
join author on paper.aid=author.aid
group by author.name
)
order by paper_count desc
*this answer assumes sql server.
or (as Giorgos Betsos points out:)
select top 1 author.name,count(paper.pid) as paper_count
from paper
join author on paper.aid=author.aid
group by author.name
order by paper_count desc
http://sqlfiddle.com/#!6/7ed37/3
(Some SQL platforms won't let you order by the results of an agg function but SQL server does.)
It looks like you have a comma before "name" in your group by statement. Also, max(count()) doesn't really work or make sense. Try the below to get the author with the greatest number of papers:
select *
from(
select author.name,count(paper.pid)
from paper,author
where paper.aid=author.aid
group by author.name
order by count(paper.pid) desc
)
where rownum=1
And here is the sqlfiddle showing this in action: fiddle. Jane has 2 papers and John has 3. John shows in the results.
Alternatively you can do rownum <= x where you want the top X values.
This is an Oracle 11g solution. If using some other brands of sql you'll probably want to use Top 1/Top x I believe.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a table consisting of the following columns:
billid, patientid, doctorid, fees
How do i display the doctors who treat more than one patient?
TRIED THE FOLLOWING CODE and got it.
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
Thanks :)
SELECT doctorID
FROM YourTable
GROUP BY doctorID
HAVING COUNT (DISTINCT patientid) > 1
These are basic SQL queries. If you have trouble with something like this, you should really get to some SQL tutorial or book first.
select doctorid, count(patientid) from table1 group by doctorid having COUNT (DISTINCT patientid) > 1 ;
This will show you the doctor list having more than 1 distinct patient
on the given information if you will just select doctorid who is treating more than one patient with this query
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
and then you can use that doctorid in rest of your operations