Stock list to only show unique part numbers - sql

I'm trying to create a stock list but only showing unique part numbers
The distinct line works and displays only unique part numbers
SELECT distinct PartNumber FROM v_StockValuation
However when I include this in the following way it displays several lines with the same part number
SELECT * FROM v_StockValuation
WHERE partnumber IN
(
SELECT distinct PartNumber FROM v_StockValuation
)
order by PartNumber
I've tried adding
GROUP BY partnumber HAVING COUNT(*) = 1 but this ends up excluding partnumbers which exist more than once
I'm probably missing something simple. Could someone guide me please?
Thanks

You can do:
SELECT * FROM v_StockValuation group by PartNumber;

Related

Get Count of Each Distinct Pair

I am trying to create a query that will give me all combinations of original sources and new sources, along with how many times each occur. What I have below seems to do the first part of giving me all of the different pairs, but I am struggling with getting it to display how many occurrences each have.
SELECT DISTINCT original_source, new_source
FROM sources
WHERE identifier = 1
ORDER BY original_source
You need to use GROUP BY
SELECT original_source, new_source, count(1) [Count]
FROM sources
WHERE identifier = 1
GROUP BY original_source, new_source
ORDER BY original_source

How do I used distinct to remove duplicates in this query?

I have the following table: Table
I am trying to write a query (that I will include in another query) to display how many account numbers there are per symbol.
I wrote the following query:
SELECT Symbol,
(SELECT DISTINCT COUNT([Account Number]) FROM [Open] T2 WHERE T2.Symbol = T1.Symbol) AS Accounts
FROM Open T1
GROUP BY [Symbol];
The query displays like this but it counts the same account number multiple times per symbol. EURUSD should have 3 and USDJPY should only have 1 next to it.
It should display like this.
I am trying to include this as part of another big table that has other information next to each symbol too.
I will appreciate any assistance.
Access doesn't support count(DISTINCT ...). You can try
SELECT Symbol, count(*) AS Accounts
FROM (SELECT DISTINCT Symbol, Account FROM Open)
GROUP BY Symbol;

Query that counts rows for a extra column for a select

I would like to make a viww that find the top 5 highest quantity items from a characters inventory but also a column that shows how many other people also has the item.
CHARACTERS
CHARID PK
INVENTORY
ITEMID
CHARID
quantity
ITEM
ITEMID
This is what I got so far
CREATE VIEW vwTop5
AS
SELECT TOP 5 INVENTORY.itemID, INVENTORY.charID,quantity,COUNT(SELECT *
FROM INVENTORY)
FROM INVENTORY
WHERE INVENTORY.charID = 3
I know this what above is wrong but could you guys please show me what to do to fix it? :)
You were close :
CREATE VIEW vwTop5
AS
SELECT TOP 5 INVENTORY.itemID, INVENTORY.charID,quantity,
(SELECT COUNT(*) FROM INVENTORY) as cnt
FROM INVENTORY
WHERE INVENTORY.charID =
Like #FelixPamittan said, you need to add an ORDER BY clause to specify which TOP 5 records you want.

Need SQL query to group together but sort overall

I have a table with Display_UPC, Brand, Item_Description, and other fields. There are several items with the same Display_UPC (all items belonging to the same display), and some displays have multiple brands.
I'm trying to print out a page that shows all of the Display contents (all Display_UPC together) with the various item descriptions, but sorted by Brand (so it starts with the "A" brands at the top of the page, then "B" brands, etc...).
Problem is, if I try:
SELECT DISTINCT *
FROM tbl_All_Displays
ORDER BY Brand, Display_UPC
some of the displays (the ones containing multiple brands) are missing some items because they are different brands. I can get rid of "Brand" in the ORDER BY and it returns complete displays together but they are not sorted by Brand (obviously).
I'm guessing maybe a GROUP BY is needed here but I can't get one to work. If I try something like:
SELECT DISTINCT *
FROM tbl_All_Displays
GROUP BY Display_UPC
ORDER BY Brand, Display_UPC
I get the error:
Column 'tbl_All_Displays.Item_Description' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
But I need Item_Description (and the other various fields) to be displayed on the page. They just aren't important in the ordering/grouping.
Sample Data:
Sample Expected Result:
So, basically, it doesn't matter which brand in a display the query uses in the sort. If a display contains a brand then it's okay for it to belong to that brand "group" if that makes sense. Is this possible?
Note: I deleted and reposted a previos question because it got messy with edits.
Edit: Here is sqlfiddle with sample table data - http://sqlfiddle.com/#!6/5069c
You want to sort first by the min(Brand) of each Display_UPC?
Then you need to sort by a "Group Min" first (fiddle:
SELECT *
FROM Table1
ORDER BY
min(Brand) over (partition by Display_UPC),
Display_UPC,
Brand
dnoeth did a great solution. But i want show the one I was working with.
SQL FIDDLE DEMO
with
minBrand as (
SELECT Display_UPC, MIN(BRAND) Brand
from Table1
GROUP BY Display_UPC
)
select m.Brand MainBrand, t.*
from minBrand m
inner join Table1 t
on m.Display_UPC = t.Display_UPC
order by m.Brand, t.Display_UPC, t.Brand, t.Item_Description

how to query access to select entire records given a distinct criteria

I want to select the entire first row of each record where a promo code is unique. I am trying to create a samples table, in this table will be one record (the first record) from each distinct promo code. I have asked all of my co-workers and they usually go though the data by hand and select one from each. the problem is that the number of promo codes grows each time and the codes change. so I want to write a query that will select the first record found to have each distinct code. so for I have something like this:
SELECT DISTINCT Customer.promo1 FROM Customer AS promo;
SELECT * FROM Customer, promo
WHERE Customer.promo1 = promo.promo1;
But this obviously give the original table. I do have a ID field called AutoID in Customer.
Thanks in advance.
I'm assuming you want the first Customer.AutoId associated with each Customer.Promo
SELECT
c.*
FROM
Customer c
INNER JOIN
(
SELECT
c.promo1,
MIN(c.AutoID) AutoID
FROM
Customer c
GROUP BY
c.promo1) FirstCusomterWithPromo
ON c.AutoID = FirstCusomterWithPromo.AutoID
Something like that:
SELECT * FROM Customer
GROUP BY Customer.promo1