I'm a bit confused why my ACCESS SQL query would act differently... When I do a SELECT query with the NOW() parameter, I get the right result. But if I use the TempVar!Date with todays date it returns 0...The queries look as follows:
This one works:
SELECT [Account Transactions]., Categories.,
IIf([Categories].[Income/Expense]="Expense",-([Account
Transactions]![Transaction Amount]),[Account
Transactions]![Transaction Amount]) AS [Actual Amount] FROM [Account
Transactions] LEFT JOIN Categories ON [Account Transactions].Category
= Categories.ID WHERE month([Account Transactions].[Entry Date]) = month(Now());
This one doesn't work:
SELECT [Account Transactions]., Categories., IIf([Categories].
[Income/Expense]="Expense",-([Account Transactions]![Transaction
Amount]),[Account Transactions]![Transaction Amount]) AS [Actual
Amount] FROM [Account Transactions] LEFT JOIN Categories ON [Account
Transactions].Category = Categories.ID WHERE month([Account
Transactions].[Entry Date]) = month([TempVars]![Date]);
I need the second one - with my variable to work.bYour help will be greatly appreciated. I've tested the tempvar value, and it's fine.
Try this:
SELECT [Account Transactions].*, Categories.*, IIf([Categories].[Income/Expense]="Expense",-([Account Transactions]![Transaction Amount]),[Account Transactions]![Transaction Amount]) AS [Actual Amount]
FROM [Account Transactions]
LEFT JOIN Categories ON [Account Transactions].Category = Categories.ID
WHERE month([Account Transactions].[Entry Date]) = month([TempVars]![Date]);
I've removed some extra spaces and added in asterisks where needed.
Your TempVars values will only last for your current open session of MS Access. What do you get when you enter ?Application.TempVars("Date").Value in your VBA window?
Related
I'm trying to run the following query through SQL Server. I keep getting a incorrect syntax error near',' but I can't figure out which comma is incorrect. I'm pretty new to SQL but especially still trying to figure out more complex queries.
SELECT
lastdate.[Date of Record],
billing.[Club Code],
billing.[Club Name],
lastdate.[Member Code with Name],
billing.[Activity Code],
billing.[Category Code],
billing.[Dues Net Amount],
billing.[Dues Gross Amount],
billing.[Member Type Code],
billing.[Member Join Date],
billing.[Member Status Rule Code]
FROM
[dbo].[view_Club_Transactions_0100_(15) Dues_Summary] billing
LEFT JOIN
(MAX(lastdate.[Date of Record]) dor,
lastdate.[Member Code with Name]
FROM
[dbo].[view_Club_Transactions_0100_(15) Dues_Summary] lastdate
GROUP BY
lastdate.[Member Code with Name])
ON
billing.[Member Code with Name]=lastdate.[Member Code with Name]
WHERE
([Member Status Rule Code] = N'ZRESIGN')
AND
([Activity Code] = N'DUES')
Your subquery is missing the SELECT keyword... And an alias too. You also need to align the subquery column alias for the date column with the outer query:
SELECT
lastdate.[Max Date of Record], ---------------> column alias
billing.[Club Code],
...
FROM [dbo].[view_Club_Transactions_0100_(15) Dues_Summary] billing
LEFT JOIN (
SELECT -------------------------------> "SELECT" keyword
MAX([Date of Record]) [Max Date of Record], ---> column alias
[Member Code with Name]
FROM [dbo].[view_Club_Transactions_0100_(15) Dues_Summary]
GROUP BY [Member Code with Name]
) lastdate -------------------------------> subquery alias
ON billing.[Member Code with Name]=lastdate.[Member Code with Name]
WHERE ...
I actually suspect that you can skip the self join and use window functions instead. That could be:
SELECT *
FROM (
SELECT
MAX([Date of Record]) OVER(PARTITION BY [Member Code with Name]) [Max Date of Record],
[Club Code],
[Club Name],
[Member Code with Name],
[Activity Code],
[Category Code],
[Dues Net Amount],
[Dues Gross Amount],
[Member Type Code],
[Member Join Date],
[Member Status Rule Code]
FROM [dbo].[view_Club_Transactions_0100_(15) Dues_Summary]
) t
WHERE [Member Status Rule Code] = N'ZRESIGN' AND [Activity Code] = N'DUES'
Below is the Query,I had joined 3 tables supplier is main table. The actual scenario is I want all the data from table account payable table though it is not their in purchase order table, so I have joined using FULL Outer with supplier and Purchase Order, but the supplier details were not coming against the data of account payable though supplier key is available.
SELECT ISNULL(dbo.Supplier.supplier_key,dbo.Fact_AccountPayables.supplier_key) AS supplier_key,
dbo.Supplier.Supplier,
dbo.Supplier.Name,
dbo.Supplier.Status,
dbo.Supplier.AddressCode,
dbo.Supplier.Address,
dbo.Supplier.HouseNo,
dbo.Supplier.Street,
dbo.Supplier.City,
dbo.Supplier.Country,
dbo.Supplier.ZipCode,
dbo.Supplier.StartDate,
dbo.Supplier.CreditLimit,
dbo.Supplier.FinancialGroup,
dbo.Supplier.LastTransactionDate,
dbo.Fact_PurchaseOrder.Company,
ISNULL(dbo.Fact_PurchaseOrder.[Purchase Order],dbo.Fact_AccountPayables.[PO Number]) AS PurchaseOrder,
ISNULL( dbo.Fact_PurchaseOrder.Sequence,dbo.Fact_AccountPayables.Line) AS POSequence,
dbo.Fact_PurchaseOrder.[Order Quantity],
dbo.Fact_PurchaseOrder.[Per Purchase Unit],
dbo.Fact_PurchaseOrder.[Per Quantity Price],
dbo.Fact_PurchaseOrder.[Purchase price unit],
dbo.Fact_PurchaseOrder.[Total Order Amount],
dbo.Fact_PurchaseOrder.Currency,
dbo.Fact_PurchaseOrder.[Rate Date],
dbo.Fact_PurchaseOrder.[Actual Receipt Date],
dbo.Fact_PurchaseOrder.[Receipt No],
dbo.Fact_PurchaseOrder.[Receipt Sequence],
dbo.Fact_PurchaseOrder.[Received Quantity],
dbo.Fact_PurchaseOrder.[Approved Quantity],
dbo.Fact_PurchaseOrder.[Purchase Office],
dbo.Fact_PurchaseOrder.[Invoice Number],
dbo.Fact_PurchaseOrder.[Invoice Date],
dbo.Fact_PurchaseOrder.[Invoice Quantity],
dbo.Fact_PurchaseOrder.[Invoice Amount],
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.Type AS InvoiceType,
dbo.Fact_AccountPayables.[Order Type] AS OrderInvoiceType,
dbo.Fact_AccountPayables.AP_Balance_EUR,
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
dbo.Fact_AccountPayables.supplier_key AS EXPR2,
dbo.Fact_AccountPayables.[IntercompanyTrade Order No] AS EXPR23,
dbo.Fact_AccountPayables.[IntercompanyTrade Line Number] AS EXPR24,
dbo.Fact_AccountPayables.[Intercompany Trade Financial Company] AS EXPR25,
dbo.Fact_AccountPayables.[Intercompany Trade Purchase Company] AS EXPR26,
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.DueDate,
dbo.Fact_AccountPayables.DocDate,
dbo.Fact_PurchaseOrder.[Order Date],
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
(CASE WHEN dbo.Fact_PurchaseOrder.[Receipt No] = ' ' THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
WHEN dbo.Fact_PurchaseOrder.[Receipt No] != ' ' and dbo.Fact_AccountPayables.InvoiceNumber IS NULL then dbo.Fact_PurchaseOrder.[Total Order Amount] END) AS ORDERBALANCE,
(dbo.Supplier.CreditLimit -(ORDERBALANCE + dbo.Fact_AccountPayables.[Invoice Amount_EUR])) AS Availablecredit
FROM dbo.Supplier
LEFT OUTER JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
full OUTER JOIN dbo.Fact_AccountPayables ON dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
The output is like this:
Try your Payable table after that FROM Clause and join the other tables by using LEFT JOIN
FROM dbo.Fact_AccountPayables
LEFT JOIN dbo.Fact_PurchaseOrder ON dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
LRFT JOIN dbo.Supplier ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key AND dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key
You have used not only supplier key, but also PO number and PO line as join criterion with full outer join. If there is no matching entry for these (matching entries for ALL join criteria in the same time) in supplier table you will also see the null values you mentioned.
Just for testing remove temporarily the other join criteria (PO number and line) and leave only the supplier key. Check if you still see the null values in this case.
You're using a FULL join for the Fact_AccountPayables table. This way, all records from Fact_AccountPayables are included even if there are no matches with the other two tables. If you don't want that, use LEFT join instead of FULL join.
SELECT
ISNULL(dbo.Supplier.supplier_key, dbo.Fact_AccountPayables.supplier_key) AS supplier_key,
dbo.Supplier.Supplier,
dbo.Supplier.Name,
dbo.Supplier.Status,
dbo.Supplier.AddressCode,
dbo.Supplier.Address,
dbo.Supplier.HouseNo,
dbo.Supplier.Street,
dbo.Supplier.City,
dbo.Supplier.Country,
dbo.Supplier.ZipCode,
dbo.Supplier.StartDate,
dbo.Supplier.CreditLimit,
dbo.Supplier.FinancialGroup,
dbo.Supplier.LastTransactionDate,
dbo.Fact_PurchaseOrder.Company,
ISNULL(dbo.Fact_PurchaseOrder.[Purchase Order], dbo.Fact_AccountPayables.[PO Number]) AS PurchaseOrder,
ISNULL(dbo.Fact_PurchaseOrder.Sequence, dbo.Fact_AccountPayables.Line) AS POSequence,
dbo.Fact_PurchaseOrder.[Order Quantity],
dbo.Fact_PurchaseOrder.[Per Purchase Unit],
dbo.Fact_PurchaseOrder.[Per Quantity Price],
dbo.Fact_PurchaseOrder.[Purchase price unit],
dbo.Fact_PurchaseOrder.[Total Order Amount],
dbo.Fact_PurchaseOrder.Currency,
dbo.Fact_PurchaseOrder.[Rate Date],
dbo.Fact_PurchaseOrder.[Actual Receipt Date],
dbo.Fact_PurchaseOrder.[Receipt No],
dbo.Fact_PurchaseOrder.[Receipt Sequence],
dbo.Fact_PurchaseOrder.[Received Quantity],
dbo.Fact_PurchaseOrder.[Approved Quantity],
dbo.Fact_PurchaseOrder.[Purchase Office],
dbo.Fact_PurchaseOrder.[Invoice Number],
dbo.Fact_PurchaseOrder.[Invoice Date],
dbo.Fact_PurchaseOrder.[Invoice Quantity],
dbo.Fact_PurchaseOrder.[Invoice Amount],
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.Type AS InvoiceType,
dbo.Fact_AccountPayables.[Order Type] AS OrderInvoiceType,
dbo.Fact_AccountPayables.AP_Balance_EUR,
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
dbo.Fact_AccountPayables.supplier_key AS EXPR2,
dbo.Fact_AccountPayables.[IntercompanyTrade Order No] AS EXPR23,
dbo.Fact_AccountPayables.[IntercompanyTrade Line Number] AS EXPR24,
dbo.Fact_AccountPayables.[Intercompany Trade Financial Company] AS EXPR25,
dbo.Fact_AccountPayables.[Intercompany Trade Purchase Company] AS EXPR26,
dbo.Fact_AccountPayables.InvoiceNumber,
dbo.Fact_AccountPayables.DueDate,
dbo.Fact_AccountPayables.DocDate,
dbo.Fact_PurchaseOrder.[Order Date],
dbo.Fact_AccountPayables.[Invoice Amount_EUR],
CASE
WHEN dbo.Fact_PurchaseOrder.[Receipt No] = ' ' THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
WHEN dbo.Fact_AccountPayables.InvoiceNumber IS NULL THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
END AS ORDERBALANCE,
dbo.Supplier.CreditLimit - (ORDERBALANCE + dbo.Fact_AccountPayables.[Invoice Amount_EUR]) AS Availablecredit
FROM
dbo.Supplier
LEFT JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
LEFT JOIN dbo.Fact_AccountPayables ON
dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
Some of the rows are getting filtered in the Fact_PurchaseOrder, if they dont have supplier. You need to go for FULL OUTER JOIN for all tables, to get data for all rows of AccountPayables, irrespective of whether they have got suppliers or not.
FROM dbo.Supplier
FULL OUTER JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
FULL OUTER JOIN dbo.Fact_AccountPayables ON dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order] AND
dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
I am using 3 tables in the query: 2 with INNER JOIN and the 3rd with LEFT JOIN.
However, I am getting some NULLrecords while the LEFT JOIN condition is executing. There are coming from the Purchase line(PL_ TABLE)`which should not be the case.
Can any one recommend the change in the query to replace NULL values in the record with actual values?
Sorry, I could not find any option to attach the sample table data.
The Query
SELECT
pih.[PO Number],
pih.[Pre-Assigned No_] as [Invoice No],
pil.[Document No_],
pil.[Description] as [Reason For Discrepency],
pil.[Line No_],
pl.[No_] as [Item No],
pl.[Vendor Item No_],
pl.[Order Date],
pil.[Posting Date],
pil.[Expected Receipt Date],
pih.[Notes] as [Header Notes],
pil.[No_] as [G/L Account No],
pih.[Buy-from Vendor No_],
pih.[Buy-from Vendor Name],
Pil.Quantity as [Inv Qty From InvoiceLine],
pil.[Amount Including VAT] as [Inv Value From InvoiceLine],
pl.Quantity as [PO Quantity From Purchaseline],
pl.[Quantity Received] as [Received Qty From PurchaseLine],
pl.[Quantity Invoiced] as [Invoiced Qty From PurchaseLine] ,
pl.[Amount Including VAT] as [PO Value From PurchaseLine]
FROM
[Purch_ Inv_ Line] pil
INNER JOIN Purch_ Inv_ Header] pih
ON pil.[Document No_] = pih.[No_]
LEFT JOIN [Purchase Line] pl
ON pih.[PO Number]=pl.[Document No_] and pl.[Line No_]=pil.[Line No_]
WHERE
PIL.[Document No_] IN
(
SELECT distinct pil.[Document No_] FROM
Purch_ Inv_ Line] pil
WHERE piL.[No_] in ('700xxx','700xxx','17xxxxx') and pil.[Posting Date] >=getdate()-7
)
AND piL.[Type]='1'
There might be 2 reasons why you are getting NULL values:
Values of columns in [Purchase line] table are actually NULL
There are no matching rows in [Purchase line] table for rows that are NULL. This is how LEFT JOIN works
If you want to get only matching rows, then change LEFT JOIN to INNER JOIN - however then your result set will not contain rows from [Purch_ Inv_ Line] and [Purch_ Inv_ Header] where there are not matching [Purchase line] rows.
If you want to give default values to NULL values you can do it using ISNULL:
ISNULL(pl.[Amount Including VAT], 0) as [PO Value From PurchaseLine]
Simple question, I assume it is impossible to use an aggregate function as part of an expression but here is basically what I want to do:
SELECT ((Sum(RESOURCES.[Hours Spent]*EMPLOYEES.[Employee Rate]))
+ GRANTS.Overhead) AS [Estimated Development Cost]
FROM GRANTS
INNER JOIN
(EMPLOYEES
INNER JOIN RESOURCES ON EMPLOYEES.[Employee Number] = RESOURCES.[Employee Number])
ON GRANTS.[Grant Program Number] = RESOURCES.[Grant Program Number];
It works perfectly before I try to add that last value to it (Grants.Overhead). How could I go about doing this?
Thanks.
You have to put the Grants.Overhead in a separate query, as it counts only once per group. So here you go:
SELECT T1.EDC + GRANTS.Overhead AS [Estimated Development Cost]
FROM (SELECT RESOURCES.[Grant Program Number] AS GPN,
(Sum(RESOURCES.[Hours Spent] * EMPLOYEES.[Employee Rate])) AS EDC
FROM EMPLOYEES
INNER JOIN
RESOURCES
ON EMPLOYEES.[Employee Number] = RESOURCES.[Employee Number]
GROUP BY RESOURCES.[Grant Program Number]) AS T1
INNER JOIN
GRANTS
ON GRANTS.[Grant Program Number] = T1.GPN;
You need to sum() the overhead too, otherwise you will get the old "it is not contained in either an aggregate function or the GROUP BY clause" error.
This works:
SELECT ((Sum(RESOURCES.[Hours Spent]*EMPLOYEES.[Employee Rate])) + sum(GRANTS.Overhead)) AS [Estimated Development Cost]
FROM GRANTS
INNER JOIN (EMPLOYEES
INNER JOIN RESOURCES ON EMPLOYEES.[Employee Number] = RESOURCES.[Employee Number])
ON GRANTS.[Grant Program Number] = RESOURCES.[Grant Program Number];
I have two separate queries here that I need to make into one query, I'll post the queries, then try to explain what I'm trying to do.
SELECT Distinct I.ITMCDE, V.VNDRCDE, V.VNAME
FROM (SELECT RIGHT(Items.[Item Number], 3) as ITMCDE FROM Items) I,
(SELECT LEFT(Vendors.[Vendor ID], 3) as VNDRCDE,
Vendors.[Vendor Name] as VNAME
FROM Vendors) V
WHERE I.ITMCDE = V.VNDRCDE
In this first one, I simply match up the vendor code with the item code, to get the vendor name that produces the item.
SELECT DISTINCT (Items.[Item Description]), ItemQuantities.[QTY Available],
Items.[Selling U Of M], Items.[Item Number]
FROM ItemQuantities
INNER JOIN Items ON ItemQuantities.[Item Number] = Items.[Item Number]
WHERE Items.[Item Number] LIKE 'WH%'
AND Items.[Item Number] NOT LIKE '%RMW'
In this second one I'm selecting the item description, quantity available from two separate tables (quantity available is in a different table, match them up using the item number)
As you can see, the only correlation between the three tables is the item number, and not even that in the vendors table. The last three characters of the item number correlate with the first three characters of the vendor id... I did not design this setup. I'm just trying to work with it now.
How do I join these two statements into one single statement that will give me the vendor name, item description, Unit of Measure (Selling U of M), and item quantity where the item description is unique?
I think this should work:
SELECT DISTINCT Items.[Item Description],
ItemQuantities.[QTY Available],
Items.[Selling U Of M],
Items.[Item Number],
V.VNAME
FROM ItemQuantities
INNER JOIN Items ON ItemQuantities.[Item Number] = Items.[Item Number]
INNER JOIN Vendors ON
RIGHT(Items.[Item Number], 3) = LEFT(Vendors.[Vendor ID], 3)
WHERE Items.[Item Number] LIKE 'WH%'
AND Items.[Item Number] NOT LIKE '%RMW'
You can join it in:
SELECT DISTINCT (i.[Item Description]), iq.[QTY Available],
i.[Selling U Of M], i.[Item Number],
V.VNDRCDE, V.VNAME
FROM ItemQuantities iq INNER JOIN
Items i
ON iq.[Item Number] = i.[Item Number] left outer join
Vendors v
on LEFT(v.[Vendor ID], 3) = RIGHT(i.[Item Number], 3)
WHERE i.[Item Number] LIKE 'WH%' and
i.[Item Number] NOT LIKE '%RMW'
I am not sure if you intend anything special with the parentheses around i.[Item Description]. The distinct keyword applies to the entire row.
If you want distinct only on the description, then you need to use group by. Something like:
SELECT i.[Item Description],
max(iq.[QTY Available]),
max(i.[Selling U Of M]), max(i.[Item Number]),
max(V.VNDRCDE), max(V.VNAME)
FROM ItemQuantities iq INNER JOIN
Items i
ON iq.[Item Number] = i.[Item Number] left outer join
Vendors v
on LEFT(v.[Vendor ID], 3) = RIGHT(i.[Item Number], 3)
WHERE i.[Item Number] LIKE 'WH%' and
i.[Item Number] NOT LIKE '%RMW'
group by i.[Item Description])
The max() will return the maximum value. If all are the same, then this is a good way to get an "arbitrary" value.
#sgeddes Thank you.
This is what I came up with
SELECT DISTINCT
Item.ITEMNMBR AS [Item Number],
Item.ITEMDESC AS [Item Description],
Item.ITMGEDSC AS [Item Category],
Item.SELNGUOM AS [Unit of Measure],
(SELECT VENDNAME FROM PM00200 WHERE (VENDORID = IV00103.VENDORID)) AS [Vendor Name],
(CASE WHEN Quan.QTYONHND > 0 THEN 'In Stock' ELSE 'Out of Stock' END) AS [Stock Status]
FROM IV00101 AS Item INNER JOIN
IV00102 AS Quan ON Item.ITEMNMBR = Quan.ITEMNMBR INNER JOIN
IV00103 ON Item.ITEMNMBR = IV00103.ITEMNMBR AND Quan.ITEMNMBR = IV00103.ITEMNMBR
WHERE (Item.ITEMNMBR LIKE 'WH%') AND (IV00103.VENDORID NOT LIKE '%MIL')