I've been working on some sql code in FileMaker and I have a basically a table called "Incidents" and a field called "Incident Type" under the said table. I use the code below to count the number of times the option "Injury" is selected from the field "Incident Type". This works 100%. However, I have been trying to sort the records in this database by the year it happened and to no surprise the code is counting all times the option "Injury" is selected in its respective field. I want to only count the times when the field "Incident Type" is equal to "Injury" and the other field "Incident Year" is equal to '2014'. Can someone revise the code below to show that?
ExecuteSQL(
"SELECT Count(\"Incident Type\")
FROM \"Incidents\"
WHERE \"Incident Type\" = ?"
;"";"";"Injury")
I'm not 100% sure this will work for you, but it should be close enough for you to tweak to your needs.
ExecuteSQL(
"
SELECT Count(\"Incident Type\")
FROM \"Incidents\"
WHERE (\"Incident Type\" = ?)
AND (\"Incident Year\" = ?)
"; ""; ""; "Injury"; "2014"
)
In case you are trying to display total injuries for a number of years, sorted by year
ExecuteSQL(
"
SELECT \"Incident Year\", Count(\"Incident Type\")
FROM \"Incidents\"
WHERE (\"Incident Type\" = ?)
GROUP BY \"Incident Year\"
ORDER BY 1
"; ""; ""; "Injury"
)
Related
I have in Power Query a Column "% sum of all". I need to create a custom column "Sum Consecutive" that each row has as value the "% sum of all" of the current row + the value of "Sum Consecutive" of the previous row.
Current row situation
New Custom Column Expectation
You can see two images that show the current situation and the next situation I need in the Power Query.
Can you please help me find a code/command to create this new column like that?
Although there are similar solved questions in DAX, I still need to keep editing the file after that, so it should be performed in M language in power query.
Thank you!
Not sure how performant my approaches are. I would think both should be reasonably efficient as they only loop over each row in the table once (and "remember" the work done in the previous rows). However, maybe the conversion to records/list and then back to table is slow for large tables (I don't know).
Approach 1: Isolate the input column as a list, transform the list by cumulatively adding, put the transformed list back in the table as a new column.
let
someTable = Table.FromColumns({List.Repeat({0.0093}, 7) & List.Repeat({0.0086}, 7) & {0.0068, 0.0068}}, {"% of sum of all"}),
listToLoopOver = someTable[#"% of sum of all"],
cumulativeSum = List.Accumulate(List.Positions(listToLoopOver), {}, (listState, currentIndex) =>
let
numberToAdd = listToLoopOver{currentIndex},
sum = try listState{currentIndex - 1} + numberToAdd otherwise numberToAdd,
append = listState & {sum}
in
append
),
backToTable = Table.FromColumns(Table.ToColumns(someTable) & {cumulativeSum}, Table.ColumnNames(someTable) & {"Cumulative sum"})
in
backToTable
Approach 2: Convert the table to a list of records, loop over each record and add a new field (representing the new column) to each record, then convert the transformed list of records back into a table.
let
someTable = Table.FromColumns({List.Repeat({0.0093}, 7) & List.Repeat({0.0086}, 7) & {0.0068, 0.0068}}, {"% of sum of all"}),
listToLoopOver = Table.ToRecords(someTable),
cumulativeSum = List.Accumulate(List.Positions(listToLoopOver), {}, (listState, currentIndex) =>
let
numberToAdd = Record.Field(listToLoopOver{currentIndex}, "% of sum of all"),
sum = try listState{currentIndex - 1}[Cumulative sum] + numberToAdd otherwise numberToAdd, // 'try' should only be necessary for first item
recordToAdd = listToLoopOver{currentIndex} & [Cumulative sum = sum],
append = listState & {recordToAdd}
in
append
),
backToTable = Table.FromRecords(cumulativeSum)
in
backToTable
I couldn't find a function in the reference for M/Power Query that sums a list cumulatively.
I have a query that runs great as is like this
WHERE (customer IN (#cust))
and #cust is a customer or a multiple list of customers
what I want to add is an AND with more in another optional parameter like
No: #expiring = " "
OR
Yes: #expiring = " AND getDate() >= dateAdd(d,[expiryWarning],[expiryDate]) "
then I want to add that to the end of the WHERE and have it do that second part if it's chosen from the dropdown as Yes or No so I can show the whole list of customers or just the ones expiring in the report.
WHERE (customer IN (#cust)) #expiring
but I am seeing an error when I try to run this report
that there's an error near #expiring
any insight? I've been searching all day, is this even possible?
You need to change your where clause to take #expiring into account like this.
WHERE customer in (#cust)
AND (#expiring='No' OR getDate() >= dateAdd(d,[expiryWarning],[expiryDate]))
So if #expring is 'No' then that part of the WHERE clause always returns true. If expiring = 'Yes' then the date criteria must be true else that part will return false.
Hello I am trying to use a drop down box on my form that will display two different record sources based on an if statement and can't get it to work. Is this possible??
Basically I want to show the codes and descriptions for the DX_Codes table If the date is less than 10/1/2015 and show the DX_Codes_ICD_10 if it is greater than or equal to 10/1/2015. The date is also a field on the same form.
IIf(Me.from_date < #10/1/2015#,
SELECT DX_Codes.dx_code, DX_Codes.dx_code_desc FROM DX_Codes ORDER BY DX_Codes.dx_code,
SELECT DX_Codes_ICD10.dx_code, DX_Codes_ICD10.dx_code_desc FROM DX_Codes_ICD10 ORDER BY DX_Codes_ICD10.dx_code);
You have to use VBA to manage a RowSource of a control like a combobox. Place the below behind some event trigger, possibly related with the from_date control.
If Me.from_date < #10/1/2015# Then
Me.ComboBoxName.RowSource = "SELECT DX_Codes.dx_code, DX_Codes.dx_code_desc" _
& " FROM DX_Codes ORDER BY DX_Codes.dx_code;"
Me.ComboBoxName.RowSourceType = "Table/Query"
Else
Me.ComboBoxName.RowSource = "SELECT DX_Codes_ICD10.dx_code, DX_Codes_ICD10.dx_code_desc" _
& " FROM DX_Codes_ICD10 ORDER BY DX_Codes_ICD10.dx_code);"
Me.ComboBoxName.RowSourceType = "Table/Query"
End If
I have an Access Database with many fields connected through a datagridview in my vb.net project. Two of these fields contain Date/Time Values. I want to create a query through the query builder that uses input from the user to find records that match the dates the user wants. This "where clause" works :
WHERE BETWEEN ? AND ?
This creates a toolstrip in which I can input 2 dates so that the query can fill the datagridview with the records.
What I want now is to make a query like the above only this time the user inputs the name of a month he wants (ex. February or 02 ). Is there any way to do that ?
EDIT: Tried using Plutonix's code (Sailing is the Column name) :
WHERE SAILING BETWEEN #01/31/yyyy# AND #03/01/yyyy#
and I got this error: "Cannot convert entry to valid datetime TO_DATE function might be required"
EDIT 2: I have created a combobox containing all 12 months and I have a commandbutton. I want to find a way so that if the user selects one of the 12 months from that combobox and clicks the commandbutton, to have the datagridview control (access database) show him only the records that go with that month based on their datetime(Short Date) fields. What code should I put in my commandbutton_click ?
Eventually I created a long query where I added a "Between ? and ?" in my where clause for every Year included in the database (2004-2015), created a huge if clause that gives the beginnings and endings of the month requested for every year and used all those strings to query the database.
If Month.Text = "JANUARY" Then
A04A = "01/01/04"
A04B = "31/01/04"
A05A = "01/01/05"
A05B = "31/01/05"
A06A = "01/01/06"
A06B = "31/01/06"
A07A = "01/01/07"
A07B = "31/01/07"
A08A = "01/01/08"
A08B = "31/01/08"
A09A = "01/01/09"
A09B = "31/01/09"
A10A = "01/01/10"
A10B = "31/01/10"
A11A = "01/01/11"
A11B = "31/01/11"
A12A = "01/01/12"
A12B = "31/01/12"
A13A = "01/01/13"
A13B = "31/01/13"
A14A = "01/01/14"
A14B = "31/01/14"
A15A = "01/01/15"
A15B = "31/01/15"
A16A = "01/01/16"
A16B = "31/01/16"
ElseIf etc etc
I've created a basic Combobox Filter Sort that sorts through Company Regions for my company (Acronyns mostly) we refer to these as AOR's or Area of Reference. After defining your AOR, it limits the next combo box to show only Countries in that specific AOR, hense the Filter Sort. But, my problem is - when it displays the countries after selecting an AOR - It displays ALL RECORDS in that specific country, instead of just 1 country listing.
Basically, It isn't grouping my countries - and when I select "Totals" which normally gives me only unique results, this doesnt work.
My question, How can I re-write this code to include a Group By?
My Code:
Private Sub cboRegion_AfterUpdate()
' Region -> Country
Dim sManagerSource As String
sManagerSource = "SELECT [FullEmail].[AORID], [FullEmail].[ID], [FullEmail].[Country] " & _
"FROM FullEmail " & _
"WHERE [AORID] = " & Me.cboRegion.Value
Me.cboCountry.RowSource = sManagerSource
Me.cboCountry.Requery
End Sub
My SQL statement looks like this (It's got Group By in it, but it doesn't GROUP)
SELECT FullEmail.AORID, FullEmail.ID, FullEmail.Country
FROM FullEmail
GROUP BY FullEmail.AORID, FullEmail.ID, FullEmail.Country
HAVING (((FullEmail.AORID)=1));
Thanks in advance for reading through!
The previous answer is correct, except with a having clause you need to use aggregates... so just use where.
SELECT Country
FROM FullEmail
WHERE AORID=1
GROUP BY Country;
That should fix you up. When you use HAVING it looks at what you've selected, since you didn't select AORID, it didn't know what to do.