Query to aggregate contract line-item values across contracts - sql

I have 2 tables in Microsoft Access: Contract_1 & Contract Items
Contract_1 has columns like: Contract ID, Contract Value, Date, Committed Prod_ID, an example of their values will be:
1 $100,000 3/5/15 111
Contract Items has Prod ID, Product, Unit Price an example of their values will be:
111 Light Bulb $5.00
They are linked by a One to Many relationship, Prod ID {PK} to Committed Prod_ID in Contract_1.
When I write a query to show a summary of the products and total price, I get
Product| Unit Price| Quantity| Total Price|
Light Bulb| $5.00| 2| $10.00
Light Bulb| $5.00| 3| $15.00
In my data i have 2 Contracts, contract ID 1 and 5, that both purchased light bulbs. But since they are the same product, how can I make them appear as a total of 5 instead of 3 and 2?
I tried using GROUP BY, but it does not work. My current query looks like this:
SELECT [Contract Items].Product,
[Contract Items].[Unit Price],
Contract_1.[Actual Qty] AS Quantity,
[Unit Price]*[Quantity] AS [Total Price]
FROM [Contract Items]
INNER JOIN Contract_1
ON [Contract Items].[Prod ID]=Contract_1.[Committed Prod_ID]
GROUP BY [Contract Items].Product,
[Contract Items].[Unit Price],
Contract_1.[Actual Qty],
[Contract Items].[Quantity];
Thanks!

Try this(EDITED), what #jarlh is saying :
SELECT [Contract Items].Product,
[Contract Items].[Unit Price],
SUM(Contract_1.[Actual Qty]) AS Quantity,
SUM([Contract Items].[Unit Price]*Contract_1.[Actual Qty]) AS [Total Price]
FROM [Contract Items]
INNER JOIN Contract_1
ON [Contract Items].[Prod ID]=Contract_1.[Committed Prod_ID]
GROUP BY [Contract Items].Product,
[Contract Items].[Unit Price];

In that case first get the count and then do a join and calculate the total price like below
SELECT [Contract Items].Product,
[Contract Items].[Unit Price],
tab.Quantity,
[Contract Items].[Unit Price] * tab.[Quantity] AS [Total Price]
FROM [Contract Items]
INNER JOIN
(
SELECT Committed Prod_ID,
count(*) as Quantity
FROM Contract_1
GROUP BY Committed Prod_ID
) tab ON [Contract Items].[Prod ID]=tab.[Committed Prod_ID]

Related

Brand new to SQL, looking to find total sales on a specific day

I have the following:
SELECT [Sales_Line_Item].[Sales Date],
[Sales_Line_Item].[Quantity]*[Sales_Line_Item].[Unit Price] AS [Total Sales]
FROM Sales_Line_Item
WHERE [Sales Date] = #9/1/2020#;
Which displays:
I want to add the total sales together so that the result is:
Sales Date Total Sales
9/1/2020 $6,276.00
Thank you for any help
I wanted to use SUM(Total Sales) someway but I'm missing an operator
You can do:
SELECT [Sales_Line_Item].[Sales Date],
SUM([Sales_Line_Item].[Quantity]*[Sales_Line_Item].[Unit Price]) AS [Total Sales]
FROM Sales_Line_Item
WHERE [Sales Date] = #9/1/2020#
GROUP BY [Sales Date]

Meeting 2 conditions in 2 different columns

I'm trying to run a query where I need very specific conditions to be met:
Sales code is All
Item has Original Price flag set
Item has a price with no Original Price flag set that is the same as the Price with Original Price flag set
Price without Original price flag set must be created after the price with Original price flag
Currently I am using the following query to get the information I need;
select [item no_], [variant code],[unit price including vat],
[original price], [Starting Date], [Ending Date] from [Sales Price]
where [Sales Code] = 'all'
and [Ending Date] = '1753-01-01 00:00:00.000'
This is the example result:
1 means Original Price flag is set and 0 means it is not
The result I need from this query would be to only show these two:
I am assuming you are working with SQL Server as your current query syntax suggests.
If, so you can use lag() :
select sp.*
from (select sp.*,
lag([original price]) over (partition by [item no_] order by [Starting Date]) as prev_price
from [Sales Price] sp
where [Sales Code] = 'all'
) sp
where ([original price] = 1 or prev_price = 1);
let me know if you need me to explain; else its pretty straight forward.
select a.*
from (
select [item no_]
, [variant code]
,[unit price including vat]
, [original price]
, [Starting Date]
, [Ending Date]
,Column_Test = case when ( [original price] = 1 and [original price] = 0 ) and ([Starting Date]<[Ending Date]) then 1 else 0 end
from [Sales Price]
where [Sales Code] = 'all'
and [Ending Date] = '1753-01-01 00:00:00.000'
) a
where Column_Test = 1

Aggregated sub query inside other aggregated query

I use Microsoft VBA together with Excel and also use the ADODB connection to treat the Sheet as database.
I have one issue regarding a SQL aggregated query that uses also aggregated sub queries. The issue is that I cannot use it like this, due to the fact it is throwing errors and I don't know how to change it
The SQL query:
Select inv.[Region], inv.[Org Name], inv.[Bill To Customer Number], inv.[Bill To Customer Name],
SUM(inv.[AR Global Total Amount]) as "Amount Invoiced",
COUNT(pay.[Sales Invoice Number]) as "Count Invoices",
SUM(inv.[AR Global Total Amount]*(inv.[Payment Due Fiscal Date]-inv.[Invoiced Fiscal Date])) as "Sum of Terms Mult",
SUM(inv.[AR Global Total Amount]*(pay.GL_Min-inv.[Invoiced Fiscal Date])) as "Sum of Pay Days Mult",
SUM(inv.[AR Global Total Amount]*(pay.GL_Min-inv.[Payment Due Fiscal Date])) as "Sum of Days Late Mult"
FROM
(
Select *
From [Data$] as inv
WHERE [AR Transaction Sub Type] IN ('Inv', 'Inv-T')
) a,
(
Select [Region], [Org Name], [Bill To Customer Number], [Bill To Customer Name], [Sales Invoice Number], Min([GL Fiscal Date]) as GL_Min
FROM [Data$] as pay
WHERE [AR Transaction Sub Type] IN ('Cash', 'Cash-T')
GROUP BY [Region], [Org Name], [Bill To Customer Number], [Bill To Customer Name], [Sales Invoice Number]
) t
WHERE inv.[Sales Invoice Number] = pay.[Sales Invoice Number] AND inv.[Org Name] = pay.[Org Name] AND inv.[AR Global Total Amount]>0
GROUP BY inv.[Region], inv.[Org Name], inv.[Bill To Customer Number], inv.[Bill To Customer Name]
ORDER BY SUM(inv.[AR Global Total Amount]) DESC
The problem is on the second sub query, the one where I try to capture the min date.
Could someone point me to a proper syntax?
Thanks!
Your query seems correct except subquery alias. Could you try this?
Select inv.[Region], inv.[Org Name], inv.[Bill To Customer Number], inv.[Bill To Customer Name],
SUM(inv.[AR Global Total Amount]) as "Amount Invoiced",
COUNT(pay.[Sales Invoice Number]) as "Count Invoices",
SUM(inv.[AR Global Total Amount]*(inv.[Payment Due Fiscal Date]-inv.[Invoiced Fiscal Date])) as "Sum of Terms Mult",
SUM(inv.[AR Global Total Amount]*(pay.GL_Min-inv.[Invoiced Fiscal Date])) as "Sum of Pay Days Mult",
SUM(inv.[AR Global Total Amount]*(pay.GL_Min-inv.[Payment Due Fiscal Date])) as "Sum of Days Late Mult"
FROM
(
Select *
From [Data$] as inv
WHERE [AR Transaction Sub Type] IN ('Inv', 'Inv-T')
) inv,
(
Select [Region], [Org Name], [Bill To Customer Number], [Bill To Customer Name], [Sales Invoice Number], Min([GL Fiscal Date]) as GL_Min
FROM [Data$] as pay
WHERE [AR Transaction Sub Type] IN ('Cash', 'Cash-T')
GROUP BY [Region], [Org Name], [Bill To Customer Number], [Bill To Customer Name], [Sales Invoice Number]
) pay
WHERE inv.[Sales Invoice Number] = pay.[Sales Invoice Number] AND inv.[Org Name] = pay.[Org Name] AND inv.[AR Global Total Amount]>0
GROUP BY inv.[Region], inv.[Org Name], inv.[Bill To Customer Number], inv.[Bill To Customer Name]
ORDER BY SUM(inv.[AR Global Total Amount]) DESC

Adding New Records from one table with existing and new records to another table in SQL

I'm trying to to append data to a table that contains all the data up to this point. Every week I will be pulling in the new data (which will contain data already existing in the All table) and adding the new records. I added a few test data to the temp table where the generic, material num, etc. are all different but when I run this query it still says it is adding 0 records. Please help.
INSERT INTO ExtWafersAll ( generic, [material number], description, vendor, [net price], [std price], NumberOfDups )
SELECT
ExtWafersTemp.generic,
ExtWafersTemp.[material number],
ExtWafersTemp.description,
ExtWafersTemp.vendor,
ExtWafersTemp.[net price],
ExtWafersTemp.[std price],
ExtWafersTemp.NumberOfDups
FROM ExtWafersTemp
RIGHT JOIN ExtWafersAll
ON (ExtWafersAll.NumberOfDups = ExtWafersTemp.NumberOfDups)
AND (ExtWafersAll.[std price] = ExtWafersTemp.[std price])
AND (ExtWafersAll.[net price] = ExtWafersTemp.[net price])
AND (ExtWafersAll.vendor = ExtWafersTemp.vendor)
AND (ExtWafersAll.description = ExtWafersTemp.description)
AND (ExtWafersAll.[material number] = ExtWafersTemp.[material number])
AND (ExtWafersAll.generic = ExtWafersTemp.generic)
WHERE
ExtWafersTemp.vendor <> ExtWafersAll.vendor
OR ExtWafersTemp.description <> ExtWafersAll.description
OR ExtWafersTemp.[material number] <> ExtWafersAll.[material number]
OR ExtWafersTemp.generic <> ExtWafersAll.generic;
So for example in ExtWafersTemp we have:
Generic Material Number Description Vendor Net Price Std Price
j2151 sjkdga215 xxx125125 TMA 12 14
asdg asgasg aggsggs asg 15 18
And then in ExtWafersAll:
Generic Material Number Description Vendor Net Price Std Price
j2151 sjkdga215 xxx125125 TMA 12 14
I can't figure out how to add the new record thats in the temp to the all file
Maybe this would suit your need:
insert into ExtWafersAll ( generic, [material number], description, vendor, [net price], [std price], NumberOfDups )
select generic, [material number], description, vendor, [net price], [std price], NumberOfDups
from ExtWafersTemp
except
select generic, [material number], description, vendor, [net price], [std price], NumberOfDups
from ExtWafersAll;
In above snippet you add records from ExtWafersTemp table which are not present in ExtWafersAll table. Is this what are you trying to achieve?
About "except" operator you could read here: http://en.wikipedia.org/wiki/Set_operations_%28SQL%29
UPDATE
As it occurred to be MS Access problem you could try to test this:
SELECT
ExtWafersTemp.generic,
ExtWafersTemp.[material number],
ExtWafersTemp.description,
ExtWafersTemp.vendor,
ExtWafersTemp.[net price],
ExtWafersTemp.[std price],
ExtWafersTemp.NumberOfDups
FROM ExtWafersAll RIGHT JOIN ExtWafersTemp
ON (ExtWafersAll.NumberOfDups = ExtWafersTemp.NumberOfDups
AND ExtWafersAll.[std price] = ExtWafersTemp.[std price]
AND ExtWafersAll.[net price] = ExtWafersTemp.[net price]
AND ExtWafersAll.vendor = ExtWafersTemp.vendor
AND ExtWafersAll.description = ExtWafersTemp.description
AND ExtWafersAll.[material number] = ExtWafersTemp.[material number]
AND ExtWafersAll.generic = ExtWafersTemp.generic)
WHERE ExtWafersAll.NumberOfDups is null
AND ExtWafersAll.[std price] is null
AND ExtWafersAll.[net price] is null
AND ExtWafersAll.vendor is null
AND ExtWafersAll.description is null
AND ExtWafersAll.[material number] is null
AND ExtWafersAll.generic is null
Genarally it is a following pattern (in example there is a primary key field - id):
select tt.id
from tableall t right join tabletemp tt
on (t.id = tt.id)
where t.id is null
Hope that it helps.

I am looking for a union query to produce one total not two..ideas?

I am not the greatest at writing SQL. I wrote this in Access and then went to SQL view. Took both queries and did a union all between them. I keep getting a total from each query. How do I get just one total for each product line.
Results:
Prod Line Period Amount
Cash Discounts 12 -1404010.46
CASH DISCOUNTS 12 1541.19
Freight 12 4050823.43
Freight 12 6817.27
INK 12 -24467.76
INK 12 44414.29
Want
Cash discounts -1402469.27
Freight 405764.70
INK 24467.76
SQL
SELECT [JE Details].[Prod Line], [JE Details].Period, Sum([JE Details].Amount) AS Amount
FROM [JE Details]
GROUP BY [JE Details].[Prod Line], [JE Details].Period
HAVING ((([JE Details].Period)=12))
UNION ALL
SELECT [AP Details].[Product Line], [AP Details].[Fiscal Period], Sum([AP Details].
[Invoice Amt]) AS Amount
FROM [AP Details]
GROUP BY [AP Details].[Product Line], [AP Details].[Fiscal Period]
HAVING ((([AP Details].[Fiscal Period])=12));
How about:
SELECT [Prod Line], Sum(Amount)
FROM
(SELECT [Prod Line], Amount
FROM [JE Details]
WHERE Period=12
UNION ALL
SELECT [Product Line] ,[Invoice Amt]
FROM [AP Details]
WHERE [Fiscal Period]=12) q
GROUP BY [Product Line]