Query with checkbox and combobox in Access Database - sql

Good afternoon
This is my query:
SELECT Reference.quote_date AS [Quote Date]
, Reference.agent_ID AS Agent
, Reference.ref_ID AS ReferenceID
, Reference.ref_number AS [Ref Number]
, Customer.title_id AS Title
, Customer.name AS Name
, Customer.surname AS Surname
, Customer.postcode AS Postcode
, Customer.telephone AS Telephone
, Reference.location_ID AS Location
, Reference.policy_ID AS Product
, Reference.price AS Price
, Reference.status_ID AS Status
, Reference.source_ID AS Source
, Reference.calltype_ID AS [Call Type]
, Reference.prize_draw AS [Prize Draw]
, Reference.Call_back_date AS [Call back date]
, Reference.call_back AS [Call back]
, Reference.IsCompleted AS Completed
, Reference.comments AS Comments
FROM Customer INNER JOIN Reference
ON Customer.[customer_ID] = Reference.[customer_ID]
WHERE (((Reference.agent_ID)=[Forms]![Call_Back_Search1]![Combo5])
AND ((Reference.Call_back_date)
Between [Forms]![Call_Back_Search1]![StartDateTxt] And [Forms]![Call_Back_Search1]![EndDatetxt]));
It Currently shows all call backs within certain criteria selected in Call_Back_Search1 form:
- date criteria: between Start and date:End
- and allows to select the Agent (Combobox05).
I wish to add additional criteria to my query :
checkbox called "Completed" to be not ticked
- to show results based only on date range selected (leaving combobox blank)
Wonder if I can ask you for any suggestions
Thank you

I hope someone will find my answer useful
Code is listed below :
SELECT Reference.quote_date AS [Quote Date], Reference.agent_ID AS Agent, Reference.ref_ID AS ReferenceID, Reference.ref_number AS [Ref Number], Customer.title_id AS Title, Customer.name AS Name, Customer.surname AS Surname, Customer.postcode AS Postcode, Customer.telephone AS Telephone, Reference.location_ID AS Location, Reference.policy_ID AS Product, Reference.price AS Price, Reference.status_ID AS Status, Reference.source_ID AS Source, Reference.calltype_ID AS [Call Type], Reference.prize_draw AS [Prize Draw], Reference.Call_back_date AS [Call back date], Reference.call_back AS [Call back], Reference.comments AS Comments, Reference.IsCompleted
FROM Customer INNER JOIN Reference ON Customer.[customer_ID] = Reference.[customer_ID]
WHERE IIF(IsNull(Forms!Call_Back_Search1!Combo5),True,(Reference.agent_ID)=Forms!Call_Back_Search1!Combo5) And ((Reference.Call_back_date) Between Forms!Call_Back_Search1!StartDateTxt And Forms!Call_Back_Search1!EndDatetxt) And (([Reference.IsCompleted])=0);

Related

Insert value only when column is null

I have 2 different Excel sources
Employee Excel source
Sales Excel source
In my employee source I have multiple folder and in my sales source I have multiple folders as well as multiple sheets, so I had to use two data flow task and two for each loop containers for that purpose.
I need to take the ID column from my employee Excel source, if the ID column in my employee source is not available (null) only if not available in my employee source then I need to get the ID Column from my sales source, they have matching SSN, how can I achieve this?
This is my staging table
SELECT
ID, [SSN],
[Borrower FirstName], [Borrower LastName],
[Borrower Email], [Home Phone], [Cell Phone],
[Marital Status], [Date of Birth],
[Current Street Address], [City], [State], [Zip],
[YearsAtThisAddress], [Sex], [Ethnicity], [Race]
FROM
[Mortgage]
This is my staging table, half of my data from the employee and half from sales.
Employee source has
[ID] ,[SSN] ,[Borrower FirstName] ,[Borrower LastName] ,[Borrower Email] ,[Home Phone] ,[Cell Phone] ,[Marital Status] ,[Date of Birth] ,[YearsAtThisAddress]
and SALES source has
[ID] ,[SSN] ,[Borrower FirstName] ,[Borrower LastName] ,[Borrower Email] ,[Home Phone] ,[Cell Phone] ,[Current Street Address] ,[City] ,[State] ,[Zip]
You have records from two data sources merged together and part of your requirement is to know from which source the record came (whether the ID column is NULL only if from the employee source). Our only clue as to whether a record came from the "employee source" is if there are values in the columns that differ between the two ([Marital Status], [Date of Birth], [YearsAtThisAddress]), but you don't say if any of these nullable, so we must make an assumption. Assuming the value in column [YearsAtThisAddress] can be used to determine the record's source (NOT NULL means "Employee" while NULL means "Sales"), you could do something like this to return the corresponding "Sales" ID number for records matching on SSN when the "Employee" ID number is NULL.
SELECT
CASE WHEN ID IS NULL AND [YearsAtThisAddress] IS NOT NULL
THEN (SELECT TOP (1) ID
FROM [Mortgage]
WHERE [YearsAtThisAddress] IS NULL
AND ID IS NOT NULL
AND [SSN] = m.[SSN]
ORDER BY ID)
ELSE ID
END AS ID,
[SSN],
[Borrower FirstName], [Borrower LastName],
[Borrower Email], [Home Phone], [Cell Phone],
[Marital Status], [Date of Birth],
[Current Street Address], [City], [State], [Zip],
[YearsAtThisAddress], [Sex], [Ethnicity], [Race]
FROM
[Mortgage] AS m

MS ACCESS: Unable to retrieve LAST Price Paid based on Maximum Date

OBJECTIVE
Develop a sales catalog for a COMPANY ID based on ITEM ID and latest PRICE paid (based on LAST SHIP DATE).
APPROACH
Pull in CUSTOMER, SALES, ITEM Tables
Run Query Link tables based off of CUSTOMER ID and ITEM to understand purchase history
Export a table showing a COMPANY ID, ITEM ID, LAST SALES PRICE, LAST SHIP DATE
CODE
SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
(CCur([SDLPRC])/10000) AS PRICE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM,
(CCur([SDLPRC])/10000);
ISSUE/QUESTION
CUSTOMER A bought ITEM ABC # 3 different prices on 3 different dates. I've taken the Max of Ship Date in hope to show the LAST PRICE PAID (resulting in one single value for price). However, for some reason, I am still receiving the three different prices on three different dates. How can I have MS Access only display the latest price based off of the latest ship date?
NOTE: SDLPRC = "Sold Price". I have to convert SLDPRC into a Currency and then divide by 1000; this is due to our current database setup. Also, SDAITM is an "Abbreviated Item Number" that is more customer-friendly.
The problem is that you're grouping by your Price variable (CCur([SDLPRC])/10000). When you use GROUP BY, Access/SQL will split the rows by all the variables in the GROUP BY statement. So you need to not group by price.
Change your query to use a subquery that finds the last date of a sale grouped by [Company ID], [Item ID] and Style. The use an outer query to grab the price for that particular record. Something like:
SELECT b.[COMPANY ID], b.[ITEM ID], b.STYLE, b.[LAST SHIP DATE], CCur(a.[SDLPRC])/10000 as PRICE
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)] as a
INNER JOIN
(SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM
) as b
ON a.SDAN8 = b.[COMPANY ID]
and a.SDITM = b.[ITEM ID]
and a.SDAITM = b.STYLE
and a.SDDRQJ = b.[LAST SHIP DATE]

Extract a specific line from a SELECT statement based on the last Trasanction date

Good day,
I have an SQL code that return to me all quantities that I received over time, but I want to display only the latest one
SELECT * FROM
(SELECT DISTINCT
[dbo].[ttcibd001110].[t_cmnf] AS [Manufacturer],
[dbo].[ttcibd001110].[t_item] AS [Item code],
[dbo].[ttcibd001110].[t_dsca] AS [Description],
[dbo].[ttcibd001110].[t_seak] AS [Search key 1],
[dbo].[twhinr110110].[t_trdt] AS [Transaction date],
[dbo].[twhinr110110].[t_cwar] AS [Warehouse],
[dbo].[twhinr110110].[t_qstk] AS [Quantity Inventory Unit]
FROM [dbo].[twhinr110110] LEFT JOIN [dbo].[ttcibd001110]
ON [dbo].[twhinr110110].[t_item]=[dbo].[ttcibd001110].[t_item]
WHERE [dbo].[twhinr110110].[t_koor]='2' AND [dbo].[ttcibd001110].[t_cmnf]='ManufacturerX') AS tabel
WHERE ltrim(tabel.[Item code])='1000045'
Now, from this selection I want to select only the line with the latest Transaction date, but I am stuck.
Can somebody help me in this way?
Thank you!
Change your beginning to
SELECT TOP 1
and after where use
ORDER BY [Transaction date] DESC

Sql Query with inner select [duplicate]

This question already has answers here:
Sql query to create a calculated field
(2 answers)
Closed 8 years ago.
I have this sql query:
SELECT DISTINCT
[Card NO],
[User Name],
(
SELECT
MIN(DateTime) AS [Enter Time],
MAX(DateTime) AS [Exit Time],
MAX(DateTime) - MIN(DateTime) AS [Inside Hours]
FROM
ExcelData
)
FROM
ExcelData
GROUP BY
[Card NO], [User Name], DateTime
Table Schema: CardNO | UserName | DateTime
I tried to execute it but with no success. I says that it is an invalid query.
Can anyone find what is wrong in this query?
This would not resolve your possible Problems with reserved keywords and column names, but should be a valid query. You were using a select query as column Name.
SELECT
[Card NO],
[User Name],
MIN(DateTime) AS [Enter Time],
MAX(DateTime) AS [Exit Time],
MAX(DateTime) - MIN(DateTime) AS [Inside Hours]
FROM
ExcelData
GROUP BY
[Card NO], [User Name]
You are grouping by DateTime, which is a reserved keyword, and you don't select a column DateTime. You do select aggregates over DateTime, which means you shouldn't group on it. #irene got the right answer.

SQL aggregate and other fields showing in query

I have a query, where I need the MIN of a DateTime field and then I need the value of a corresponding field in the same row.
Now, I have something like this, however I cannot get Price field without putting it also in an aggregate clause, which is not what I want.
SELECT MIN([Registration Time]), Price FROM MyData WHERE [Product Series] = 'XXXXX'
I need the MIN of the Registration Time field and then I just want the corresponding Price field for that row, however how do I show that?
I do also need my WHERE clause as shown.
I'm sure I've overlooked something really obvious. Using SQL Server 2008
If you want just one record with [Registration Time], Price, it'd be as simple as this:
select top 1 [Registration Time], Price
from MyData
where [Product Series] = 'XXXXX'
order by [Registration Time]
If you want minimum [Registration Time] and corresponding Price for all [Product Series], then there's a few approaches, for example, using row_number() function:
with cte as (
select
[Registration Time], Price,
row_number() over(partition by [Product Series] order by [Registration Time]) as rn
from MyData
)
select
[Registration Time], Price, [Product Series]
where rn = 1