Why I got Not a single-group group function in sql [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 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.

Related

oracle sql 12 how to display the number of current account is not displaying , any suggestions please?the query and table below [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 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.

SQL command not properly ended union [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 1 year ago.
Improve this question
Here is my error message:
This is the text
Oracle requires parentheses for the subqueries:
(select TeamName As Name, points As points
from standing
group by TeamName, points
having Length(TeamName) >= 3 and points > 5
order by points DESC, TeamName ASC
Fetch first 2 rows only
) union
(select TeamName As Name, points As points
from standing
group by TeamName, points
having Length(TeamName) >= 3 and points > 5
order by points ASC
Fetch first 1 rows only
)
The parentheses are needed when the subqueries have either order by or fetch clauses. They are needed to distinguish between applying those clauses to an individual subquery or to the results of the entire union.
Also, you might want union all instead of union -- union incurs overhead for removing duplicates.

sql generate group on related groups [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
Sorry, if question unclear, i had misstakes it first tables. I made some updates:
Database: PostgreSQL
I want to group table based on transition (if a=b & b=c then a=c)
Adding a pair (4,c) will merge 2 groups to one "group1".
i assume u want a.b.c to be group1 and the d as group2..
the groupby will work perfectly fine with aliases..
but the number of group op wants is 3 millions groups so a stored proc with a incremental in the end and group by will work fine..
From your comments, it looks like you want to find out transitive relationship.
You can do that with following query. But if the goal here is just to identify the relationship among different groups with their respective id, i guess you can afford to have groups which are not getting incremented with 1.
According to your given example in OP, i think it won't affect you if end result has group1 and group5 instead of group2.
If mention result is fine then you can do that with following updated query. Giving group names in successive manner will impact on query performance which you don't want as you've 3 million of groups.
Please try following query:
select t1.id, concat('group', min(t2.minId)) groups
from t1
join
(select min(id) minId, groups
from t1
group by groups
) t2
on t1.groups = t2.groups
join (select #cnt := 1)y
group by t1.id;
Demo : Click here

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.

SQL Server SELECT first [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
I am trying to create a JOIN with 2 tables and having ce first element of the joined table.
My tables look like this :
Product
id
name
Sales
idProduct
prices
date
I want to have the last sales price for each product but the function FIRST doesn't exist in SQL Server.
Someone have an idea ?
Thanks,
You can use a ranking function like ROW_NUMBER:
WITH CTE AS(
SELECT id, name, idProduct, prices, date,
RN = ROW_NUMBER() OVER (PARTITION BY idProduct ORDER BY date DESC)
FROM dbo.Product p INNER JOIN dbo.Sales s on p.id = s.idProduct
)
SELECT * FROM CTE WHERE RN = 1
Ranking Functions (Transact-SQL)
The CTE is a common-table-expression similar to a sub-query but more readable and maintainable.
If it's SQL Server, simply use:
SELECT TOP 1 *
FROM Product p
JOIN Sales s ON p.id = s.idProduct
ORDER BY s.Date DESC