Adding New Records from one table with existing and new records to another table in SQL - 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.

Related

How can I summarize a set of records into one line where only one record in a column has text and all others are null?

I have invoice data stored in a SQL database and I need a summary query to bring up the Invoice Number, PO number, Date, and Invoice Amount in a single line using an MS Access Query. Unfortunately, the customer PO number is only on one line of the invoice data and pops up on the query result like this.
Invoice Date
Invoice #
PO Number
Amount
8/11/22
12345
NULL
$23.00
8/11/22
12345
456
$00.00
I need the output to look like this instead:
Invoice Date
Invoice #
PO Number
Amount
8/11/22
12345
456
$23.00
My query looks like this:
SELECT
[Invoice Date],
[Invoice #],
[PO Number],
FORMAT$(Sum([Amount])),'$#,##0.00') AS [Amount]
FROM [Invoice Details]
GROUP BY
[Invoice Date],
[Invoice #],
[PO Number]
HAVING
[INVOICE DATE] BETWEEN [8/11/2022] AND [8/11/2022]
ORDER BY
[INVOICE #]
I am still a novice when it comes to SQL queries and am not sure what I am missing here. Any help is appreciated.
Then you can exclude [PO Number] column from your GROUP BY and just take the greater value in each group. I think you can use :
SELECT
[Invoice Date],
[Invoice #],
MAX(Nz([PO Number], 0)) AS [PO Number],
FORMAT$(Sum([Amount])),'$#,##0.00') AS [Amount]
FROM [Invoice Details]
GROUP BY
[Invoice Date],
[Invoice #],
HAVING
[INVOICE DATE] BETWEEN [8/11/2022] AND [8/11/2022]
ORDER BY
[INVOICE #]
Nz is replacing Null by 0 here.
You have very limited example input, but assuming TABLE1
This will work
SELECT Amounts.[Invoice Date], Amounts.[Invoice #], [PO Numbers].[PO Number], Amounts.Amount
FROM Table1 AS Amounts
INNER JOIN Table1 AS [PO Numbers]
ON (Amounts.[Invoice Date] = [PO Numbers].[Invoice Date]) AND (Amounts.[Invoice #] = [PO Numbers].[Invoice #])
WHERE ((([PO Numbers].[PO Number]) Is Not Null) AND ((Amounts.[PO Number]) Is Null));
Looks like this in the query window

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

Query to aggregate contract line-item values across contracts

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]

SSRS Report - Complex Layout?

I need to create a report, but I'm unsure how to get the layout correct.
The way the report should look is Here
The data is currently formatted like so:
Customer No | Sale Price | Cost Price | Margin | Date
Customer A | 200 | 100 | 100 | 1/1/14
Is it possible to design a report with this layout? I suspect so, but I haven't encountered this issue yet.
Any hints and tips to get me on my way?
Thanks!
Since what you want is actually a cross-tab, it is easier if your dataset is structured like this:
Customer No Price Type Date Amount
----------- ---------- ------- ------
Customer A Sale 1/1/14 200
Customer A Cost 1/1/14 100
Customer A Margin 1/1/14 100
...
To achieve this, simply use UNION ALL in the SQL statement of your dataset, like so:
SELECT [Customer No], 'Sale' AS [Price Type], [Date], [Sale Price]
FROM MyTable
UNION ALL
SELECT [Customer No], 'Cost' AS [Price Type], [Date], [Cost Price]
FROM MyTable
UNION ALL
SELECT [Customer No], 'Margin' AS [Price Type], [Date], [Margin]
FROM MyTable
With a dataset like this, it is straightforward to get the report layout you want, for example using the Tablix Wizard.
I managed to get it working. It actually wasn't too difficult, to my surprise. This probably isn't the best way to do things, but it worked well for me!
Query
SELECT sa.[Document No_]
,[sa.Customer No_]
,DATEPART(m, sa.[Posting Date]) AS MonthName
,sa.Quantity
,sa.[Amount (LCY)]
,sa.[Cost (LCY)]
,sa.[Profit (LCY)]
,c.[Salesperson Code]
,c.NAME
FROM [Sales Analysis] AS sa
INNER JOIN [Customer] AS c ON c.[No_] = sa.[customer no_]
WHERE [Posting Date] BETWEEN '2014-01-01' AND '2014-05-31'
AND [Customer No_] IS NOT NULL
AND [Customer No_] <> ''
Matrix Layout
Results
Thanks for the help!

SQL Result Set Merge

I have a limitation where I can only send one result set to a reporting application at any one time, to produce an end report for a customer.
So a query like this
select
[AGENT],
[TRANSDATE],
[RECIPT NO],
[CUSTOMER NAME],
[ORDER NO] ,
[TRANS NO] ,
QUANTITY,
[AMOUNT COST],
From [Customer] C
However I need lots of totals at the bottom such as this query for some of the columns. I cannot make any changes to front end due to it being a legacy reporting application.
select
Sum ( QUANTITY ) as [SUM OF QUANTITY] ,
Sum ( AMOUNT COST ) AS [SUM OF AMOUNT COST]
From [Customer] C
Obviously I simplified the queries I am using. So the question is how to make 2 results sets one result set in SQL?
Union and union all failed due to date columns being defaulted if you use blank for a column in end application.
Rollup or Pivoting or CTE I kinda thought of but cannot see a solution yet.
what about windowed functions?
like...
select
[AGENT],
[TRANSDATE],
[RECIPT NO],
[CUSTOMER NAME],
[ORDER NO] ,
[TRANS NO] ,
QUANTITY,
[AMOUNT COST],
Sum ( QUANTITY ) over () as [SUM OF QUANTITY] ,
Sum ( [AMOUNT COST] ) over () AS [SUM OF AMOUNT COST]
From [Customer] C