Querying from multiple linked Excel files - sql

I currently have linked multiple Excel files to Access (one file for each month) and I want to create a query from each file that will give me a "master query." All the files are exactly the same with the headings and format. I cannot merge all three and then link because it is well over the Excel Maximum.
I tried this and it gives me the error Data type mismatch in criteria expression
SELECT [Created Date], [Store Name], ProductID, [Customer First Name] & " " & [Customer Last Name] AS CustomerName
FROM August2015
WHERE ProductID = 1587996 OR ProductID = 1587985
UNION
SELECT [Created Date], [Store Name], ProductID, [Customer First Name] & " " & [Customer Last Name] AS CustomerName
FROM July2015
WHERE ProductID = 1587996 OR ProductID = 1587985
UNION
SELECT [Created Date], [Store Name], ProductID, [Customer First Name] & " " & [Customer Last Name] AS CustomerName
FROM June2015
WHERE ProductID = 1587996 OR ProductID= 1587985
Can someone point me in the right direction. Thanks.

Sounds like ProductID is a string. If so:
WHERE ProductID = '1587996' OR ProductID = '1587985'

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

Selecting values only for last day of month

I have a query that's supposed to grab only the value for the last day of the month from all my existing records (so the last day of multiple months). I'm unable to get the value I'm needing for ONLY the last day, due to using a SUM() clause. This is adding all the values for the whole month together. Can I get only the value for the last day another way? Is there something I'm missing?
Code so far:
SELECT
ID,
[Customer Name],
Year([ValDate]) & IIf(Len(Month([ValDate]))>1,Month([ValDate]),"0" & Month([ValDate])) AS BalMonth,
Sum(Value) AS LastValue
FROM
Archive
GROUP BY
ID,
[Customer Name],
Year([ValDate]),
Month([ValDate])
ORDER BY
ID,
Year(ValDate) & Month(ValDate)
Other Code tested:
SELECT
ID,
[Customer Name],
YEAR([ValDate]) & MONTH([ValDate]) & MAX(DAY([BalDate])) AS LastDayofMonth
FROM
Archive
GROUP BY
ID ,
[Customer Name],
YEAR([ValDate]),
MONTH([ValDate])
ORDER BY
ID,
[Customer Name],
YEAR([ValDate]),
MONTH([ValDate])
The second section of code didn't work as it produces the dates in YYYYMMDD format. This makes it so it doesn't allow proper ordering of the dates. Instead, the dates are being ordered as 1 , 10 , 11 , 12 , 2 , 3 , 4 , etc.
If anything is unclear I'll try my best to clarify, just let me know!
Try something like:
SELECT
ID,
[Customer Name],
[ValDate] AS LastDayofMonth,
Sum([FieldToSum]) As Total
FROM
Archive
GROUP BY
ID,
[Customer Name],
[ValDate]
HAVING
[ValDate] = DateSerial(Year([ValDate]), Month([ValDate]) + 1, 0)
Edit:
To have a formatted date output, try this:
SELECT
ID,
[Customer Name],
Format([ValDate], "yyyymmdd") AS LastDayofMonth,
Sum([FieldToSum]) As Total
FROM
Archive
WHERE
[ValDate] = DateSerial(Year([ValDate]), Month([ValDate]) + 1, 0)
GROUP BY
ID,
[Customer Name],
Format([ValDate], "yyyymmdd")

Parameter values

can anyone tell me why the following query is asking me for parameter values. It should be (or at least I want to) populating the table with data 'from' where it's pulling it from:
SELECT [BILLING_REJECTS_orig].[ORG NBR] AS BK,
[BILLING_REJECTS_orig].[ACCOUNT NUMBER] AS ACCT,
[BILLING_REJECTS_orig].APPL AS AP,
[BILLING_REJECTS_orig].[ACCOUNT NAME] AS [ACCT NAME],
[BILLING_REJECTS_orig].[TMO NAME],
IIf(Len(DatePart("m", [BILLING_REJECTS_orig]![REPORT DATE])) = 1, Year([BILLING_REJECTS_orig]![REPORT DATE]) & "-" & "0" & Month([BILLING_REJECTS_orig]![REPORT DATE]), Year([BILLING_REJECTS_orig]![REPORT DATE]) & "-" & Month([BILLING_REJECTS_orig]![REPORT DATE])) AS [ACTIVITY MONTH]
INTO Billing_Rejects_Orig
FROM dbo_BILLING_REJECTS_DEPT,
TM_Report_Date
WHERE (
((Year([Billing_Rejects_Orig]![REPORT DATE])) = Year([TM_Report_Date]![Report_Date]))
AND ((Month([Billing_Rejects_Orig]![REPORT DATE])) = Month([TM_Report_Date]![Report_Date]))
);
As #WEI_DBA points out with incorrect reference to table, consider using table aliases as shown with b and t. This cuts down SQL code and is a helpful tool in maintainability as you can then switch out the table name in FROM or JOIN clauses without a whole re-write of query, assuming same structured table.
Especially in MS Access being a default backend database (Jet/ACE) that can be switched out with other RDBMS's (SQL Server, MySQL, etc.) and occasionally used for prototyping, table aliases can help in migration between both linked and local tables.
SELECT b.[ORG NBR] AS BK,
b.[ACCOUNT NUMBER] AS ACCT,
b.APPL AS AP,
b.[ACCOUNT NAME] AS [ACCT NAME],
b.[TMO NAME],
IIf(Len(DatePart('m', b.[REPORT DATE])) = 1,
Year(b.[REPORT DATE]) & '-' & '0' & Month(b.[REPORT DATE]),
Year(b.[REPORT DATE]) & '-' & Month(b.[REPORT DATE])) AS [ACTIVITY MONTH]
INTO Billing_Rejects_Orig
FROM dbo_BILLING_REJECTS_DEPT b,
TM_Report_Date t
WHERE (
((Year(b.[REPORT DATE])) = Year(t.[Report_Date]))
AND ((Month(b.[REPORT DATE])) = Month(t.[Report_Date]))
);

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.

MS Query with multiple Parameters

I have this SQL Query and i want the results in an Excel 2013 sheet / table
SELECT
[MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
FROM
(
SELECT
[MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
FROM
vwSPLAInformatie
WHERE
(DATEPART(YEAR, fakdat) = '2013')
AND (DATEPART(QUARTER, fakdat) = '1')
AND docnumber LIKE '%kwartaal%'
GROUP BY
[MS Part Number], [Item Name], [Aantal], [Prijs]
UNION ALL
SELECT
[MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
FROM
vwSPLAInformatie
WHERE
(DATEPART(YEAR, fakdat) = '2013')
AND (DATEPART(MONTH, fakdat) = '1')
AND NOT docnumber LIKE '%kwartaal%'
GROUP BY
[MS Part Number], [Item Name], [Aantal], [Prijs]
) AS Basis
GROUP BY
[MS Part Number], [Item Name], [Prijs]
It works perfectly, the only thing is that i have 4 parameters that i want to connect to 3 cell values in Excel.
(Year = 2013) (Quarter = 1) (Month = 2). But when i adjust my Query to the following:
WHERE (DATEPART(YEAR, fakdat) = '?')
And i select the cell in Excel that says 2013, so basicly it should work, but it doensn't. I get the following error:
[Microsoft]ODBC SQL Server][SQL Server]Conversion failed when converting the varchar value '?' to data type in.
The value is the same, but it does not work at all. How can i fix this? I would be extremely happy when this works! In forward, many thanks.