multiple count(0 on table - sql

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.

Related

How to tally distinct combinations of rows in sqlite?

Say I have a collection of glass jars with some plastic balls of different colors in them. There can be at most one ball of each color in every jar and every jar contains one or more balls.
I represent this collection with an sqlite table with the following columns:
JarID INTEGER, Color TEXT
A collection of three jars might then look like this:
JarID Color
0, 'Red'
1, 'Red'
1, 'Green'
2, 'Red'
2, 'Blue'
3, 'Red'
I'd like to write a query that will find all the different color combinations that exist in my jar collection, and list each combination alongside the total number of jars with that combination.
For the table above, the query should return either:
'Red', 2
'Red,Green', 1
'Red,Blue', 1
Or:
Red
2
Red
Green
1
Red
Blue
1
Currently I have a terrible mess of common table expressions and window functions that seems to achieve the desired result, but I can't help feeling that I'm missing some elegant, standard SQL solution to this.
The Group_Concat function is what you're looking for, but first you need to aggregate and order your list so red-green-red comes out the same as green-red-red.
Do that with:
Select Jar_Id, Color
From Jars
Group By Jar_Id, Color
Order by Jar_Id, Color
Now feed that to a group_concat subquery to make a list of colors by jar_id:
Select Jar_Id, Group_Concat(Color) as Combos
From <first subquery>
Group By Jar_Id
Then feed that to an aggregator to count occurrences, and you're done:
Select Combos, count(*) as Occurrences
From <second subquery>
Group By Combos
Order by Combos
Put it all together with:
Select Combos,count(*) as Occurrences
From (
Select Jar_Id, Group_Concat(Color) as Combos
From (
Select Jar_Id, Color
From Jars
Group By Jar_Id, Color
Order by Jar_Id, Color
)
Group By Jar_Id
)
Group By Combos
Order by Combos
One caution: Order Bys in a subquery are not strictly defined in SQL and might be ignored, but most implementations will do them.

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);

Return the column value that common among multiple rows meeting criteria

I have a table of schedules, and each schedule must have 1 or more associated criteria. The parent and child look like this:
SchedId ScheduleName
1 ScheduleA
2 ScheduleB
3 ScheduleC
Criteria table:
CriteriaName CriteriaValue SchedId
color red 1
width wide 1
depth verydeep 1
color blue 2
density dense 3
height short 3
porosity spongy 3
Now, I have data records for which I need to identify the correct schedule. Because my query is restricted to a particular domain, I happen to know exactly which criteria to test for--in other words I know I'm interested only in results where there are exactly 3 criteria involved, and I know they are color, width and depth.
A typical data record set might look like:
Color Width Depth
--------------------------------------
red narrow shallow
red medium deep
blue wide verydeep
red wide verydeep
I also happen to know that there is 1 and only 1 schedule having a given set of values for the known criteria. So what could be easier?
Conceptually my desired query looks like:
SELECT color, width, depth, SchedId
FROM [...]
and a correct return row would be:
red wide verydeep 1
I've used the answer to a similar problem here to get the desired SchedId in at least two different ways--one by JOINing selects on the criteria table for each set of criteria (criterianame = color, criteriavalue = red), joining on the SchedId requiring the SchedId for all returned rows to be the same. This works because I used literal values in the query.
I can find no way, in a regular query (no sproc) to get the values of my data rows to the JOIN subselects for use in comparison.
The other technique I've used in the past for this exact problem is one I'm trying to avoid--it basically counts the number of matches I get on the criteria for each SchedId. If the number of matches for a given SchedId equals the number of criteria for that Id, then it's the correct Sched. I don't much care for that approach, although it's worked okay. This technique was used with MySQL, but I am now using T-SQL.
Finallly, I designed the criteria table this way to allow unlimited numbers of criteria, and unlimited kinds of criteria for an unlimited number of schedules. It has proven to be amazingly challenging. Ideas?
You can do this using conditional aggregation:
select pt.Schedid, pt.schedname,
max(case when ct.citerianame = 'color' then ct.criteriavalue end) as Color,
max(case when ct.citerianame = 'width' then ct.criteriavalue end) as Width,
max(case when ct.citerianame = 'depth' then ct.criteriavalue end) as Depth
from parenttbl pt inner join
criteriatbl ct
on pt.schedid = ct.schedid
group by pt.schedid, pt.schedname;
SELECT
case when ct.citerianame = 'color' then ct.criteriavalue end Color,
,case when ct.citerianame = 'width' then ct.criteriavalue end Witdh,
,case when ct.citerianame = 'depth' then ct.criteriavalue end Depth
,pt.Schedid
FROM
parenttbl pt
inner join criteriatbl ct on pt.schedid = ct.schedid
where
pt.schedid = 1

Issue in Query Formation

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.