I have a table called "Orders" and I need to get a list with only the name of the cities that have had more than 30 orders.
the columns:
OrderID, CustomerID, EmployeeID, TerritoryID,
Orderdate, Requireddate, Shippeddate, Shipvia,
Freight, Shipname, Shipaddress, Shipcity, Shipregion,
Shippostalcode, Shipcountry
I have managed to get it to tell me the number of orders by city with the 'Shippostalcode' but I don't know how to tell it to show me only those with more than 30 orders
The code I was trying was this:
SELECT shipcity,
COUNT(shippostalcode) AS order_numbers
FROM orders
where order_numbers>30
GROUP BY shipcity
ORDER BY order_numbers DESC;
It is not a WHERE, but HAVING clause:
SELECT shipcity,
COUNT(*) AS order_numbers
FROM orders
GROUP BY shipcity
HAVING COUNT(*) > 30
ORDER BY order_numbers DESC;
Related
I have an Orders Table with an Order Date Time Field, I want to grouping the result by before and after today order date. and after that I want to sort the order after today by cusmtomer id ASC and sort the order before today by customer id DESC.
The Orders Table looks like the following image:
I tried query order before and after today and I combine the result using UNION but still not getting the result like what I want
I was tried query separately both of order before and after today. like this following code:
First I query the order after today like this:
select OrderID, CustomerID, OrderDate, ShipName, ShipCountry
from Orders
WHERE OrderDate > GETDATE() AND ShipCountry = 'USA'
ORDER BY CustomerID ASC
and the result look like this:
and then I query again like this:
select OrderID, CustomerID, OrderDate, ShipName, ShipCountry
from Orders
WHERE OrderDate < GETDATE() AND ShipCountry = 'USA'
ORDER BY CustomerID DESC
and the result look like this:
but I don't know how to joining both of two query result above with single query instead of separately query.
What I expect in my query result is combination of before today query result and after today query result.
You can Split the order by into 2 parts:
select OrderID, CustomerID, OrderDate, ShipName, ShipCountry
from Orders
WHERE ShipCountry = 'USA'
ORDER BY
(CASE WHEN OrderDate < GETDATE()
THEN CustomerID
END) DESC
, (CASE WHEN OrderDate > GETDATE()
THEN CustomerID
END) ASC
Old Ans..
select OrderID, CustomerID, OrderDate, ShipName, ShipCountry
from Orders
WHERE OrderDate <> GETDATE() AND ShipCountry = 'USA'
ORDER BY CustomerID DESC
Note:- use is not Equal to Operator for this..then you will get the
all result other than today date...
New Ans...
As you told ...you can use Order by Clause with UNION.....you need to set top records...
SELECT orders1.OrderID,orders1.CustomerID, orders1.OrderDate, orders1.ShipName, orders1.ShipCountry FROM
(
SELECT TOP 100 OrderID, CustomerID, OrderDate, ShipName, ShipCountry
from Orders
WHERE OrderDate > GETDATE() AND ShipCountry = 'USA'
ORDER BY CustomerID ASC
)orders1
UNION
SELECT orders2.OrderID, orders2.CustomerID, orders2.OrderDate, orders2.ShipName,orders2.ShipCountry FROM
(
SELECT TOP 100 OrderID, CustomerID, OrderDate, ShipName, ShipCountry
from Orders
WHERE OrderDate < GETDATE() AND ShipCountry = 'USA'
ORDER BY CustomerID DESC
)orders2
I am writing a SQL query using the AdventureWorks 2014 database.
I want to show Which orders contain more than two products? Show order number, order value, and number of products that the order contains.
I tried to write statement by itself (see below), but I'd like to be able to solve the relation :
select SalesOrderID , ProductID , LineTotal
from Sales.SalesOrderDetail
order by SalesOrderID
SELECT SalesOrderID,
COUNT(ProductID) as total_products,
SUM(LineTotal) as total_invoice
FROM SalesOrderDetail s
GROUP BY SalesOrderID
HAVING COUNT(ProductID) > 2
ORDER BY s.SalesOrderID
#Juan Carlos Oropeza
i did it can you check if there is something wrong with it
select s.SalesOrderID,
a.SalesOrderCount,
a.ProductCount,
sum(LineTotal) as OrderValue
from Sales.SalesOrderDetail s ,
(select SalesOrderID ,
count(SalesOrderID) as SalesOrderCount ,
count(productid) as ProductCount
from Sales.SalesOrderDetail
group by SalesOrderID
) a
where s.SalesOrderID = a.SalesOrderID
group by a.salesordercount, s.SalesOrderID, a.ProductCount
having a.ProductCount >2
order by s.SalesOrderID
Select
SalesOrderID,
sum(LineTotal) as [order value],
count(SalesOrderID) as [number of Product]
from Sales.SalesOrderDetail
group by SalesOrderID having count(SalesOrderID)>=2
order by SalesOrderID desc
This code would give your expected answer
SELECT SalesOrderID
,ProductID
,LineTotal
,'Sales Order ' +CAST(SalesOrderID AS VARCHAR(100))+' Contains Productid of'+CAST(ProductID AS VARCHAR(100))
AS [ProductsCountPerOrder]
,Productscount
FROM
(
SELECT salesorderid ,
productid ,
linetotal ,
Count(productid)OVER(partition BY salesorderid ORDER BY salesorderid) AS productscount
FROM sales.salesorderdetail
)dt
WHERE dt.productscount>2
ORDER BY salesorderid
I have a table like attached image in SQL server.
I am trying to retrieve sorted data based on SUM(freight) column. For this i have used the below query.
SELECT ShipCountry
FROM CountryDetails
GROUP BY ShipCountry
ORDER BY SUM(freight) ASC
When i run this i am getting result like below.
If i run the below query i am getting result like below. It's fine.
SELECT ShipCountry, ShipCity
FROM CountryDetails
GROUP BY ShipCountry, ShipCity
ORDER BY SUM(Freight), ShipCity ASC
Instead of this i need a result like below. In order by clause SUM(Freight) should consider only ShipCountry. It should not consider both ShipCountry and ShipCity. My Expected result is
How to achieve this result through SQL query?
You can try a query like below
select
ShipCountry,
ShipCity
from
(
Select
ShipCountry,
ShipCity,
SUM(Freight) OVER( Partition by ShipCountry order by ShipCountry) NetCountry,
SUM(Freight) OVER( Partition by ShipCountry,ShipCity order by ShipCountry) NetCity
from
CountryDetails
) T
Order by NetCountry asc,ShipCity asc
Live Demo
I think CUBE or ROLLUP will help you to attain the result. However please find the query below that I created. I just created the query from the model you shared. I can not guarantee on the performance. Also please omit the first and last columns.
;
WITH CTE_ShipDetails1
AS (
SELECT ShipCountry
, MIN(ShipCity) ShipCity
, SUM(Freight) NetCity
, 0 RowNum
FROM CountryDetails
GROUP BY ShipCountry
)
, CTE_ShipDetails2
AS
(
SELECT CTE1.ShipCountry ShipCountry1
, NULL ShipCountry
, CountryDetails.ShipCity
, ROW_NUMBER() OVER (
ORDER BY CTE1.ShipCountry
, CTE1.ShipCity
) RowNum
, SUM(Freight) OVER(PARTITION BY CountryDetails.ShipCountry, CountryDetails.ShipCity) NetCity
FROM CountryDetails
, CTE_ShipDetails1 CTE1
WHERE CTE1.ShipCountry = CountryDetails.ShipCountry
AND CTE1.ShipCity <> CountryDetails.ShipCity
)
SELECT DISTINCT ShipCountry1, ShipCountry
, ShipCity, NetCity, MIN(RowNum) RowNum
FROM
(
SELECT TOP 250000 ShipCountry ShipCountry1, ShipCountry
, ShipCity, NetCity, RowNum
FROM CTE_ShipDetails1 CTE1 UNION
SELECT TOP 250000 ShipCountry1, ShipCountry
, ShipCity, NetCity, RowNum
FROM CTE_ShipDetails2 CTE2
) AS a GROUP BY ShipCountry1, ShipCountry
, ShipCity, NetCity
ORDER BY ShipCountry1 ASC
, RowNum ASC
;
I'm having trouble crafting a simple SQL query that retrieves the number of orders placed on 7/15/1998, which includes the date in the result. The label number of orders should be "OrderCount".
This is what I have got so far without success.
SELECT SUM(OrderT.CID) as OrderCount
FROM OrderT
WHERE OrderT.CID= #7/15/1998#;
Here is a screenshot of the database:
SELECT OrderDate, COUNT(OrderID) AS OrderCount
FROM OrderT
WHERE OrderDate = #1998-07-15#
GROUP BY OrderDate;
This is how:
SELECT Count(*) AS OrderCount
FROM OrderT
WHERE OrderDate = #7/15/1998#;
To include order date:
SELECT OrderDate, Count(*) AS OrderCount
FROM OrderT
GROUP BY OrderDate
HAVING OrderDate = #7/15/1998#;
This might be it:
SELECT OrderDate, COUNT(OrderID) AS OrderCount FROM OrderT WHERE OrderDate = #7/15/1998#
I have a view which ultimately I want to return 1 row per customer.
Currently its a Select as follows;
SELECT
Customerid,
MAX(purchasedate) AS purchasedate,
paymenttype,
delivery,
amount,
discountrate
FROM
Customer
GROUP BY
Customerid,
paymenttype,
delivery,
amount,
discountrate
I was hoping the MAX(purchasedate) would work but when I do my groupings it breaks as sometimes there could be a discountrate, sometimes its NULL, paymenttype can differ for each customer also, is there anyway just to show the last purchase a customer makes?
since SQL Server 2008 r2 supports windows function,
SELECT Customerid,
purchasedate,
paymenttype,
delivery,
amount,
discountrate
FROM
(
SELECT Customerid,
purchasedate,
paymenttype,
delivery,
amount,
discountrate,
ROW_NUMBER() OVER (Partition By CustomerID
ORDER BY purchasedate DESC) rn
FROM Customer
) derivedTable
WHERE derivedTable.rn = 1
or by using Common Table Expression
WITH derivedTable
AS
(
SELECT Customerid,
purchasedate,
paymenttype,
delivery,
amount,
discountrate,
ROW_NUMBER() OVER (Partition By CustomerID
ORDER BY purchasedate DESC) rn
FROM Customer
)
SELECT Customerid,
purchasedate,
paymenttype,
delivery,
amount,
discountrate
FROM derivedTable
WHERE derivedTable.rn = 1
or by using join with subquery which works in other DBMS
SELECT a.*
FROM Customer a
INNER JOIN
(
SELECT CustomerID, MAX(purchasedate) maxDate
FROM Customer
GROUP BY CustomerID
) b ON a.CustomerID = b.CustomerID AND
a.purchasedate = b.maxDate