Issue in Query Formation - sql

I am facing issue in query formation, require your inputs. Scenario is as below:
I am having three color of t-shirts that is blue, yellow and red. Each student is bind with one color t-shirt only. Can you help me to find out number of t-shirt of each color in a class(2/3/4) means group by class 2/3/4.
In DB we have studentId, class and stShirtColor(B/Y/R)
Thanks in advance

is this what you are looking for :
SELECT class,
stShirtColor,
Count(*) AS NumberOfTshirts
FROM yourtable
WHERE class IN( 2, 3, 4 )
GROUP BY class,stShirtColor
where clause will filter out the results as what you need and group by will make group of class,stShirtColor combination and ultimately count(*) will give you count of such groups.

You can get and print the color of the Tshirt and its count for each class by following query,
SELECT class, stShirtColor, count(*) as NUMBER_OF_TSHIRST FROM YOUR_TABLE WHERE class IN (2,3,4) group by class, stShirtColor;
Group By will organize your data based on class. You can use most aggregate functions when using GROUP BY.

Related

counting values in two columns at the same time? SQL/SQLITE

The problem I am attemping to do is find which two people go on the most number of trips together. I created a table where you have the name of a person, the name of somebody that went on a hike with them, the name of the peak, and the date. I want to be able to count all of the values of name1 and name2. Ex in my dataset below I want to count how many times 'Mary' and 'Patricia' appear side by side. I tried to use a COUNT(name1,name2) as numPairs and using group by (name1,name2) but SQLLITE says count() can only take in one parameter.
If my query looks off it is mainly due to the fact I am more comfortable using relational algebra selections/projections to get my data. am open to any other solutions that may help me
mary,patricia
brad,steven
cherry,rick
brad,steven
mary,patricia
mary,patricia
| 2
brad,steven | 2
cherry,rick | 1
Do you simply want aggregation?
select name1, name2, count(*)
from t
group by name1, name2;
If you want distinct pairs, so the ordering doesn't matter, you can use least() and greatest():
select least(name1, name2), greatest(name1, name2), count(*)
from t
group by least(name1, name2), greatest(name1, name2);
You need to use nested query as below
select count(*) as Num_of_times,both_names, peak1 from (select concat(name1,",",name2)
as both_names, peak1 from your_table) as tbl group by both_names;

multiple count(0 on table

I have a vehicle database and want to count how many cars have a specific colour.
But I don't know what colours there are as there are many, also combinations.
So this code does not do the trick for me:
SELECT
SUM(CASE WHEN colour='red' THEN 1 ELSE 0 END) red,
SUM(CASE WHEN colour='green' THEN 1 ELSE 0 END) green
(etc)
FROM vehicles
To get all colours, I could do:
select distinct colour from vehicles
But how can I use that information in a sql statement like the one above?
I am using MS sql server.
You could put the result set in rows rather than columns:
SELECT colour, count(*)
FROM vehicles
GROUP BY colour;
The alternative is that you would need to use dynamic SQL or express the result set as XML or JSON.
Why not simply do the aggregation ?
select colour, count(*) as no_vehicles
from vehicles v
group by colour;
This will pull a list of all colours found in table vehicles:
SELECT distinct colour
from vehicles
But what you really want to use is the group by clause, like so:
SELECT
colours
,count(*) HowMany
from vehicles
group by
colours
This will produce one row for every distinct value in column colours. It will NOT parse out color combinations; "red with black trim" will be its own column, and not +1 for red, _1 for black--that would be a much more complex problem.

Count two items in a query

I have to define a query that returns item num, item size, item colour and a count of how many items of a given color and size there are.
I am not able to figure out what kind of count the question is asking.
Here's what I tried but I am getting errors:-
SELECT inventoryItem.itemNum,
inventoryItem.itemSize,
inventoryItem.itemColor,
count(inventoryItem.itemSize),
count(inventoryItem.itemColor),
FROM inventoryItem
GROUP BY inventoryItem.itemSize, inventoryItem.itemColor;
Query you shown is not going to work, because in select there is column inventoryItem.itemNum, and that column is missing in your group by clause.
You are asked to give counts of items of a given color and size, that can be done using below query. In case you need itemNum, it will have to be added to select and group by section both.
SELECT inventoryItem.itemSize,
inventoryItem.itemColor,
count(1) count
FROM inventoryItem
GROUP BY inventoryItem.itemSize, inventoryItem.itemColor
I think you want:
SELECT itemSize,
itemColor,
count(1) itemcount
FROM inventoryItem
GROUP BY itemSize,
itemColor;

Pulling/Outputting data from teradata

I'm using Teradata as a database and I'm trying to pull and output some data for it.
Here are the links to:
Relationship
Metadata
I'm looking to figure out the queries for the following items.
1) What is the most common color and how many unique products have this color?
SELECT TOP 1 COLOR
FROM SKUINFO
GROUP BY COLOR
ORDER BY Count (*) desc;
this allowed me to get the color: Black.
But now I don't know how to get the number of products
2) What sku has the largest profit per unit and in which stores is this sku being sold?
Thank you!
Consider the WHERE clause subquery approach:
SELECT Count(*) AS ProductCount
FROM SKUINFO
WHERE Color IN
(SELECT TOP 1 COLOR
FROM SKUINFO
GROUP BY COLOR
ORDER BY Count (*) DESC);

SQL Group on a combination of values

I'm working a DB design regarding how a user launched something.
My idea was to have timestamp (DateTime) column, and a method column (varchar).
This 'method' (varchar) could be anything:
BUTTON_OK
BUTTON_X
APP_Y
APP_Z
etc
How can I COUNT the uses but group some values. In this case I want to have my result:
BUTTONS: 20
APP_X: 10
APP_Z: 14
You need some way of defining which 'methods' fall into which 'method group'.
One way would be to have a lookup table:
tbl_methodgroup
method_id Method Method_group
1 Button_OK Buttons
2 Button_X Buttons
3 App_Y App_Y
4 App_Z App_Z
then you could use:
select
b.method_group,
count(1)
from
tbl_methodgroup a
inner join tbl_method b on a.Method=b.Method
group by b.method_group
This method would have the advantage of being scalable as more methods get added. Rather than hand coding queries, which would need to be modified each time.
If the name of the table is tblTest, then the query will look like following:
SELECT method, COUNT(*) FROM tblTEst Group BY method
Apologies if I missread question, last chance to make it right if you have consistency in the data and grouping scenarios you can do following:
SELECT LEFT(method,CHARINDEX('_',method)-1),
COUNT(*)
FROM tblTest
GROUP BY LEFT(method,CHARINDEX('_',method)-1)
Otherwise Stuart Moore's answer is correct one.