How to combine select max and count? - abap

I've got this:
select ordernr
from users
having count(ordernr) =
( select max(count(ordernr))
from users where ordernr = ordernr
group by ordernr )
group by ordernr
to get the most used order-number (ordernr) from all users.
How to get it into ABAP SAP System? I've tried this:
select SINGLE ordernr
from ZDEVXXX_PROJECT3 INTO ordernrU
having count( * ) =
( select max( count( * ) )
from ZDEVXXX_PROJECT3
where ordernr = ordernr
group by ordernr )
But I get this error:
"Unknown columnname COUNT("
How to combine max and count in ABAP? The SQL Query above is working in Oracle for me.
Thanks!

You need to have COUNT(*) in the result set if you want to use it in the HAVING clause. See http://help.sap.com/abapdocu_751/en/ABENWHERE_LOGEXP_ALL_ANY_SOME.htm for an example.

Since release 6.1 you can use aggregates in HAVING clause.
But your answer is "No way". Aggregates must be only in form aggr( {[distinct] column | *} )
So you must to
select count( * )
into table itab
from ZDEVXXX_PROJECT3
where ordernr = ordernr
group by ordernr
Then to find maximum of counts programmaticaly. And only then to use it in HAVING condition.

Or you can use ABAP Open SQL. It gives you the access to SQL of your particular DB and you can execute your mentioned above query.

Related

Output of non-existent values when grouping in sql

For example, i have a table with the data:
Screenshot
This table named "table".
I have the SQL query:
select
kind,
count(kind)
from table
where region = 'eng'
group by kind
And I get the result:
Question: how do I write a query that would return all the values that are in the kind field (or any other field that can be in group by)? Even if this value is 0. For the example above, the desired result is
It is mandatory to use group by in the query.
I use a postgresql 10.
Using a conditional aggregation
select
kind,
count(case region when 'eng' then kind end) cnt
from table
group by kind
select
t1.kind,
coalesce(t2.total, 0) total
from
(
select distinct kind from table
) t1
left join
(
select
kind,
count(kind) total
from table
where region = 'eng'
group by kind
)t2
on t1.kind = t2.kind
db fiddle

How to fetch only single type using Oracle SQL

I want to fetch only those company for which there is only one type. I am using using Oracle 12C. Below is the sample dataset
Result Set:-
Some one please help me on this.
select companyid, min(type_) as type_
from [table_name]
group by companyid
having min(type_) = max(type_)
;
Replace [table_name] with your actual type name. Note also that I used type_ (with an underscore) for the column name. I hope your column name is not type, which is a reserved keyword; if it is, change it.
A possible alternative is to use having count(distinct(type_)) = 1 - but that is a poor solution. It requires a distinct operation within each group (by companyid). By contrast, min and max are much easier to keep track of.
You can use not exists:
select t.*
from t
where not exists (select 1
from t t2
where t2.companyid = t.companyid and t2.type <> t.type
);
Another way is to compare MIN(TYPE) for each company with its MAX(TYPE). If they're the same then it only has one TYPE.
SELECT CompanyId, Type
FROM myTable
WHERE COMPANYID IN (
SELECT COMPANYID
FROM myTable
GROUP BY COMPANYID
HAVING MIN(TYPE) = MAX(TYPE)
)

Getting result basis on count of another SQL query

I have a table with the following columns:
bkng_date
bkng_id (varchar)
villa_id (varchar)
This query
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date;
returns the no.of records for each date as count.
Now I need to find dates in the resultset of this query where cnt = 2.
I tried a couple of subqueries but I'm not getting the desired results.
The simplest, correct and safe solution is adding having count(*) = 2 clause as Gordon said.
For completeness, if you were curious how to solve it using subqueries (you didn't provide your db vendor though it's very likely your vendor supports having clause), it would be:
select x.bkng_date, x.cnt from (
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date
) x
where x.cnt = 2
or
with x as (
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date
)
select * from x where cnt = 2
Best Option is to use the Having Clause as follows,
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date
having count(*) = 2

SQL Oracle Find Max of count

I have this table called item:
| PERSON_id | ITEM_id |
|------------------|----------------|
|------CP2---------|-----A03--------|
|------CP2---------|-----A02--------|
|------HB3---------|-----A02--------|
|------BW4---------|-----A01--------|
I need an SQL statement that would output the person with the most Items. Not really sure where to start either.
I advice you to use inner query for this purpose. the inner query is going to include group by and order by statement. and outer query will select the first statement which has the most items.
SELECT * FROM
(
SELECT PERSON_ID, COUNT(*) FROM TABLE1
GROUP BY PERSON_ID
ORDER BY 2 DESC
)
WHERE ROWNUM = 1
here is the fiddler link : http://sqlfiddle.com/#!4/4c4228/5
Locating the maximum of an aggregated column requires more than a single calculation, so here you can use a "common table expression" (cte) to hold the result and then re-use that result in a where clause:
with cte as (
select
person_id
, count(item_id) count_items
from mytable
group by
person_id
)
select
*
from cte
where count_items = (select max(count_items) from cte)
Note, if more than one person shares the same maximum count; more than one row will be returned bu this query.

sql query - filtering duplicate values to create report

I am trying to list all the duplicate records in a table. This table does not have a Primary Key and has been specifically created only for creating a report to list out duplicates. It comprises of both unique and duplicate values.
The query I have so far is:
SELECT [OfficeCD]
,[NewID]
,[Year]
,[Type]
FROM [Test].[dbo].[Duplicates]
GROUP BY [OfficeCD]
,[NewID]
,[Year]
,[Type]
HAVING COUNT(*) > 1
This works right and gives me all the duplicates - that is the number of times it occurs.
But I want to display all the values in my report of all the columns. How can I do that without querying for each record separately?
For example:
Each table has 10 fields and [NewID] is the field which is occuring multiple times.I need to create a report with all the data in all the fields where newID has been duplicated.
Please help.
Thank you.
You need a subquery:
SELECT * FROM yourtable
WHERE NewID IN (
SELECT NewID FROM yourtable
GROUP BY OfficeCD,NewID,Year,Type
HAVING Count(*)>1
)
Additionally you might want to check your tags: You tagged mysql, but the Syntax lets me think you mean sql-server
Try this:
SELECT * FROM [Duplicates] WHERE NewID IN
(
SELECT [NewID] FROM [Duplicates] GROUP BY [NewID] HAVING COUNT(*) > 1
)
select d.*
from Duplicates d
inner join (
select NewID
from Duplicates
group by NewID
having COUNT(*) > 1
) dd on d.NewID = dd.NewID