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);
Related
I have a table that looks like this -
Table screenshot link - https://i.stack.imgur.com/Pztpq.png
I want to add a new column 'Manufacturer_Updated', such that -
If any particular 'Product' has more than 1 (distinct) 'Manufacturer', then the Manufacturer having highest 'Sales' should be populated in the 'Manufacturer_Updated' column for all rows of that particular 'Product'.
Ex - In the above screenshot, Product - 'TOTAL HAIR CARE NA' has 2 different Manufacturer, so in the 'Manufacturer_Updated' column, 'SEXY HAIR CONCEPTS' should appear for both the rows, as it has the higher sales.
Could someone pls help with this query? Thanks in Advance!
Something like this should work:
SELECT Manufacturer, Product, Sales, Manufacturer as Manufacturer_Updated FROM
WHERE amt_of_manufacturers > 1
((SELECT Product, max(Sales) as Sales, count(distinct Manufacturer) as amt_of_manufacturers
FROM your_table
GROUP BY Product) as q1
left join
(SELECT Manufacturer, Sales, Product
FROM your_table
) as q2
ON q1.Sales = q2.Sales
AND q1.Product = q2.Product
) as q3
In the first query (q1), you're retrieving maximum sales per each product along with amount of manufacturers for a specific product (used later in upper query). In the second one (q2) you just need to retrieve Manufacturer (to transform it later to Manufacturer_Updated), Sales and Product (as join keys). After this you only need to filter out all products with single manufacturer.
Alternatively, if you want to keep those, you can remove where amt_of_manufacturers > 1 and replace Manufacturer_Updated in the upper query with the following:
CASE WHEN
amt_of_manufacturers <=1 THEN null
ELSE Manufacturer
END AS Manufacturer_Updated
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.
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;
I have a table that lists thousands of products. A product can be either Standard (123450.000.000), one colour (123456.BLA.000), one size (123456.000.LAR) or both colour and size (123456.BLA.LAR).
A product can have multiple colours (123456.BLA.000, 123456.YEL.OOO etc etc). I am trying to do a query that brings back a product that has multiple colours but only one colour in stock i.e. ProductQTY = 1 and the remaining colours are out of stock.
All I have been able to come up with is the query below but this just brings back all 'variant' products that have 1 in stock. What do I have to add or change to make it bring back results whereby a product has different colours but only ONE of those colours are in stock and the remaining are out of stock? Do I need to do a UNION?
select *
from Product
where productcode NOT LIKE ('%000.000')
AND ProductQTY = '1'
First off, that's a terrible structure. Store color and size information in separate tables, don't make them part of one massive variable.
Second, you want to do a query that selects Product IDs (1-6, looks like) that have a count > 1 with color present and the sum of their quantities is 1 exactly (according to your question - if that one record could have a qty>1 and still be okay, this is a little more complicated).
select * from product where substr(product,1,6) in (
select substr(product,1,6) from product
where not (product like '%.000.%')
group by 1
having count(1) > 1
and sum(ProductQTY)=1
)
The question would be much easier if you would split up the properties in different columns. But you could do that in a query aswell:
select code from
(select substring(productcode,-3) as size,
substring(productcode,-7,3) as colour,
substring(productcode,0,len(prodcutcode)-7) as code
from product) t
group by code
having sum(productQTY) = 1
//or count(*) = 1 to get all unique ones
I've got two tables that I'm trying to grab data from. The first is a 'titles' table, which represents product information (name, unique ID, etc). The second is a 'prices' table which collects price information for various currencies (each product can have multiple historic entries in the prices table).
I've written a fairly long-winded SQL statement to grab the latest price changes across products, but there are some issues that hopefully more experienced users will be able to help me out with:
SELECT
t.id,
t.name,
t.type,
p.value,
(SELECT
value
FROM
prices
WHERE
prices.id = p.id AND
prices.country='US' AND
prices.timestamp < p.timestamp
ORDER BY
prices.timestamp DESC
LIMIT 1) AS last_value
FROM
prices AS p
INNER JOIN
titles AS t
ON
t.row_id = p.id
WHERE
p.country = 'US' AND
(SELECT
value
FROM
prices
WHERE
prices.id = p.id AND
prices.country='US' AND
prices.timestamp < p.timestamp
ORDER BY
prices.timestamp DESC
LIMIT 1) IS NOT NULL
GROUP BY
t.id
ORDER BY
p.timestamp DESC,
last_value DESC
LIMIT 0, 25"
The first issue I've run into is that, while this query works, titles appear multiple times in the same listing. While this is expected, I'd ideally like only the latest price change to be displayed for the title. To solve this, I tried GROUPING by the titles 'id' (note the: GROUP BY t.id above). Unfortunately, while I'd expect the GROUP to respect the ORDER BY (which orders the latest price changes in DESC order), the results seem to remove the latest changes and show the GROUP'd titles with earlier price values.
Secondly, is there any better way to grab the last price of each item (currently I grab the current value, and then run a subquery to grab the 'last_value' - which effectively represents the value before the current price change). At the moment I run two subqueries: one to grab the second to last known price, and again to ensure that a previous price exists in the database (otherwise there's no point in listing the title as having a price change).
Any help would be appreciated!
How about this:
SELECT titles.id, titles.name, titles.type, prices.value, MAX(prices.timestamp)
FROM titles, prices
WHERE prices.row_id = titles.id AND prices.country='US';
Mind you, I don't have MySQL installed so I couldn't try this query.
[Edit:] I think it won't work 'cause it'll always display the last price entered for all the items because it'll always choose the highest timestamp from the prices table, maybe a group by will do, I'm really sleepy now and I can't think straight;
[Edit2:] How about this:
(SELECT max(report_run_date) as maxdate, report_name
FROM report_history
GROUP BY report_name) maxresults
SELECT titles.id, titles.name, titles.type, prices.value,
(SELECT MAX(prices.timestamp) as maxtimestamp FROM prices GROUP BY prices.row_id)
FROM titles, prices
WHERE prices.row_id = titles.id AND prices.country='US';