SQL Display Inventory Updates - sql

I have a table that displays all the updates of the inventory:
╔══════════════╦════════╦══════════╦═══════╗
║ ItemName ║ Type ║ Quantity ║ Date ║
╠══════════════╬════════╬══════════╬═══════╣
║ BottledWater ║ Add ║ 50 ║ 07/03 ║
║ BottledWater ║ Deduct ║ 20 ║ 07/03 ║
║ Chips ║ Add ║ 30 ║ 07/02 ║
║ BottledWater ║ Deduct ║ 10 ║ 07/02 ║
║ Chips ║ Deduct ║ 20 ║ 07/01 ║
╚══════════════╩════════╩══════════╩═══════╝
I would like to write a query and return a table that would display the total of an items added and deducted stocks, the table would look something like:
╔══════════════╦═══════╦══════════╗
║ ItemName ║ Added ║ Deducted ║
╠══════════════╬═══════╬══════════╣
║ BottledWater ║ 50 ║ 30 ║
║ Chips ║ 30 ║ 20 ║
╚══════════════╩═══════╩══════════╝
Any ideas?

Just use conditional aggregation:
select item,
sum(case when type = 'Add' then quantity else 0 end) as added,
sum(case when type = 'Deduct' then quantity else 0 end) as deducted
from inventory
group by item;

Syntax is slightly off, but this is the pseudocode:
select ItemName, sum(Type==add) as added, sum(Type==deduct) as deducted,
from inventory
group by ItemName

Related

How to Pivot this table on Oracle?

Can you help me figure out how to pivot this table:
╔═══════════╦═════════════╦══════╦════════╦════════╗
║ Big Group ║ Small Group ║ Kids ║ Adults ║ Elders ║
╠═══════════╬═════════════╬══════╬════════╬════════╣
║ 1 ║ 1 ║ 10 ║ 20 ║ 5 ║
║ 1 ║ 2 ║ 15 ║ 10 ║ 10 ║
║ 2 ║ 1 ║ 20 ║ 0 ║ 15 ║
╚═══════════╩═════════════╩══════╩════════╩════════╝
Into something like this?
╔═══════════╦═════════════╦══════╦════════╦════════╦═════════════╦══════╦════════╦════════╗
║ Big Group ║ Small Group ║ Kids ║ Adults ║ Elders ║ Small Group ║ Kids ║ Adults ║ Elders ║
╠═══════════╬═════════════╬══════╬════════╬════════╬═════════════╬══════╬════════╬════════╣
║ 1 ║ 1 ║ 10 ║ 20 ║ 5 ║ 2 ║ 15 ║ 10 ║ 10 ║
║ 2 ║ 1 ║ 20 ║ 0 ║ 15 ║ ║ ║ ║ ║
╚═══════════╩═════════════╩══════╩════════╩════════╩═════════════╩══════╩════════╩════════╝
The number of small groups per Big group is variable, and that's what is being difficult for me to understand how to do it.
Can anyone help me?
Thanks in advance
There is a way but the overhead of using PIVOT is to provide the list of all values which needs to be pivoted.
As you also need each small group to be pivoted we need to create a virtual column between big group and small group to be used in pivot clause as you see below
with table1
as
(select 1 bg
,1 sg,10 kids
,20 adult
from dual
union all
select 1,2,15,25 from dual
union all
select 2,1,20,0 from dual
)
select *
from
(
select t1.*,t1.bg||'_'||t1.sg piv
from table1 t1
)
pivot
(
max(sg) sg,max(kids) kids,max(adult) adult
for piv in ('1_1' as bg1_sg1
,'1_2' as bg2_sg2
,'2_1' as bg2_sg1)
)
order by bg
Demo

How to group by an expression in TSQL and capture the result?

How can I include the results of an expression in a GROUP BY clause and also select the output of the expression ?
Say I have this table:
╔════════════════════════╦═══════════╦═══════╗
║ Forest ║ Animal ║ Count ║
╠════════════════════════╬═══════════╬═══════╣
║ Tongass ║ Hyena ║ 600 ║
║ Tongass ║ Bear ║ 1200 ║
║ Mount Baker-Snoqualmie ║ Wolf ║ 30 ║
║ Mount Baker-Snoqualmie ║ Bunny ║ 2 ║
║ Ozark-St. Francis ║ Pigeon ║ 100 ║
║ Ozark-St. Francis ║ Ostrich ║ 1 ║
║ Bitterroot ║ Tarantula ║ 9001 ║
╚════════════════════════╩═══════════╩═══════╝
I need a row with the count of carnivores in each forest and a row for the count of non-carnivores (if there are any). This is the output I'm looking for in this example:
╔════════════════════════╦═══════════════╦═══════════════╗
║ Forest ║ AnimalsOfType ║ AreCarnivores ║
╠════════════════════════╬═══════════════╬═══════════════╣
║ Tongass ║ 1800 ║ 1 ║
║ Mount Baker-Snoqualmie ║ 2 ║ 0 ║
║ Mount Baker-Snoqualmie ║ 30 ║ 1 ║
║ Ozark-St. Francis ║ 101 ║ 0 ║
║ Bitterroot ║ 9001 ║ 1 ║
╚════════════════════════╩═══════════════╩═══════════════╝
The information for whether or not an animal is carnivorous is encoded in the expression.
What I'd like to do is include the expression in the group-by and reference its result in the select clause:
SELECT TOP (1000)
[Forest],
SUM([COUNT]) AS AnimalsOfType,
AreCarnivores
FROM [Tinker].[dbo].[ForestAnimals]
GROUP BY
Forest,
CASE WHEN ForestAnimals.Animal IN ('Pigeon', 'Ostrich', 'Bunny') THEN 0 ELSE 1 END AS AreCarnivores
However, this is not valid TSQL syntax.
If I include the Animal column in the GROUP BY clause to allow me to rerun the function in the SELECT, I'll get one row per animal type, which is not the desired behavior.
Doing separate selects into temp tables and unioning the results is undesirable because the real version of this query features a large number of expressions which need this behavior in the same result set, which would make for an extremely awkward stored procedure.
Use a CTE:
WITH X AS (
SELECT Forest, Animal, Count,
CASE WHEN ForestAnimals.Animal IN ('Pigeon', 'Ostrich', 'Bunny')
THEN 0
ELSE 1 END AS AreCarnivores
FROM [Tinker].[dbo].[ForestAnimals]
)
SELECT Forest, SUM(Count) AS AnimalsOfType, AreCarnivores
FROM X
Group by Forest, AreCarnivores;
Or be more verbose about it and repeat yourself:
SELECT Forest, SUM(Count) AS AnimalsOfType,
CASE WHEN ForestAnimals.Animal IN ('Pigeon', 'Ostrich', 'Bunny')
THEN 0
ELSE 1 END AS AreCarnivores
FROM [Tinker].[dbo].[ForestAnimals]
GROUP BY Forest, CASE WHEN ForestAnimals.Animal IN ('Pigeon', 'Ostrich', 'Bunny')
THEN 0
ELSE 1 END;
They're equivalent queries to the optimizer.

Have a SUM(Field_1) not exceed SUM(Field_2)

There might be a better way to accomplish but here is what I have:
Environment - Plex ERP -SQL Query Editor
Back-end - SQL Server 2012
Summary
Parts have a "unit" worth based on manufacturing complexity
Some days we ship part\s. Other days we don't
The part units are summed for each day they are scheduled to ship 'Rel_Units_Calc'
The plant gets credit for 5 units a day (when open) 'Unit_multiplier'
This daily credit is summed for each day 'Unit_Capacity'
In order to prevent an overloading of capacity in a slow month, I need to prevent the plant from getting the 5 unit credit when the SUM(Unit_Capacity) will exceed the SUM(Rel_Units_Calc).
A report will be created that will use a case statement to evaluate if the Rel_Units_Calc > Unit_Capcity, then show red else green.
Detailed Scope
I'm trying to create a sales report that will prevent the sales group from overloading (exceeding the capacity) of the plant. To simplify, lets say we have 3 parts (Part A, B, & C). Part A is simple and worth 1 "Unit". Part B is a little more complex and worth 2 "Units". Part C is the most complex and worth 5 "Units". The plant can process 5 units a day that it is open.
The report will show when a day has been overloaded by showing the color Red and green when days are not overloaded. Any days in red will need to have the sales order moved out.
My approach was to take the units * order quantity to give me the 'Release_Units'. Then I am doing a sum(Release_Units) to show a tally for each day in a field called 'Release_Units_Calc'.
I have another field called 'Unit_Multiplier' that gives the 5 unit per day credit on eligible days (excludes weekends and holidays). Then I am doing a sum(Unit_Multiplier) to show a tally for each day in a field called 'Unit_Capacity'.
The color Red and Green were going to be determined by using a case statement comparing the two columns Release_Units_Calc and Unit_Capacity. When Unit_capacity = Release_Unit then green else red.
This works ok until you look at December when we have a slow down for these parts and then we start banking Unit_Capacity. The Unit_Capacity field continues to accrue the 5 units per day even after it has surpassed the Release_Units_Calc. These parts are not produced in December so think 20 business days * 5 units per day gives us 100 Units on Jan 1 which is not good. Essentially, this would cause the sales group to overwhelm the plant in January as they will have 100 banked units to draw from.
I would like for the Unit_Capacity which again, is a SUM(Unit_Multiplier) to not exceed the Release_Units_Calc which is from SUM(Release_Units).
SQL Below:
This temp table marks the days that should be included for the capacity
SELECT
DISTINCT FDPO.FULL_DATE,
----case statement below to create an include flag. It will exclude weekends unless we have a shipment going out
(CASE WHEN (DATENAME(dw, DATEADD(d,0,FDPO.Full_Date)) NOT IN
('Saturday','Sunday')) THEN 1
WHEN (DATENAME(dw, DATEADD(d,0,FDPO.Full_Date)) IN
('Saturday','Sunday')) AND FDPO.DUE_DATE IS NOT NULL THEN 1
ELSE 0 END) AS 'Include'
INTO #Capacity_Temp1
FROM #FDPO AS FDPO
This temp table uses the include flag to remove the dates that should not accrue capacity and adds a capacity column.
SELECT
CT1.FULL_DATE,
#Unit_Multiplier AS 'Unit_multiplier'
INTO #Capacity_Temp2
FROM #Capacity_Temp1 AS ct1
WHERE ct1.INCLUDE= 1
The temp table below adds the unit multiplier up for each day
SELECT
DISTINCT CT2.FULL_DATE,
CT2.Unit_multiplier,
SUM(CT2.Unit_multiplier) OVER (Order By CT2.FULL_DATE) AS 'Unit_Capacity'
INTO #Unit_Capacity
FROM #Capacity_Temp2 AS CT2
The final display query
SELECT
RUC.FULL_DATE,
RUC.Release_Units,
RUC.Release_Units_Calc,--running talley of the release units
ISNULL(UC.Unit_multiplier,0) AS 'Unit_multiplier',
-- credit units given per day except when closed
UC.Unit_Capacity --running talley of the unit multiplier
FROM #RUC AS RUC
LEFT JOIN #Unit_Capacity AS UC
ON UC.FULL_DATE = RUC.FULL_DATE
The output at present:
╔══════╦═══════════════╦════════════════╦═════════════════╦═══════════════╗
║ DATE ║ Release_Units ║ Rel_Units_Calc ║ Unit_multiplier ║ Unit_Capacity ║
╠══════╬═══════════════╬════════════════╬═════════════════╬═══════════════╣
║ 8/3 ║ 15 ║ 15 ║ 5 ║ 5 ║
║ 8/4 ║ NULL ║ 15 ║ 5 ║ 10 ║
║ 8/5 ║ 20 ║ 50 ║ 5 ║ 15 ║
║ 8/5 ║ 15 ║ 50 ║ 5 ║ 15 ║
║ 8/6 ║ NULL ║ 50 ║ 0 ║ NULL ║
║ 8/7 ║ NULL ║ 50 ║ 5 ║ 20 ║
║ 8/8 ║ NULL ║ 50 ║ 5 ║ 25 ║
║ 8/9 ║ NULL ║ 50 ║ 5 ║ 30 ║
║ 8/10 ║ NULL ║ 50 ║ 5 ║ 35 ║
║ 8/11 ║ NULL ║ 50 ║ 5 ║ 40 ║
║ 8/12 ║ 15 ║ 65 ║ 5 ║ 45 ║
║ 8/13 ║ NULL ║ 65 ║ 0 ║ NULL ║
║ 8/14 ║ NULL ║ 65 ║ 5 ║ 50 ║
║ 8/15 ║ NULL ║ 65 ║ 5 ║ 55 ║
║ 8/16 ║ 10 ║ 75 ║ 5 ║ 60 ║
║ 8/17 ║ NULL ║ 75 ║ 5 ║ 65 ║
║ 8/18 ║ NULL ║ 75 ║ 5 ║ 70 ║
║ 8/19 ║ NULL ║ 75 ║ 0 ║ NULL ║
║ 8/20 ║ NULL ║ 75 ║ 0 ║ NULL ║
║ 8/21 ║ NULL ║ 75 ║ 5 ║ 75 ║
║ 8/22 ║ NULL ║ 75 ║ 5 ║ 80 ║
║ 8/23 ║ NULL ║ 75 ║ 5 ║ 85 ║
║ 8/24 ║ NULL ║ 75 ║ 5 ║ 90 ║
║ 8/25 ║ NULL ║ 75 ║ 5 ║ 95 ║
║ 8/26 ║ 10 ║ 95 ║ 5 ║ 100 ║
║ 8/27 ║ 10 ║ 95 ║ 5 ║ 105 ║
╚══════╩═══════════════╩════════════════╩═════════════════╩═══════════════╝
The problem occurs on 8/22 where we start to exceed the Rel_Units_Calc field. This allows an order to be placed on 8/27 that will not trigger the Red because the Unit_Capacity will be greater than the Rel_Units_Calc.
Sorry for the long post. I'm open to any suggestions if there is a better way to accomplish this.
Thanks in Advance,
Mike

Result from Stored proc different to SSMS Query

I have the identical code in a stored proc and in a query window, but I get different results. The only difference is that the SP is selecting into a temp table and then selecting from the temp table and that it is running in an SP.
The SP has no parameters - so its not parameter sniffing, The SP has ANSI_NULLS ON.
RecId and invoiceOrderRecId are bigints, brand is nvarchar(10)
SELECT
ZIOH.Brand,
Count(RecId) as NumDispatched
FROM
ZFSINVOICEORDERHISTORY ZIOH
WHERE
ZIOH.DISPATCHDATETIME >= CAST(CURRENT_TIMESTAMP AS DATE)
AND
ZIOH.DISPATCHDATETIME < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
AND
ZIOH.INVOICEORDERRECID NOT IN
(SELECT
RecId
FROM
ZFSINVOICEORDER ZIO
WHERE
ZIO.PARTITION = ZIOH.PARTITION)
GROUP BY ZIOH.Brand
ORDER BY ZIOH.Brand
Results from Query Window
╔═══════╦═══════════════╗
║ Brand ║ NumDispatched ║
╠═══════╬═══════════════╣
║ CCO ║ 1 ║
║ CVDUK ║ 13 ║
║ FLX ║ 12 ║
║ MSFR ║ 74 ║
║ MSGR ║ 1 ║
║ MSUK ║ 211 ║
║ PIAFR ║ 25 ║
║ PIAUK ║ 129 ║
╚═══════╩═══════════════╝
Result From SP
╔═══════╦═══════════════╗
║ Brand ║ NumDispatched ║
╠═══════╬═══════════════╣
║ CCO ║ 1 ║
║ CVDUK ║ 7 ║
║ FLX ║ 12 ║
║ MSFR ║ 53 ║
║ MSUK ║ 147 ║
║ PIAFR ║ 21 ║
║ PIAUK ║ 121 ║
╚═══════╩═══════════════╝
An Uncommitted transaction was causing the difference in results - well that's a morning lost.

SQL: Count distinct values from one column based on multiple criteria in other columns

I am trying to do count distinct values based on multiple criteria.
Sample data exercise included below.
Table1
╔════════╦════════╦══════╗
║ Bug ID ║ Status ║ Test ║
╠════════╬════════╬══════╣
║ 1 ║ Open ║ w ║
║ 2 ║ Closed ║ w ║
║ 3 ║ Open ║ w ║
║ 4 ║ Open ║ x ║
║ 4 ║ Open ║ x ║
║ 5 ║ Closed ║ x ║
║ 5 ║ Closed ║ x ║
║ 5 ║ Closed ║ y ║
║ 6 ║ Open ║ z ║
║ 6 ║ Open ║ z ║
║ 6 ║ Open ║ z ║
║ 7 ║ Closed ║ z ║
║ 8 ║ Closed ║ z ║
╚════════╩════════╩══════╝
Desired Query Results
╔══════╦═══════════╦════════════╗
║ Test ║ Open Bugs ║ Total Bugs ║
╠══════╬═══════════╬════════════╣
║ w ║ 2 ║ 3 ║
║ x ║ 1 ║ 2 ║
║ y ║ 0 ║ 1 ║
║ z ║ 1 ║ 3 ║
╚══════╩═══════════╩════════════╝
A given Bug can be found in multiple Tests, multiple times for the same Test(ex: 6), or both (ex: 5).
The following query works fine to accurately deliver 'Total Bugs'
SELECT
Test,
COUNT(DISTINCT Bug ID) AS "Total Bugs"
FROM
Table1
GROUP BY Test
My research has led me to variations on the following query. They miss the distinct bugs and therefore return the incorrect results (shown below the query) for the 'Open Bugs' column
SELECT
Test,
SUM(CASE WHEN Status <> 'Closed' THEN 1 ELSE 0 END) AS "Open Bugs"
FROM
Table1
GROUP BY Test
╔══════╦═══════════╗
║ Test ║ Open Bugs ║
╠══════╬═══════════╣
║ w ║ 2 ║
║ x ║ 2 ║
║ y ║ 0 ║
║ z ║ 3 ║
╚══════╩═══════════╝
Of course my end result must deliver both count columns in one table (rather than using separate queries as I have done for demonstration purposes).
I would like not rely on multiple subqueries because my live example will have more than two columns with counts from the same table but various criteria.
I am working with SQL Server (not sure release).
Any help is greatly appreciated.
You can have a conditional count(distinct) by using this code:
SELECT Test, COUNT(DISTINCT "Bug ID") AS "Total Bugs",
count(distinct (CASE WHEN "Status" <> 'Closed' THEN "Bug ID" END)) as "Open Bugs"
FROM Table1
GROUP BY Test
The case statement checks the condition. When true, it returns the Bug ID. When not present, it defaults to NULL, so the id does not get counted.