Help on "GROUP BY clause" error - max

Wish anybody can help me with this error "Column 'Sales.No_' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."...
I spent few days to understand this error but failed :(
My Query:-
SELECT SH.[No_], SH.[Sell-to Customer No_], SH.[Sell-to Contact No_],
SH.[Sell-to Customer Template Code], MAX (A.[Version No_])
FROM [Sales] AS SH
LEFT JOIN [Sales Archive] A ON (SH.[Document Type] = A.[Document Type]
AND SH.[No_]=A.[No_]
AND SH.[Doc_ No_ Occurrence]=A.[Doc_ No_ Occurrence])
WHERE (SH.[Document Type]='0' and SH.[Order]='1')

The "MAX" function requires a group by if you have an non-aggregate columns.
So you will need to add a group by sh.[No_]....
Also I have reformatted your query so I can read it easier -hope that is ok-
select SH.[No_]
, SH.[Sell-to Customer No_]
, SH.[Sell-to Contact No_]
, SH.[Sell-to Customer Template Code]
, MAX (A.[Version No_])
from [Sales] AS SH
LEFT JOIN [Sales Archive] A ON
(SH.[Document Type] = A.[Document Type]
AND SH.[No_]=A.[No_]
AND SH.[Doc_ No_ Occurrence]=A.[Doc_ No_ Occurrence]
)
where (SH.[Document Type]='0' and SH.[Order]='1')
group by SH.[No_]
, SH.[Sell-to Customer No_]
, SH.[Sell-to Contact No_]
, SH.[Sell-to Customer Template Code]

It's because you've used an aggregate function (MAX), so the remaining selected columns must also be using aggregate functions or in a group by clause. eg
select SH.[No_],SH.[Sell-to Customer No_],SH.[Sell-to Contact No_],
SH.[Sell-to Customer Template Code],MAX (A.[Version No_])
from [Sales] AS SH LEFT JOIN [Sales Archive] A ON (SH.[Document Type] = A.[Document Type]
AND SH.[No_]=A.[No_] AND SH.[Doc_ No_ Occurrence]=A.[Doc_ No_ Occurrence])
where (SH.[Document Type]='0' and SH.[Order]='1')
group by SH.[No_],SH.[Sell-to Customer No_],SH.[Sell-to Contact No_],
SH.[Sell-to Customer Template Code]

Related

Customer details based on booking count

Write a query to display user id and user name where the number of seats booked by the user in the individual booking is greater than 1. Display records in ascending order by user name.
I have tried this query and I m getting errors. Please help me!!
select u.user_id,u.name
from users u join bookingdetails bd
on u.name=bd.name
join tickets t on u.user_id=t.user_id
group by u.name
having count(t.no_seats) > 1
order by u.name;
select distinct u.user_id,u.name from users u
join tickets t on u.user_id = t.user_id
where
u.user_id in( select user_id from tickets where no_seats>1)
order by u.name;
If I understand your assignment it looks like you should do
WITH cteBookings AS (SELECT bd.USER_ID, SUM(t.NO_SEATS) AS TOTAL_SEATS_BOOKED
FROM BOOKINGDETAILS bd
INNER JOIN PAYMENTS p
ON p.BD_ID = bd.BD_ID
INNER JOIN TICKETS t
ON t.TICKET_ID = p.TICKET_ID
GROUP BY bd.USER_ID)
SELECT DISTINCT b.USER_ID, u.USER_NAME
FROM USERS u
INNER JOIN cteBookings b
ON b.USER_ID = u.USER_ID
WHERE b.TOTAL_SEATS_BOOKED > 1
ORDER BY u.USER_NAME ASC
Best of luck.
According to the question, it asks for the individual bookings > 1
So, the solution is very simple i.e, for each ticket_id, the no of seats should be greater than 1.
SELECT DISTINCT(user_id),name
FROM Tickets inner join Users
USING (user_id)
WHERE no_seats>1
ORDER BY name;
It will simpler if you try it without joins.
A simple example using a subquery:
select user_id, name from users
where user_id = any(select distinct user_id from tickets where no_seats > 1)
order by name asc;

Sample clause using Group by and Having clauses

I am trying to using Sample clause using Group by and Having clauses.
My requirement is getting a Company ID which has only one record, I have tried many ways below is one of the methods I have tried.
With TESTA AS(
select TA.COMPANY_ID from(
select t1.COMPANY_ID from Table11 t1
where t1.COMPANY_ID in (select t2.COMPANY_ID from Table22 t2)
group by t1.COMPANY_ID having count(t1.COMPANY_ID)=1)TA )
select * FROM TESTA Sample(1);
When I execute the above query, It is throwing the below error.
ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT,
GROUP BY, etc.
01446. 00000 - "cannot select ROWID from, or sample, a view with DISTINCT, GROUP BY, etc."
*Cause:
*Action:
But, When I execute the below query, I am getting the results but it is not satisfying my requirement.
select COMPANY_ID from Table11 Sample(10)
where COMPANY_ID in (select company_id from Table22 )
Group by COMPANY_ID HAVING Count(COMPANY_ID)=1
It is executing all the possible records, can someone help me on this, please.

SQL Join Statement + COUNT doesn't work

Here is my SQL (Oracle) code:
SELECT a.Task_id, a.Activity_desc AS "ACTIVITY DESCRIPTION" ,
a.Activity_date || ' (' || a.Activity_day || ')' AS "ACTIVITY DATE",
pr.Project_name, COUNT(a.Volunteer_id)
FROM task_activity a
JOIN task t
ON a.Task_id = t.Task_id
JOIN project pr
ON t.Project_id = pr.Project_id
GROUP BY a.Task_id
ORDER BY Task_id
An error :
ORA-00979: not a GROUP BY expression
Without group statemen table looks like that :
I want to count number of volunteers for each task and group by task_id, but after hours of tries I gave up )
You need to group by all non aggregated values. Start off with something small and build up e.g.
SELECT a.Activity_desc, COUNT(a.Volunteer_id) countVolunteer
FROM task_activity a
GROUP BY a.Activity_desc
Check that returns you a count per activity, and then slowly add your other attributes to both the SELECT and GROUP BY sections e.g.
SELECT a.Activity_desc, t.Task_id, COUNT(a.Volunteer_id) countVolunteer
FROM task_activity a
JOIN task t
ON a.Task_id = t.Task_id
GROUP BY a.Activity_desc,t.Task_id

Error thrown when converting SQL Server to Oracle SQL

I'm having to convert a lot of T-SQL statements this week to Oracle, most I have worked out but I'm still getting stuck on a few. The code below is one of them, I get the error that that the from clause is incorrect, I have tracked it down to this line of code:
NewBlc = Sls.CurrentBlc - SUM(NVL(Sls.NewBlc, 0) AS "New Balance",
I do not know what the correct format is to fix it. Any help appreciated.
SELECT
Sls.CustNme AS "Customer Name",
Sls.CustAddr AS "Customer Address",
Sls.CurrentBlc AS "Old Balance",
NewBlc = Sls.CurrentBlc - SUM(NVL(Sls.NewBlc, 0) AS "New Balance",
Tis
FROM
Customers,
(SELECT
c.Name AS CustNme,
c.Address AS CustAddr,
c.Balance AS CurrentBlc,
Cis(s.Item, c.Name) AS NewBlc,
Sis(c.name) AS Tis
FROM
CUST c
INNER JOIN
Sales o ON c.Name = o.cust
INNER JOIN
Purchases i on i.OrderNo = o.OrderNo
INNER JOIN
Contracters s on i.Item = s.Item
WHERE
o.Order_Date BETWEEN TO_DATE('01-01-2008', 'dd/mm/yyyy' )
AND TO_DATE('30-12-2009', 'dd/mm/yyyy')
GROUP BY
c.Name, c.Address, s.Item, c.Balance) Sls
GROUP BY
Sls.CustNme, Sls.CustAddr,Sls.CurrentBlc, NewBlc, Tis;

Display only the largest count in a group by statment

I'm trying to display only the largest group in this group by statement;
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno GROUP BY type;
I modified it so we get this query response now you can see group double is larger then family.
BOOKING TYPE
5 double
2 family
I know there is a HAVING keyword you can add in order display only a count compared to a number so I could do COUNT(type) HAVING > 2 or similar but that's not very dynamic and that would only work in this instance because I know the two amounts.
ORDER BY COUNT(type) DESC LIMIT 1
There isn't a having statement that does this. But you can use rownum with a subquery:
select t.*
from (SELECT COUNT(type) AS booking, type
FROM booking b join
room r
on r.rno = b.rno AND r.hno = b.hno
GROUP BY type
order by count(type) desc
) t
where rownum = 1;
Just order your query..
order by booking desc
regards
TRY this
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno ORDER BY type DESC LIMIT 1

Resources