Combining records but other columns have different values - sql

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

Related

SQL and JPQL query - searching all records grouping by parameter with the given date

I have table which looks similar to this:
I want to build query for searching all records from this table with the given date (let's say 5.12.2019) and with earlier dates but group by materialID.
Example: select all materials with date 6.12.2019 should show all materials with this date (or materials with earlier dates) group by material id with the biggest date Result should look like this:
Problem: I want to group my results by MaterialID with the biggest date. So in this example I don't want to show materials with the same id with earlier dates.
For the same example:
Question: How to build query like this using SQL and also JPQL? Because i would like to use this query in Hibernate so i need also JPQL query.
Thanks for your help.
This is a special case of a "top N per category" query. You want to show the maximum date per material id. In SQL (would also work in JPQL):
SELECT SUM(Amount), SUM(Price), MaterialId, MAX(Date)
FROM t
GROUP BY MaterialId
Note that with this technique, you cannot also display the ID, or MAX(ID), as the IDs and dates are not necessarily both monotonously increasing. If you still want the ID displayed as in your example, then write this SQL query (I don't think this can be done in JPQL):
SELECT MAX(ID), SUM(Amount), SUM(Price), MaterialId, MAX(Date)
FROM (
SELECT last_value(ID) OVER (
PARTITION BY MaterialId
ORDER BY Date, ID
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS ID,
Amount,
Price,
MaterialId,
SELECT last_value(Date) OVER (
PARTITION BY MaterialId
ORDER BY Date, ID
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS Date
FROM t
) t
GROUP BY MaterialId

Select values with duplicate max values sql

I have a table made up of dates and sales totals for the particular date. I would like to be able to query the table and select the following: max sales, the date associated with the max sale figure, sum of all sales, and the minimum date in the table. One additional complication is that there are duplicate max values. I don't care which max value is chosen but I just want one at random. This is for Oracle.
Here is what I tried. It was using a sub query.
Select sales, date, min(date), sum(sales) from table
Where sales = (select distinct(max(sales)) from table)
select
max(sales),
max(date_) keep (dense_rank first order by sales desc),
sum(sales),
min(date_)
from
table_
See also This SQL Fiddle

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

Does this code return the latest date PER product?

I wish to get the latest date per product ID:
SELECT ProductID, Max(Date) as Date FROM MyTable GROUP BY ProductID
I was always under the impression I had to do a nested join, but this appears to work!
EDIT: Thanks for all replies
That's what aggregates do!
For each unique value of ProductId, return the maximum Date value. Not nested JOIN is necessary.
It is correct. It returns the biggest value of the column Date of all rows with the same ProductID.
Yes, per ProductId being exact. If it represents Product and there exists Date for this product - then completely YES
It is correct. You would need to use a nested JOIN if you were wanting to retrieve other columns in addition to the ProductID columns from the table. E.g:
SELECT ProductID, Quantity, ...
FROM MyTable
JOIN
(
SELECT ProductID, Max(Date) AS Date
FROM MyTable
GROUP BY ProductID
) T1 ON MyTable.ProductID = T1.ProductID AND MyTable.Date = T1.Date;

Find Number of Unique Values in Table

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