I have a question about a look up function in ssrs. After some research on the forum I managed it to a certain degree. However the last step I can't solve.
In my report I am using two data sets. One set with some costumer parameters and one data set with the turnover grouped by (tip out of this forum) costumer ID and year. With this costumer ID I link the two tables.
=lookup(Fields!cmp_wwn.Value,Fields!cmp_wwn.Value, Fields!Omzet.Value, "Omzet")
When I use the code above I get the first value out of the set. However I want to specify which year I want to show.
With the code below I have tried it again.
=lookup(
Fields!cmp_wwn.Value & "-" & "2015",
Fields!cmp_wwn.Value & "-" & Fields!Datumjaar.Value,
Fields!Omzet.Value, "Omzet")
The warning below appears.
Warning 1 [rsRuntimeErrorInExpression] The Value expression for the textrun ‘Textbox8.Paragraphs[0].TextRuns[0]’ contains an error: Operator '&' is not defined for type 'Guid' and string "-".
Just to give you an idea of the two data sets:
Dataset 1:
SELECT Status, StartDate, Jaar,
Maand, Week, cmp_name, Accountmanager, Classificatie, cmp_fcity, Description, RequestComments, fullname, res_id, cmp_code, target,
cmp_wwn
FROM _VW_ALKLIMAET_MRS_Bezoekverslagen
Dataset 2:
SELECT SUM(bdr_val) AS Omzet, Datumjaar, cmp_wwn
FROM _VW_ALKLIMAET_MRS_omzet
WHERE (Datumjaar > 2013)
GROUP BY Datumjaar, cmp_wwn
Sample data of Dataset 1:
Status Start date Year Month Week Relation name Account manager Realtion classification City fullname ID fullname Costumer code Target Relation ID
Status Startdate Jaar Maand Week cmp_name Accountmanager Classificatie cmp_fcity fullname res_id cmp_code target cmp_wwn
3 12-8-2014 8:00 2014 8 33 van Dorp Hengelo Martijn Moekotte Unknown HENGELO Martijn Moekotte 849 2010935 0 4a3a0504-c255-4e91-b106-0000972bb783
3 4-6-2013 13:30 2013 6 23 Witte Koeltechniek BV Marco van der Haven B Installateur NOORD-SCHARWOUDE Marco van der Haven 815 200183 0 cfa97188-c76a-457c-ba78-003d499d86bc
3 6-11-2013 10:00 2013 11 45 Witte Koeltechniek BV Marco van der Haven B Installateur NOORD-SCHARWOUDE Marco van der Haven 815 200183 0 cfa97188-c76a-457c-ba78-003d499d86bc
Sample data of Dataset 2:
Turnover DataYear Relation ID Omzet Datumjaar cmp_wwn
-36755,5 2015 f887da07-08ad-4479-84ec-1459be5e3311
-105 2016 ba9e2640-291f-43ee-af5b-559245af165c
-6409 2014 c06cb96c-026a-4274-9db2-89cf9c8ccb2b
-176 2014 70968c94-b3b9-43a0-8b2b-18e62583c75f
You can concatenate two fields in the lookup function.
=lookup(
Fields!cmp_wwn.Value & "-" & "2015",
Fields!cmp_wwn.Value & "-" & Fields!Year.Value,
Fields!Omzet.Value, "Omzet")
It will give you the Omzet value for the customer your row contains and 2015 year. If you want to simplify the expression you can add a calculated field in every dataset and set an expression to concatenate cmp_wwn and year.
=Fields!cmp_wwn.Value & "-" & Fields!Year.Value
Now in your lookup just use the calculated field.
=lookup(
Fields!calculated_field.Value,
Fields!calculated_field.Value,
Fields!Omzet.Value, "Omzet")
UPDATE: GUID field doesn't support & operator
=lookup(
CSTR(Fields!cmp_wwn.Value) & "-" & "2015",
CSTR(Fields!cmp_wwn.Value) & "-" & CSTR(Fields!Datumjaar.Value),
Fields!Omzet.Value, "Omzet")
Let me know if this helps you.
Related
So I'm working with this badly designed table, but it's from another department and I need the data so I have no control over it. How can I get in a proper state? I'm working with MS Access so unfortunately I don't have access to "advanced" SQL functions like partition by, or row_number, etc.
This is the bad table:
KeyID
PA
QL
RuleName
1111
X
YYY
1111
X
ZZZ
1111
X
OOO
I want my final table to look like this:
KeyID
PA
QL
RuleNamePA
RuleNameQL
RuleNameQL2
1111
X
X
YYY
ZZZ
OOO
Any help would be appreciated!
Your desired output is not really a 'proper state' any more than the original data structure is - it's worse. Since the PA and QL fields appear to be Yes/No type and mutually exclusive, a properly normalized table would be like:
ID
KeyID
Cat
RuleName
1
1111
PA
yyy
2
1111
QL
zzz
3
1111
QL
ooo
That structure can be achieved with:
SELECT ID, KeyID, IIf(PA, "PA", "QL") AS Cat, RuleName FROM Table2;
Then that query can be used like a table in subsequent queries such as a CROSSTAB:
TRANSFORM First(RuleName) AS FirstOfRuleName
SELECT KeyID
FROM Query1
GROUP BY KeyID
PIVOT "RuleName" & [Cat] & DCount("*","Query1","Cat='" & [Cat] & "' AND ID<" & [ID])+1;
If there is no unique identifier ID field, change the DCount to:
DCount("*","Query1","Cat='" & [Cat] & "' AND KeyID & Cat & RuleName<'" & [KeyID] & [Cat] & [RuleName] & "'")+1
Here is a version that does not use Query1 and will provide the PA and QL fields which really don't seem necessary. Again, adjust the DCount if there is no ID field.
TRANSFORM First(RuleName) AS FirstOfRuleName
SELECT Table1.KeyID, Max(Abs([PA])) AS P, Max(Abs([QL])) AS Q
FROM Table1
GROUP BY KeyID
PIVOT "RuleName" & IIf([PA],"PA","QL") & DCount("*","Table1","IIf([PA],'PA','QL')='" & IIf([PA],"PA","QL") & "' AND ID<" & [ID])+1;
I'm modifying the VBA code developed by someone else and am having difficulty adding a "Where" clause on a form. As I'm a newbie to SQL I appreciate any assistance.
I have a table tbl_FC_Data which contains rows of data which present a forecast of part demand. It has over 43k records. Each record is a forecast for a Part_Number for a Data_Month for a specific company (Company_Code) for a forecast provided on a specific date i.e. Date_of_Forecast. Below is a table with a sample of the data.
Part_Number Data_Month Qty Company_Code Date_of_FC
AN565B8H4 1/1/2020 35 WAL2 12/11/2019
AN565B8H4 2/1/2020 1095 WAL2 12/11/2019
02-14-202-11 12/1/2019 20 D17 12/4/2019
02-14-202-11 1/1/2020 10 D17 12/4/2019
02-14-202-11 5/1/2020 1 D17 12/4/2019
02-14-202-11 6/1/2020 1 D17 12/4/2019
435W2151-49A 7/1/2020 40 API1 6/16/2020
02-14-202-11 6/1/2020 50 EAIL1 6/1/2020
02-14-202-11 7/1/2020 1 EAIL1 6/1/2020
02-14-202-11 1/1/2020 50 CZS1 12/19/2019
02-14-202-11 1/1/2020 50 CZS1 1/30/2020
The Access form contains two list boxes, the first which lists all the companies with data in the table (lstCriteriaCompany) and the second which contains the lists of forecasts dates for all records in the table (lstCriteriaDate_Of_Fc) regardless of company.
The existing VBA/SQL code to list all the forecast dates in the table is:
Me.lstCriteriaDate_Of_Fc.RowSource = "SELECT '#' & Format(D.Date_of_FC,'yyyy-mm-dd') & '#' AS IN_LIST_VALUE, " & _
"Format(D.Date_of_FC,'mm/dd/yyyy') AS DATE_OF_FORECAST " & _
"FROM (SELECT DISTINCT tbl_FC_Data.Date_of_FC FROM tbl_FC_Data) AS D"
It produces the following list of forecast dates:
12/4/2019
12/11/2019
12/19/2019
1/30/2020
6/1/2020
6/16/2020
My goal is that when a user clicks on a company in the first textbox the On Click code (Me.lstCriteriaDate_Of_Fc.RowSource = ...) displays the list of forecasts that relate to the selected company in the first textbox. For example, if CZS1 is selected then the textbox listing the forecasts would be changed to:
12/19/2019
1/30/2020
Here is where I added the Where clause. It returns zero records instead of the two records I expected. When the code is executed a dialog box pops up and asks for the parameter value for D.Company_Code.
Me.lstCriteriaDate_Of_Fc.RowSource = "SELECT '#' & Format(D.Date_of_FC,'yyyy-mm-dd') & '#' AS IN_LIST_VALUE, " & _
"Format(D.Date_of_FC,'mm/dd/yyyy') AS DATE_OF_FORECAST " & _
"FROM (SELECT DISTINCT tbl_FC_Data.Date_of_FC FROM tbl_FC_Data) AS D " & _
"Where D.Company_Code= '" & Me.lstCriteriaCompany.Column(0) & "'"
I also tried HAVING tbl_FC_Data.Company_Code = Me.lstCriteriaCompany.Value but I get a SQL error.
Thanks for any SQL help you can provide.
Referencing listbox selection is rather different from other data controls. Pull required value of selected item by referencing column by its index, even if value is in first column which would be index 0.
Me.lstCriteriaCompany.Column(0)
Concatenate this reference to the SQL string.
I figured out that the Where clause needed to be part of the SELECT DISTINCT clause. Here is the code that works:
Me.lstCriteriaDate_Of_Fc.RowSource = "SELECT '#' & Format(tbl_FC_Data.Date_of_FC,'yyyy-mm-dd') & '#' AS IN_LIST_VALUE, " & _
"Format(tbl_FC_Data.Date_of_FC,'mm/dd/yyyy') AS DATE_OF_FORECAST " & _
"FROM (SELECT DISTINCT tbl_FC_Data.Date_of_FC FROM tbl_FC_Data " & _
"Where tbl_FC_Data.Company_Code = " & Me.lstCriteriaCompany.Column(0) & ");"
So I am trying to figure out what I am doing wrong, I have a table I am trying to run a query which asks the user if they have a specific district they are looking for....
so my columns are
name district etc
Jay 1
Tom 3
Mary 5
Tim 5
Mike 15
I am trying to be able to have a parameter [which district?:] 1,5 .. It would display
Jay 1
Mary 5
Tim 5
Code:
WHERE
[District] Like "*" & [What District(s)]
& "*" OR [What District(s)] Like "*" & [District] & "*"
but I am getting
Jay 1
Mary 5
Tim 5
Mike 15
I am trying to avoid getting the "15" record.
Consider reversing the LIKE expressions and checking by comma positions for 1) comma after (#,) 2) comma before (,#), or 3) before and after (,#,) and then exact or no comma (=).
SELECT *
FROM Employees
WHERE
[What District(s)] LIKE [District] & ',*'
OR [What District(s)] LIKE '*,' & [District] & ',*'
OR [What District(s)] LIKE '*,' & [District]
OR [Employee ID] = [What District(s)]
Advise user not to include whitespaces between entries as a complexity error strangely raises.
If you are dealing with a list of numbers separated by a quote, you can use the IN operator, like:
""WHERE [District] IN ("" & [What District(s)] & "")""
This would generate a valid SQL expression like:
WHERE [District] IN (1,5)
Reference : the IN operator in ms-access
I have this working in Excel however it really needs moved into Access as that's where the rest of the database resides.
Its simply one table that contains Unique_ID, Seller and Fruit...
1 Chris Orange
2 Chris Apple
3 Chris Apple
4 Sarah Kiwi
5 Chris Pear
6 Sarah Orange
The end results should be displayed by Seller and then a list of each fruit sold (in the following example Robert has not sold any fruit, I do have a list of all sellers name however this could be ignored in this example as that I believe that will be easy to integrate.) They will only sell a maximum of 20 fruit.
Seller 1st 2nd 3rd 4th
Chris Orange Apple Apple Pear
Sarah Kiwi Orange
Robert
At the moment Excel uses Index, Match and Small to return results. Small is simply used on the Unique_ID to find the 1st, 2nd, 3rd, ect...smallest entries and is matched to each sellers name to build the above results.
As Access doesn't have a Small function I am at a loss! In reality there are over 100,000 records (minimum) with over 4000 sellers....they are also not fruit :)
TRANSFORM First(Sales.Fruit) AS FirstOfFruit
SELECT Sales.Seller
FROM Sales
GROUP BY Sales.Seller
PIVOT DCount([id],"sales","seller='" & [seller] & "' and id<=" & [id]);
Where the table name is "Sales" and the columns are "ID", "Seller" and "Fruit"
To understand DCount better, use it is a SELECT query instead of a crosstab:
SELECT Sales.ID, Sales.Seller, Sales.Fruit, DCount([id],"sales","seller='" & [seller] & "' and id<=" & [id]) AS N
FROM Sales;
On each row, the last column is the DCount result. The syntax is DCount (field, source, expression) so what it does is count the IDs (field) in the Sales table (source) that match the expression - in other words, has the same seller as that row's record and an ID <= the current row's ID. So for Chris's sales, it numbers them 1 through 4, even though Sarah had a sale in the middle.
From this result, it's easy to take a Crosstab query that makes a table with seller in the row and N in the column - putting the sales in order for each seller the way you wanted to see them. The "First" function finds the first fruit for the combination of seller and N for each row and column of the result. You could just as easily use "Max" or "Min" here - any text function. Of course, there is only one record matching the seller row and the N column, but Crosstab queries require a function to evaluate and cannot use "Group by" for the field selected as a Value.
My 1st answer combines these steps - the select and the crosstab queries - in one query.
Hope this helps.
I have a problem of SQL syntax which I have been struggling for days.
I have created an MS Access 2010 Form called IP Country Form into which I want to insert into a TextBox called Country the Country obtained from the following query which queries a table called IPLocation which contains the Countries according to IP Numbers with the fields:
IPNS IPNE CY2(2 letter Country Code) CY3(3 Letter country Code) Country (full Country Name)
The table rows looks like this:
IPNS | IPNE | CY2 | CY3 | COUNTRY
19791872 | 19922943 | TH | THA | THAILAND
The form calculates the IP Number from the IP address and places it in a TextBox called IPNumber, which I then use in an SQL Query to obtained the Country which corresponds to it.
My current query is:
SELECT IPLocation.IPNS, IPLocation.IPNE, IPLocation.CY2, IPLocationCY3, IP.Location.Country
FROM IPLocation
WHERE (((IPLocation.IPNS)<" & [Forms]![IP Country Form]![IPNumber])
And ((IPLocation.IPNE) > " & [Forms]![IP Country Form]![IPNumber]))
IPNS, IPNE are Numbers as is IPNumber
Essentially the query is designed to find the Country where the IPNumber lies between IPNS and IPNE.
Any help would be much appreciated.
A test query I use:
Set rst = dbs.OpenRecordset("
SELECT IPLocation.IPNS, IPLocation.IPNE, IPLocation.CY2, IPLocation.CY3,
IPLocation.Country
FROM IPLocation
WHERE (((IPLocation.IPNS)>19791871)
AND ((IPLocation.IPNE)<19922944))
")
Works fine and returns the correct country
On the query you use on your code you have the comparison operators reversed on the AND clause.
IPLocation.IPNS) > " & [Forms]![IP Country Form]![IPNumber]
IPLocation.IPNE) < " & [Forms]![IP Country Form]![IPNumber]
Should be:
SELECT IPLocation.IPNS, IPLocation.IPNE, IPLocation.CY2, IPLocationCY3,
IP.Location.Country
FROM IPLocation
WHERE (((IPLocation.IPNS) > " & [Forms]![IP Country Form]![IPNumber])
And ((IPLocation.IPNE) < " & [Forms]![IP Country Form]![IPNumber]))