This might be a silly newbie question, but I am trying to figure out how to order something.
How would you construct an SQL statement so that in a table ORDERING with an ORDERDATE attribute I can see how many orders placed on each date?
I know that the following query makes it so it is organized by the date but I want to know how many duplicates for each date in order to find how many orders placed in a day.
SELECT *
FROM ORDERING
ORDER BY ORDERDATE DESC;
Count is an aggregate function, so you have to GROUP BY the attribute you want to aggregate.
SELECT ORDERDATE,COUNT(*)
FROM ORDERING
GROUP BY ORDERDATE
ORDER BY ORDERDATE DESC;
en making questiuons.
SELECT ORDERDATE,COUNT(*)
FROM ORDERING
GROUP BY ORDERDATE
HAVING COUNT(*)>1
Related
I am fairly new to SQL and i have tried my hardest to code this, but it does not seem to work for me so help would be appreciated very much.
The question states that i need to list the total number of books published by each publisher and then list them from highest and lowest using the publisher code.
I have gotten this far;
SELECT COUNT (pub_code)
FROM Library_Books
GROUP BY pub_code
ORDER BY pub_code
Any help would be appreciated whatsoever
SELECT kb_pub_code, COUNT (kb_pub_code)
FROM Library_Books
GROUP BY kb_pub_code
ORDER BY 2
You are very close:
SELECT kb_pub_code, COUNT(*) TotalBooks
FROM Library_Books
GROUP BY kb_pub_code
ORDER BY TotalBooks DESC
TotalBooks is an alias for the count column, which is counting the total number of rows for each kb_pub_code
You can reference a column alias in your ORDER BY: ie.ORDER BY TotalBooks DESC
Note, if books can be repeated in the Library_Books table, you may want a DISTINCT count. Something like:
SELECT kb_pub_code, COUNT(DISTINCT title) TotalBooks
FROM Library_Books
GROUP BY kb_pub_code
ORDER BY TotalBooks DESC
SELECT kb_pub_code
,COUNT (*) as 'TotalBooks'
FROM Library_Books
GROUP BY kb_pub_code
ORDER BY TotalBooks desc
Edit: with the additional information of possible duplicates you may wish to use a CTE.
With DistinctList as(
Select Distinct kb_pub_code as 'PubCode'
,kb_title as 'Title'
FROM Library_Books)
Select PubCode
,Count(*) as 'TotalBooks'
From DistinctList
Group by PubCode
ORDER BY TotalBooks desc
ORDER BY DESC will sort in descending order. The publisher code should be included in the result set (SELECT) otherwise you would not know which publisher is the count for.
SELECT kb_pub_code, COUNT (kb_pub_code)
FROM Library_Books
GROUP BY kb_pub_code
ORDER BY COUNT(kb_pub_code) DESC
I am using SQL Server 2012 and trying to pull the 4 most recent order dates in a OrderDate column for certain members. I am not sure how to code for this. Any help will be greatly appreciated.
Order by date in descending order, and limit the number of rows to 4, like this:
select top 4 order_date from orders order by order_date desc
Optimizer will figure out that you have a limit, and optimize your query to avoid sorting the entire table. If order_date column is indexed, query optimizer would use index to get you four dates without going to the table itself.
It should be something similar to this:
select top 4 o.OrderDate
from Members m
inner join Orders o
on m.ID = o.MemberID
where <your certain members criteria>
order by o.OrderDate desc
I'd do something like this (you didn't provide table structure, so this is just pseudo-code).
Create empty table TempOrders to store top orders for each member
Open Cursor to go through all member Ids
For each #MemberId insert into TempOrders select top 4 * from orders where MemberId = #MemberId order by OrderDate desc
your result is in TempOrders table
I have a table with 20 columns in which one column is a transaction date and one is a sales amount column. I want to pull all the columns and group it by transaction date. But I get a error if I don't mention all the columns in group by. Any suggestions.
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. You need to provide some aggregate function to use it with group by. E.g:
SELECT transaction_date, COUNT(sales_amount) as AmountCount
FROM table_name
GROUP BY transaction_date
you can try this:
SELECT *
FROM table_name
order by transaction_date ASC
note:
This will selects your 20 column from table ordered by Transaction date and change ASC or DESC as per your need.
When using Aggregate functions in SQL you need to use the group by statement by non-aggregate functions.
Example:
Select a.transactiondate,
a.vehicles,
a.market,
a.city,
sum(a.quantity) as Quantity
from table_name a
group by a.transactiondate,
a.vehicles,
a.market,
a.city
Sum being your aggregate function so you will not include in your group by. Even if you want to just group by date, you will need any other attributes that need to belong with it.
Its going to group by at the most granular data point. Since you are only summing or counting one column you will need all other attributes with it in the group by statement.
I'm trying to find the most expensive order within my table, and I've achieved this, but I'd like to know how to just return this one particular row. Right now it turns all rows with the most expensive order at the top. I'm not quite sure how to return just the most expensive order. I've grouped the orders by the order number (order_numb). I've tried using IN and a self join but I can't seem to get it to work.
My table
My current query that returns the correct result, just it returns all rows
SELECT order_numb, sum(cost_each) as totalSum
FROM order_lines
GROUP BY order_numb
ORDER BY totalSum DESC
What I'm trying to achieve
I'm trying to retrieve the most expensive order by itself.
I'm using Oracle as my database.
Use ROWNUM to filter the first row (most expensive order) of the result set:
SELECT t.*
FROM (
SELECT order_numb, sum(cost_each) as totalSum
FROM order_lines
GROUP BY order_numb
ORDER BY totalSum DESC
) t
WHERE ROWNUM <= 1
I'm working on a high school grading system.
At my school, grades can be changed by reworking problems and I store these changes with dates.
I have a function that properly returns averages because the most recent grade is flagged with a "current" field with a value of '1'. I'd like to make the function capable of returning the most recent grade with respect to a date in the past. I'm making a graph of how their average has changed over time.
What I'd like to do is something like this:
select sum(grades.points)
from grades
where date < 'thedate'
order by date DESC
group by assignmentID
I can't use sum and group by. It errors...
The best I can think of is to do a sub-select. Any other thoughts?
GROUP BY has to come before ORDER BY:
SELECT SUM(g.points)
FROM GRADES g
WHERE g.date < 'thedate'
GROUP BY g.assignmentid
ORDER BY g.date DESC
Try this:
SELECT cat_name, SUM(amount) AS total_amount
FROM table
GROUP BY cat_name