SQL SUM, Group by and Having query - sql

I have a pretty standard query that will get me a list of products that have stock above 0.
This works fine, however I now need to ADD the SUM of all negative numbers for BranchID 31 for the given SKU.
SELECT * FROM Products WHERE
PrimaryPLU IN
(select SKU FROM Stock
WHERE BranchID IN (2,12,1,11,0,96,32,13,14,15)
AND ApplicationID = #site
GROUP BY SKU HAVING(SUM(Stock)) > 0))
SO BranchID 31 can have positive and negative numbers, however I just need to get the sum of the negative ones for the SKU, this then needs to be added to the check to see if it's over 0.
Not a clue where to start, was hoping for support from a SQL master!
Thanks in advance!
Dave

Try this:
SELECT * FROM Products WHERE
PrimaryPLU IN
(select SKU FROM Stock
WHERE BranchID IN (2,12,1,11,0,96,31,32,13,14,15)
AND ApplicationID = #site
GROUP BY SKU HAVING(SUM(CASE WHEN Stock < 0 AND BranchID = 31
THEN Stock ELSE 0 END)) > 0))

Take the sum of absolute value of the negative numbers (stock) to see if they are non-zero.

Related

Retrieve the total number of orders made and the number of orders for which payment has been done

Retrieve the total number of orders made and the number of orders for which payment has been done(delivered).
TABLE ORDER
------------------------------------------------------
ORDERID QUOTATIONID STATUS
----------------------------------------------------
Q1001 Q1002 Delivered
O1002 Q1006 Ordered
O1003 Q1003 Delivered
O1004 Q1006 Delivered
O1005 Q1002 Delivered
O1006 Q1008 Delivered
O1007 Q1009 Ordered
O1008 Q1013 Ordered
Unable to get the total number of orderid i.e 8
select count(orderid) as "TOTALORDERSCOUNT",count(Status) as "PAIDORDERSCOUNT"
from orders
where status ='Delivered'
The expected output is
TOTALORDERDSCOUNT PAIDORDERSCOUNT
8 5
I think you want conditional aggregation:
select count(*) as TOTALORDERSCOUNT,
sum(case when status = 'Delivered' then 1 else 0 end) as PAIDORDERSCOUNT
from orders;
Try this-
SELECT COUNT(ORDERID) TOTALORDERDSCOUNT,
SUM(CASE WHEN STATUS = 'Delivered' THEN 1 ELSE 0 END ) PAIDORDERSCOUNT
FROM ORDER
You can also use COUNT in place of SUM as below-
SELECT COUNT(ORDERID) TOTALORDERDSCOUNT,
COUNT(CASE WHEN STATUS = 'Delivered' THEN 1 ELSE NULL END ) PAIDORDERSCOUNT
FROM ORDER
you could use cross join between the two count
select count(orderid) as TOTALORDERSCOUNT, t.PAIDORDERSCOUNT
from orders
cross join (
select count(Status) PAIDORDERSCOUNT
from orders where Status ='Delivered'
) t
What I've used in the past for summarizing totals is
SELECT
count(*) 'Total Orders',
sum( iif( orders.STATUS = 'Delivered', 1, 0 ) ) 'Total Paid Orders'
FROM orders
I personally don't like using CASE WHEN if I don't have to. This logic may look like its a little too much for a simple summation of totals, but it allows for more conditions to be added quite easily and also just involves less typing, at least for what I use this regularly for.
Using the iif( statement to set up the conditional where you're looking for all rows in the STATUS column with the value 'Delivered', with this set up, if the status is 'Delivered', then it marks it stores a value of 1 for that order, and if the status is either 'Ordered' or any other value, including null values or if you ever need a criteria such as 'Pending', it would still give an accurate count.
Then, nesting this within the 'sum' function totals all of the 1's denoted from your matched values. I use this method regularly for report querying when there's a need for many conditions to be narrowed down to a summed value. This also opens up a lot of options in the case you need to join tables in your FROM statement.
Also just out of personal preference and depending on which SQL environment you're using this in, I tend to only use AS statements for renaming when absolutely necessary and instead just denote the column name with a single quoted string. Does the same thing, but that's just personal preference.
As stated before, this may seem like it's doing too much, but for me, good SQL allows for easy change to conditions without having to rewrite an entire query.
EDIT** I forgot to mention using count(*) only works if the orderid's are all unique values. Generally speaking for an orders table, orderid is an expected unique value, but just wanted to add that in as a side note.
SELECT DISTINCT COUNT(ORDERID) AS [TOTALORDERSCOUNT],
COUNT(CASE WHEN STATUS = 'ORDERED' THEN ORDERID ELSE NULL END) AS [PAIDORDERCOUNT]
FROM ORDERS
TotalOrdersCount will count all distinct values in orderID while the case statement on PaidOrderCount will filter out any that do not have the desired Status.

SQL Replace with wildcards

I am slowly finding out that the replace string does not work with Wildcards. What I have is approx 10 SKUs, these 10 SKUs each have approx 20 sub SKUs.
Example: _Example - Parent SKU
Example7bTL - Child SKU
-End result would be to turn all child SKUs into _Example so i can get a sum of units sold in a clean format.
what i currently have for reference.
use test
CREATE TABLE #test (
Test int,
BillQty char(300) )
select Quantity, REPLACE (SKU, '%EXAMP%', 'Example') As Sku
from test.dbo.tblSFCOrderTxn
drop table #Test
Raw Data Example---
Quantity SKU
210 EXAMPLE7BOTL-C
42 EXAMPLE4BOTL
30 EXAMPLE1BOTL
28 EXAMPLE12BOTL
100 EXAMPLE7BOTL
97 EXAMPLE4BOTL
29 EXAMPLE7BOTL-C
What I want it to be
Quantity SKU
536 _Example
I am using SQL Server 2012
Given your data, just use case:
select (case when left(sku, 1) = '_' then '_Example' else sku end)
Shouldn't change data just to do a sum(). I would go with:
SELELCT
CASE WHEN SKU LIKE '%EXAMP%' THEN '_Example' ELSE SKU END AS SKU,
SUM(Quantity) AS Quantity
FROM test.dbo.tblSFCOrderTxn
GROUP BY CASE WHEN SKU LIKE '%EXAMP%' THEN '_Example' ELSE SKU END
ORDER BY 1
If you really do want to change the data, it would be:
UPDATE test.dbo.tblSFCOrderTxn
SET SKU = '_Example'
WHERE SKU LIKE '%EXAMP%'
It would be helpful
SELECT SUM(Quantity),'_Example' AS SKU FROM test.dbo.tblSFCOrderTxn GROUP BY CASE SKU WHEN '1' THEN '1' ELSE '2' END

getting avg of column based on the result set

I have a select statement that divides the count of sales by country, priceBanding (see example below)
The select statement looks like follows:
SELECT p.[Price Band]
,t.[Country]
,o.COUNT([Order]) as [Order Count]
FROM #price p (temp table)
INNER JOIN country t ON p.CountryCode = t.countryCode
INNER JOIN sales o ON o.salesValue >= p.startPrice and s.salesValue < p.endPrice
What i want to be able to do is based on this result i want to get an avg of the unit count i.e. For all orders that are under 20 what is the avg unit counts and the same for all others. How can i do this?
Its most likely simple but I cant think through it.
What I am after:
So as you can see, in the price band <20 in UK the order count is 50, and the avg Units of that is 2. As i mentioned earlier, I want the Avg Units of all orders that are under 20 (which is 50 in the picture).
Is that clearer?
Thanks in advance.
EDIT:
The first table: assume it to be the source
And the second table gets the avg, that's what I am after.
Wouldn't you just use avg()?
SELECT p.[Price Band], t.[Country],
o.COUNT(*) as [Order Count],
AVG(Items)
FROM #price p INNER JOIN
country t
ON p.CountryCode = t.countryCode INNER JOIN
sales o
ON o.salesValue >= p.startPrice and s.salesValue < p.endPrice
GROUP BY p.[Price Band], t.[Country]
ORDER BY t.[Country], p.[Price Band]
Note: SQL Server does integer division of integers (so 3/2 = 1 not 1.5) and similarly for AVG(). It is more accurate to use a decimal point number. An easy way is to use AVG(items * 1.0).

Search Invoice Tablle to find Invoices with Quantity >0 and <0

I am searching Invoice Itemized Table for Invoices with Quantity having >0 and <0. Invoice Itemized table contains details of all the items in an Invoice. How can I write a query that gives all the invoices that have Quantity of items >0 and <0.
How about something like this? This uses two queries. The first finds all the invoices with Negative Quantities. Then it APPLY's the second query to find from that list of invoices only those that also have positive quantities.
SELECT DISTINCT
PosNegInvoices.InvoiceID
FROM ItemizedInvoice AS NegInvoices
CROSS APPLY
(
SELECT
InvoiceID
FROM ItemizedInvoice
WHERE InvoiceID = NegInvoices.InvoiceID
AND Quantity > 0
) AS PosNegInvoices
WHERE NegInvoices.Quantity < 0
Here is another version that uses CTEs:
WITH NegInvoices AS
(
SELECT
InvoiceID
FROM ItemizedInvoice
WHERE Quantity < 0
),
PosInvoices AS
(
SELECT
InvoiceID
FROM ItemizedInvoice
WHERE Quantity > 0
)
SELECT DISTINCT
PosInvoices.InvoiceID
FROM NegInvoices
JOIN PosInvoices
ON NegInvoices.InvoiceID = PosInvoices.InvoiceID

Using 2 fields in ORDER BY clause

I have a page that show "special offers", and i need to order the results by discount value. Besides i want that products with quantity=0 are shown at the end of the list (regardless of the discount value).
So, there is any way to do that using only SQL? I mean... if i set "ORDER BY discount, quantity DESC" the list show products ordered by discount, and each groups of discout is ordered by the quantity value... this isn't what i want.
Thanks in advance...
ORDER BY CASE Quantity WHEN 0 THEN 99999999 ELSE Discount END, Quantity DESC
SELECT * FROM `products` ORDER BY discount WHERE quantity > 0
UNION SELECT * FROM `products` WHERE quantity <= 0;
Like this?