Using count in oracle sql developer - sql

I'm using oracle sql developer and I can't get this query to function. It's telling me its not a single group function. Please help.
SELECT LGBRAND.BRAND_NAME, LGPRODUCT.PROD_DESCRIPT,
COUNT (LGPRODUCT.PROD_DESCRIPT) AS "NUMPRODUCTS"
FROM LGBRAND, LGPRODUCT
ORDER BY LGBRAND.BRAND_NAME;
What I'm trying to accomplish is to get the total different products grouped by each brand name.

when using aggregate functions you need to use group by clause
All aggregate functions like avg, count,sum needs to be used along with a group by function. If you dont use a group by clause, you are performing the function on all the rows of the table.
SELECT LGBRAND.BRAND_NAME,
LGPRODUCT.PROD_DESCRIPT,
COUNT (LGPRODUCT.PROD_DESCRIPT) AS "NUMPRODUCTS"
FROM LGBRAND, LGPRODUCT,
GROUP BY LGBRAND.BRAND;

You need to use GROUP BY clause.

Related

Can LAG be used with HAVING?

I distinctly recall that T-SQL will never let you mix LAG and WHERE. For example,
SELECT FOO
WHERE LAG(BAR) OVER (ORDER BY DATE) > 7
will never work. T-SQL will not run it no matter what you do. But does T-SQL ever let you mix LAG with HAVING?
Note: All that an answer needs to do is either give a theory-based or documentation-based reason why it does not, or give any example at all of where it does.
From Logical Processing Order of the SELECT statement:
The following steps show the logical processing order, or binding
order, for a SELECT statement......
FROM
ON
JOIN
WHERE
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
Window functions are evaluated at the level of SELECT, which comes after HAVING, so the answer is no you can't use window functions in the HAVING clause.
Having clause can only be used with Group by clause. In order to use Group by the listed columns should be aggregated using Group by columns. Group by can only be used with aggregate functions like min,max,sum,count functions. Hence it is not possible to combine having clause along with the LAG analytical function.
In order to use LAG and Having, one should use CTE or subquery.

What is wrong with my sql "group by" and "having" statement?

I keep getting an error saying this is not a group by expression when i run this statement in oracle XE.
SELECT PROJECTID, HOURSWORKED FROM ASSIGNMENT GROUP BY PROJECTID HAVING HOURSWORKED > 20;
GROUP BY is used for an aggregating expression, such as MAX() or SUM().
You can either write
SELECT PROJECTID, HOURSWORKED
FROM ASSIGNMENT
WHERE HOURSWORKED > 20;
or
SELECT PROJECTID, SUM(HOURSWORKED)
FROM ASSIGNMENT
GROUP BY PROJECTID
HAVING SUM(HOURSWORKED) > 20;
(or some other aggregate function, eg: MAX, MIN) depending on your intent.
WHERE is used to filter individual records
HAVING is used to filter the grouped aggregate values.
You have to have an aggregate function on hoursworked - e.g. MAX(HOURSWORKED), AVERAGE(HOURSWORKED) etc. Or you also have to add it to the group by, though that would seem strange..
One other suggestion for asking SQL questions on stackoverflow - create a minimal test case using SQL Fiddle. E.g. http://sqlfiddle.com/#!17/fea91/5 for your example. Unless it's something very Oracle specific, Postgres is usually close enough for ordinary SQL questions. Try changing the WHERE to a HAVING, to see the difference between the two...

Sql query not part of aggregate function

when I run the following query in access
SELECT SUM(RegBed) AS RegBedTOTAL, PostcodeRESULTS_PostcodeNS as postcode
FROM cooltableless10;
I get the following error:
this query does not include the specified expression 'postcode' as part of an aggregate function.
Could somebody tell me why it is so?
SUM is an aggregate function, which means that any non-aggregated columns must be included in a GROUP BY clause. Try this:
SELECT SUM(RegBed) AS RegBedTOTAL, PostcodeRESULTS_PostcodeNS as postcode
FROM cooltableless10
GROUP BY PostcodeRESULTS_PostcodeNS
you need to add a group by clause
and group it by postcode
add some like this on your query:
group by postcode
you always need to add group by clause and group your data when
using aggregate functions on your query

Is there any difference to use group by in a query ?

I have a query
SELECT bk_publisher, bk_price FROM books
GROUP BY bk_price, bk_publisher
and
SELECT bk_publisher ,bk_price FROM books
both are returning the same results. Means i have 12 records in my table and both queries returning the 12 records. What is the difference ? Although i am using group by, which is use with aggregate functions. But i want to know is group by making any difference here ?
SELECT bk_publisher, bk_price FROM books
GROUP BY bk_price, bk_publisher
Will result distinct pairs of (publisher, price), even if your table contains duplicated data.
SQL group by helps you group different results by some identical value (using aggregation functions on other values)
In your case it doesn't mean anything, but when you want to aggregate values based on identical field, you use group by.
For example, if you want to get the max price of a publisher:
SELECT bk_publisher, max(bk_price) FROM books
GROUP BY bk_publisher
The GROUP BY statement is used to group the result-set by one or more columns.Group by is used when you have repeating data and you want single record for each entry.
When you use GROUP BY, it will squeeze multiple rows having identical columns listed in GROUP BY as single row in output.
It also means that in general, all other columns mentioned in SELECT list must be wrapped in aggregate functions like sum(), avg(), count(), etc.
Some SQL engines like MySQL permit not using aggregates, but many people consider this a bug.
GROUP BY clause is apparently showing no effect because there is no repeating combination of bk_price, bk_publisher values.

Select all columns on a group by throws error

I ran a query against Northwind database Products Table like below
select * from Northwind.dbo.Products GROUP BY CategoryID and i was hit with a error. I am sure you will also be hit by same error. So what is the correct statement that i need to execute to group all products with respect to their category id's.
edit: this like really helped understand a lot
http://weblogs.sqlteam.com/jeffs/archive/2007/07/20/but-why-must-that-column-be-contained-in-an-aggregate.aspx
You need to use an Aggregate function and then group by any non-aggregated columns.
I recommend reading up on GROUP BY.
If you're using GROUP BY in a query, all items in your SELECT statement must either be contained as part of an aggregate function, e.g. Sum() or Count(), else they will also need to be included in the GROUP BY clause.
Because you are using SELECT *, this is equivalent to listing ALL columns in your SELECT.
Therefore, either list them all in the GROUP BY too, use aggregating functions for the rest where possible, or only select the CategoryID.