How can a group by query be used to get the number of occurences of a particular column? - sql

I want to get the results in a way that each order is displayed with the number of times they occur in a table. For example:
Chicken Parmessan - 3
Polo Pizza - 5

select food, count(*) from tablename group by food

If you have a table like this:
create table orders (
menu_item varchar(100) not null
)
Then you'd want something like this:
select menu_item, count(*)
from orders
group by menu_item
The aggregate function count will then count the number of entries in each group and each group will be identified by menu_item.

Related

Using Derby SQL to calculate value for histogram

I have a table with various SKU in totes.
The table is totecontents with below columns:
ToteID
SKU
Each Tote can contain a maximum of 6 SKUs. (programmatically constrained)
select toteid, count(*) as qtypertote
from totecontents
group by toteid;
gives me a list of totes with the number of skus in each.
I now want to get to a table with following result
SkuCount Occurences where each row would have the ordinal value (1 through 6 ) and then the number of occurences of that value.
My efforts included the following approach
select count(*)
from
( select toteid, count(*) as qtypertote
from totecontents
group by toteid)
group by qtypertote;
Stung by the comments I performed more research. This works:
SELECT CountOfskus, COUNT(1) groupedCount
FROM
( SELECT COUNT(*) as countofskus, toteid
FROM totecontents
Group By toteid
) MyTable
GROUP BY countofskus;

how can I select rows that column does NOT have more than 1 value?

I am very new to SQL and I am wondering how to solve this issue. For example, my table looks as follows:
As you see in the table item_id 1 appears in both city_id 1 and 2, so does the item_id 4, but I want to get all the items where appears only in one city_id.
In this example, these would be item_id 2 (appearing only in city_id 2) and item_id 3 (appearing in city_id 1).
Use aggregation on item_id and count distinct values of city_id. The having clause can be used to filter on aggregates.
select item_id from mytable group by id having count(distinct city_id) = 1
You can use the following query:
SELECT item_id
FROM table_name
GROUP BY item_id
HAVING COUNT(DISTINCT city_id) = 1
In case you want to see the city_id to you can use this query:
SELECT item_id, MIN(city_id) AS city_id
FROM example
GROUP BY item_id
HAVING COUNT(DISTINCT city_id) = 1
Since there is only one city_id you can use MIN or MAX to get the id.
demo on dbfiddle.uk
You want all the id where they have only one distinct city:
SELECT item_id
FROM table
GROUP BY item_id
HAVING count(distinct city_id) = 1
It works by counting all the different values that city_id has for the same item_id. For those item ids where they repeat a lot, but the city_id is always the same the count of unique values in the city id is 1, and we can look for these using a HAVING clause. "Having" is like a where clause that runs after a GROUP BY operation is completed. It is the conceptual equivalent of this:
SELECT item_id
FROM
(
SELECT item_id, count(distinct city_id) as cdci
FROM table
GROUP BY item_id
) x
WHERE cdci = 1
If you want the city id too you can either get the MAX city (because in this case there is only one city so it's safe to do):
SELECT item_id, MAX(city_id) as city_id
FROM table
GROUP BY item_id
HAVING count(distinct city_id) = 1
or you could join this query back to the item table as a subquery:
SELECT t.*
(
SELECT item_id
FROM table
GROUP BY item_id
HAVING count(distinct city_id) = 1
) x
INNER JOIN
table t
ON x.item_id = t.item_id
This technique is the more general process for performing a group by that finds some particular set of rows, then bringing in the rest of the data from that row. You cant always stick every other column you want in a MAX because it will mix row data up, and you can't put the extra columns in your group by because that will subdivide what you're grouping on, giving the wrong results. Doing the group as a subquery and joining it back is a typical way to get all the row data when you have to group it to find which rows are interesting
In your case this form of query will bring all the duplicated rows (whereas the group by/max won't). If you don't want the duplicate rows you can make the top line SELECT DISTINCT t.* but don't make a habit of slapping distinct in to get rid of duplicated rows; if your tables don't have duplicates to start with but suddenly after you wrote a JOIN you got duplicated rows, google fornwhat a Cartesian product is in database queries and how to prevent it
You just need a group by on item id with having
Select item_id from table group by
item_id having count(distinct city_id)
=1
Also, if you want to have majority of same no of rows as input then
Select item_id, city, rank()
over(partition by item_id order by city)
rn
From table where rn=1;

SQL grouping with multiple rows

There's a table that I use that lists invoice detail. So for instance let's say a customer checks out with 2 items, there are 2 rows for each item.
Right now my SQL Query looks like this:
Select date
,order_id
,count(distinct(item_name))
from Table_1
group by 1,2
Rather than grouping it by order_id. Is there anyway to modify this query to find the number of Orders that have X amount of items on a specific date. So on 1/1/1990 5 orders have 3 items, 6 orders have 2 items, etc.
Thanks for the help!
If I'm understanding your question correctly, you could use a subquery grouping by the item count:
select t.date, t.itemCount, t.count(order_id)
from (
Select date
,order_id
,count(distinct(item_name)) AS itemCount
from Table_1
group by 1,2
) AS t
group by date, itemCount

Count items in column SQL query

Let's say I have a table that looks like,
id
2
2
3
4
5
5
5
How do I get something like,
id count
2 2
3 1
4 1
5 3
where the count column is just the count of each id in the id column?
You want to use the GROUP BY operation
SELECT id, COUNT(id)
FROM table
GROUP BY id
select id, count(id) from table_name group by id
or
select id, count(*) from table_name group by id
This is your query:
SELECT id, COUNT(id)
FROM table
GROUP BY id
What GROUP BY clause does is this:
It will split your table based on ids i.e all your 1's are separated, then the 2's , 3's and so on. You can assume it like new tables are created where in one table all the 1's are stored, 2's in another , 3's in yet another and so on.
Then after that the SELECT query is applied on each of these separate tables and the result is returned for each of these "groups".
Good luck!
Kudos! :)

Getting Number of records in oracle

Am trying to fetch the number of records in the table using Count(*) along with my query condition
Sample Table is
Table: STUD_NAME
Id Name
1 Steven
2 smith
2 Ben
1 Willy
My query is
select std.name
from STUD_Name where id='2'
for this it will display the output as "Smith" and "Ben", along with i need the total number of records in the STUD_NAME table.
By right it should display the total records as "4", please help me out to solve this issue and how to form the query in this case
SELECT name,
cnt as total_count
FROM (
SELECT id
name,
count(*) over () as cnt
FROM stud_name
) t
WHERE id = 2
Assuming that id is a numeric column the single quotes around the value 2 are not needed (and are actually harmful due to the implicit data type conversion that happens in the background)
What about:
select
std.name
,(select count(1) from STUD_Name) nrofstds
from STUD_Name std where std.id='2'
select STUD_NAME.name, CNT.count
from STUD_NAME
, (select count(*) COUNT from STUD_NAME) CNT
where id='2'