Count two items in a query - sql

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;

Related

Add and display all values

There are 2 tables called Item & Items_in_issue_Note(Items_in_issue_Note is a many to many table).
In Item table there is a column called Available_Qty and in Items_in_issue_Note there is a column called Issued_Qty.
I want to get the sum of Issued_Qty for each item and add it to Available_Qty in each item and display them item wise to get the Quantity before issue items.
I know how to get the sum of Issued_Qty by using
select ItemCode, sum(Issued_Qty)
from Items_In_Issue_Note
group by ItemCode
and how to get the Available_Qty by using
select itemCode,Available_Qty
from Item
and know how to get the quantity per item by using
select
itemCode,
Available_Qty + (select sum(Issued_Qty)
from Items_In_Issue_Note
where ItemCode='I001')
from Item
where ItemCode='I001'
But want to know how to get the output for all the items.
Thank you.
By using group by and join you may get the desired result. Try the following
select
A.itemCode,
A.Available_Qty + Coalesce(sum(B.Issued_Qty),0)
from Item A left join
Items_In_Issue_Note B on A.ItemCode=B.ItemCode
group by A.itemCode,A.Available_Qty
Your code modified below one is modified based on sub-query depends on main table value
select
itemCode,
Available_Qty + (select sum(Issued_Qty)
from Items_In_Issue_Note
where ItemCode=Item.ItemCode)
from Item
Best way is follow join instead of sub-query because sub-query gives performance issue.

SQL Sum Total with multiple assignments

select dc_id, whse_id, assg_id, START_DTIM,
UNIT_SHIP_CSE*prod_cub as TOTAL_CUBE
from exehoust.aseld
I attached a photo to show how the query currently populates. I want to sum the TOTAL_CUBE for each distinct ASSG_ID. I have tried case where sum and group by but keep failing. Basically want to do a SUM IF for each distinct ASSG_ID
You need to group by the assg_id, but ou need also the define what happens to all the other columns i choose MIN only to give you a hint, you need to choose the function yourself
select MIN(dc_id), MIN(whse_id), assg_id, MIN(START_DTIM),
SUM(UNIT_SHIP_CSE*prod_cub) as TOTAL_CUBE
from exehoust.aseld
GROUP BY assg_id
use select assg_id, sum() over(partition by assg_id order by assg_id) to sum by groupings

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.

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

Select inside select

A newbie here. So please be kind :-)
I have 2 Tables namely Item & Item Entries.
Relation is: Item.No = ItemEntries.No.
In Item Entries Table I have Columns as Qty, Entry type, Purchase Amount, Sales Amount
I like to have a report which shows as below,
Item No. | Opening Quantity | Purchase Amount | Sales Amount
To calculate Opening Inventory I summed up the quantity field and the result is as expected. No problem in that. Now From that dataset I like to run a sub query which Calculates/Sum the Purchase amount for an Item that is a part of first dataset and similarly for Sales Amount.
Select(Item No.,Sum(IE.Quantity) As Quantity, Select(......Purchase Amount),Select(....Sales Amount)
I hope I was able to clear my doubts to you guys.
Something like :
SELECT ItemNo, sum(quantity), purchaseAmount, SalesAmount FROM Item i INNER JOIN ItemEntities ie on i.no = ie.no GROUP BY ItemNo, PurchaseAmount, SalesAmount;
I believe (if I understand what you want) that this is the solution to your problem
Select Item.No ,
Sum(IE.Quantity) As Quantity,
(Select(......Purchase Amount)) As ColumnName1 ,
(Select(....Sales Amount)) As ColumnName2
From your need to "sum the purchase amount for an item that is part of the first dataset and similarly for Sales Amount" I think what you're trying to achieve is one row for each item on the Item table with a sum for each of the Qty, Sales Amount and Purchase Amount. If so, then you can simply use a 'group by' clause which groups results together which have matching values for the columns specified.
SELECT I.no, SUM(IE.qty), SUM(IE.purchase_amount), SUM(IE.sales_amount)
FROM item I JOIN item_entries IE
ON I.no = IE.no
GROUP BY I.no;
See the group_by_clause for more details and some examples.
N.B. The join from the item tables isn't strictly required in this example, but if you're producing a report I suspect you might want to get things like a description - in which case you'll need to add those columns to the group by clause too.
You might need the sum of three columns from
ItemEntries table(Qty,PurchaseAmount,SalesAmount) for each Item.No
Select A.No,
Sum(B.Qty),
Sum(B.Purchaseamount),
Sum(B.Salesamount)
From Item A,
Itementries B
Where A.No = B.No
Group By A.No;