Find Number of Unique Values in Table - sql-server-2005

I have a table with four columns:
PartNumber, ValvePartNumber, ActuatorPartNumber, Price
I want to find the number of distinct prices for each combination of ValvePartNumber and ActuatorPartNumber.
This is using SQL Server 2005

You can combine COUNT(DISINTCT) and GROUP BY to accomplish this.
SELECT ValuePartNumber, ActuatorPartNumber, COUNT(DISTINCT Price) AS Prices
FROM [Table]
GROUP BY ValuePartNumber, ActuatorPartNumber

Related

Sum over rows to result in columns in a table

I have a table like this:
Product
Type
Price
M&Ms
Peanut
2.50
M&Ms
No_Peanut
2.40
Assuming multiple products and types, I want to summarize price (Total price) over each product name but have it placed in a separate table like this:
Product
Total_price
M&Ms
4.9
I know there's analytical SQL functions like OVER but I wasn't sure how to make a separate table.
TO make seprate table use this
SELECT Product, SUM(Price) AS Total_price
INTO schema.newtable FROM schema.ProductTable
Group by Product;
This is a simple aggregation:
select prodct, sum(price) as total_price
from products
group by product
order by product;
And the result is a table. Only it is not a table stored in your database. It is usually not desired to store such an additional table, because then you'd have redundant information in your database, which can lead to inconsistencies later.
But if you want to, you can use above query to create a table:
create new_table as
select prodct, sum(price) as total_price
from products
group by product;
Update: SQL Server does not support the standard SQL syntax for creating a table from a query. The syntax in SQL Server is:
select prodct, sum(price) as total_price
into new_table
from products
group by product;
The query is just a simple GROUP BY:
SELECT Product, SUM(Price) AS Total_price
FROM Table1
GROUP BY Product;
If you want to insert it into a separate table that already exists, just use an INSERT combined with that query:
INSERT Table2 (Product, Total_price)
SELECT Product, SUM(Price) AS Total_price
FROM Table1
GROUP BY Product;
Or, to create a new table on the fly:
SELECT Product, SUM(Price) AS Total_price
INTO Table2
FROM Table1
GROUP BY Product;

how to sum result of count in sql query from one table and one column

I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1

Combining records but other columns have different values

I wanted to combine the same values in CATEGORY in this table, but as you can see the dates in the DATE column do not match up. Is there a way I can ignore this and just take the latest date in SQL?
SELECT CATEGORY, SIDE, sum(QUANTITY),sum(PRICE)
FROM table_name
group by 1,2
Basically, exclude the date column and the category can be grouped easily.
I would suggest something like:
SELECT CATEGORY, SIDE, Sum(QUANTITY) AS TOTALQUANTITY, PRICE, Max(DATE) AS LASTDATE
FROM YourTableName
GROUP BY CATEGORY, SIDE, PRICE

SQL Server query to further summarize grouped data

Assume a table named transactions with two columns: invoiceNumber and itemNumber. Multiple quantities of an item on a single invoice are reflected by multiple records in the table. (I know this isn't an appropriate design, but I'm simplifying a more complex structure to get at the root question.)
I can determine the average number of unique items for each invoice with a query like:
SELECT invoiceNumber, COUNT(DISTINCT itemNumber)
FROM transactions
GROUP BY invoiceNumber
This query effectively ignores the quantity of an item, counting each one only once per invoice and shows the result for each invoice.
Instead of all this detailed information, however, all I really want is to determine the average number of unique items across all invoices. That is, I just want to summarize the per-invoice information. How do I do that?
You can aggregate the result you've already figured out how to obtain.
WITH DistinctCounts AS (
SELECT invoiceNumber, COUNT(DISTINCT itemNumber) AS distinctItems
FROM transactions
GROUP BY invoiceNumber
)
SELECT AVG(distinctItems)
FROM DistinctCounts
Select avg(numberininvoice)
From
(
Select invoicenumber, count(itemnumber) as numberininvoie
From
(Select distinct invoicenumber, itemnumber
From transactions) a
Group by invoicenumber
) b

Aggregate SQL Function to grab only one from each grouping

I have a table that I need to normalize with many fields In SQL-Server 2000.
It contains 2 fields which I'm using to come up with distinct combination as defined by the specs.
ID and Rate: there are multiple rows of same IDs and Rates
I first created a temp table by grouping the IDs and Rates combination.
SELECT ID, Count(*) AS IDCounts, SUM(RATE) As Total
INTO #Temp
GROUP BY ID
Now I use Distinct to find only the unique combinations. So i'll have multiple ID groups sharing same Total and IDCounts
SELECT DISTINCT Total, IDCounts
INTO #uniques
FROM #Temp
Now my question is how to join a single ID back to that distinct grouping of IDCounts and Total and put that into a new table? It doesn't matter which one of the IDs in the groups as long as I use one from the same grouping.
Keeping your temp tables (although this could all be done in a single query):
SELECT ID, Count(*) AS IDCounts, SUM(RATE) As Total
INTO #Temp
GROUP BY ID
SELECT Total, IDCounts, MIN(ID) AS SomeID
INTO #uniques
FROM #Temp
GROUP BY Total, IDCounts
Add "Min(ID) AS FirstID" to the select into #uniques.
Try something like this:
SELECT MAX(ID) AS Id, Count(*) AS IDCounts, SUM(RATE) As Total
FROM SOMETABLE
GROUP BY IDCounts, Total