Is there a way to set a condition on the SQL code below? I only want to show up until the end year but am showing the start year + 5 years right now - sql

Goodmorning everyone I have this SQL code that I am using in Access to show a table of total hours per year on a form. The user has the OPTION of choosing up to 6 years which means they could also simply choose 1 2 3 4 or 5 years. I only want to show the years up to the end year not the full 6 if 6 aren't selected. The way I have the sql written right now takes the start year and adds 1 to it until it reaches year 6 and displays them all. I have a variable for the end year as well and was thinking if there is a way to put in IF startyear + number is greater than endyear then end but not sure how to achieve that. Any help would be super helpful
code:
SELECT
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours
FROM qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear
WHERE (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value))
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value))
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value))
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+1))
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value))
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+2))
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value))
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+3))
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value))
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+4))
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value))
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+5))
ORDER BY [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours;

Try something like this:
SELECT
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours
FROM
qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear
WHERE
qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID = Forms!frm_Home!cboEstimate.Value
AND
(qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID Between
Forms!frm_Home!cboStartYear.Value And
Forms!frm_Home!cboStartYear.Value + Forms!frm_Home!YearsToSelect.Value)
ORDER BY
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours;
where Forms!frm_Home!YearsToSelect.Value holds the user's choice of count of years.

Related

InfluxDB v1.8: subquery using MAX selector

I'm using InfluxDB 1.8 and trying to make a little more complex query than Influx was made for.
I want to retrieve all data that refers to the last month stored, based on tag and field values that my script stores (not the default "time" field that Influx creates). Say we have this infos measurement:
time
field_month
field_week
tag_month
tag_week
some_data
1631668119209113500
8
1
8
1
random
1631668119209113500
8
2
8
2
random
1631668119209113500
8
3
8
3
random
1631668119209113500
9
1
9
1
random
1631668119209113500
9
1
9
1
random
1631668119209113500
9
2
9
2
random
Which 8 refers to August, 9 to September, and then some_data that is stored on a given week of that month.
I can use MAX selector at field_month to get the last month of the year stored (can't use Flux date package because I'm using v1.8). Further, I want the data grouped by tag_month and tag_week so I can COUNT how many times some_data was stored on each week of the month, that's why the same data is repeated in field and tag keys. Something like that:
SELECT COUNT(field_month) FROM infos WHERE field_month = 9 GROUP BY tag_month, tag_week
Replacing 9 by MAX Selector:
SELECT COUNT(field_month) FROM infos WHERE field_month = (SELECT MAX(field_month) FROM infos) GROUP BY tag_month, tag_week
The first query works (see results here), but not the second.
Am I doing something wrong? Is there any other possibility to make this work in v1.8?
NOTE: I know Influx wasn't supposed to be used like that. I've tried and managed this easily with PostgreSQL, using an adapted form of the second query above. But while we straighten things up to use Postgres, we have to use InfluxDB v1.8.
in postgresql you can try :
SELECT COUNT(field_month) FROM infos WHERE field_month =
(SELECT field_month FROM infos ORDER BY field_month DESC limit 1)
GROUP BY tag_month, tag_week;

How to create new column with values counting up every 9th value with SQL? [duplicate]

This question already has answers here:
Group rows into sets of 5
(2 answers)
Closed 3 years ago.
as I realized that my project cannot be implemented with Excel I am now forced to learn working with SQL. Because I'm used to Excel VBA I am already failing at simple tasks.
In a table of my SQL Database I wanna create a new column with values counting up (ASC) but it shouldn't count up like 1,2,3,4,...it rather should increase the value by 1 every 9th value.
So if we start in row 1 with Value 1 than every row until row 9 in this column should countain value 1 followed by an increase in row 10 by 1. So value 2 should be shown until row 18.....in total this should end in row 306.
Is anybody able to help me? I have no clue at all..
THANK YOU FOR YOU HELP!
You can first simply add a column in your table and then update that column with a simple mathematics formula -
UPDATE YOUR_TABLE
SET NEW_COL = CEILING(((ID-1)/9)+1)
Hope this helps.

Increment Days in SQL query

I am trying to write a query that will output the following table:
|Day_0_Revenue|Day_1_Revenue|Day_2_Revenue|....|Day_90_Revenue|
The data looks like the following table. Each day will have multiple values, and I want to sum the revenue for each day. Day 0 is 11/1.
|Date|Revenue|
11/1 5
11/2 3
11/3 5
11/3 7
11/4 8
11/5 8
11/5 12
11/6 7
I believe this is simple to do if the table I want is vertical. The reason I want to output horizontally is because I will fill the table vertically with other dates. My main question is how to increment the day value without writing a really long SELECT clause? I'm not sure if I can write a loop that will have something like Day+1 until Day=90...
This is not possible in SQLite alone.
The easiest way would be to just use GROUP BY day to create a vertically oriented table, and then reorder the cells when you are displaying them.
If you really must get a horizontally oriented table from the database, you have to create the query dynamically:
SELECT (SELECT SUM(Revenue) FROM MyTable WHERE Date = '11/1') AS Day_0_Revenue,
(SELECT SUM(Revenue) FROM MyTable WHERE Date = '11/2') AS Day_1_Revenue,
(SELECT SUM(Revenue) FROM MyTable WHERE Date = '11/3') AS Day_2_Revenue,
...
You have to do this in whatever programming language you are using to access the database:
sql = "SELECT"
for i in range(90):
sql += " (SELECT SUM(Revenue) FROM MyTable" +
" WHERE Date = '%s') AS Day_%d_Revenue," % (dates[i], i)
sql = sql[:-1] # remove last comma
cursor = db.execute(sql)

Group by time span in rails

I want get output from users table based on time of creation of record. Time is stored in created_at column.
Output will be like this:
Time user count
2 am - 6 am 10
6 am - 10 am 5
10 am - 2 pm 5
2 pm - 6 pm 5
6 pm - 10 pm 5
10 pm - 2 am 5
I can't do group by created_at. Solution I found is to create another column say time_span and update that column to 2 am - 6 am if created_at time falls in this span and then I can do group_by on time_span column. Any better solution?
My suggestions is to create another column on the database, this way you avoid calculations on selects at an expense of a simple column.
I'm not sure what you mean by not being able to use group_by, but the following will work:
hours = Users.all.collect {|u| u.created_at.hour}
ranges = [(2...6), (6...10), (10...14), (14...18), (18...22), (22...26)]
summary = hours.group_by {|h| ranges.find {|r| r === (h<2 ? h+24 : h)}}
ranges.each {|r| puts "#{r} = #{(summary[r] || []).length}"}
There are probably opportunities to simplify this and you could push the grouping up into the database if you'd like, but I thought I'd go ahead and share.

Access SQL how to make an increment in SELECT query

I Have an SQL query giving me X results, I want the query output to have a coulmn called
count making the query somthing like this:
count id section
1 15 7
2 3 2
3 54 1
4 7 4
How can I make this happen?
So in your example, "count" is the derived sequence number? I don't see what pattern is used to determine the count must be 1 for id=15 and 2 for id=3.
count id section
1 15 7
2 3 2
3 54 1
4 7 4
If id contained unique values, and you order by id you could have this:
count id section
1 3 2
2 7 4
3 15 7
4 54 1
Looks to me like mikeY's DSum approach could work. Or you could use a different approach to a ranking query as Allen Browne described at this page
Edit: You could use DCount instead of DSum. I don't know how the speed would compare between the two, but DCount avoids creating a field in the table simply to store a 1 for each row.
DCount("*","YourTableName","id<=" & [id]) AS counter
Whether you go with DCount or DSum, the counter values can include duplicates if the id values are not unique. If id is a primary key, no worries.
I frankly don't understand what it is you want, but if all you want is a sequence number displayed on your form, you can use a control bound to the form's CurrentRecord property. A control with the ControlSource =CurrentRecord will have an always-accurate "record number" that is in sequence, and that will update when the form's Recordsource changes (which may or may not be desirable).
You can then use that number to navigate around the form, if you like.
But this may not be anything like what you're looking for -- I simply can't tell from the question you've posted and the "clarifications" in comments.
The only trick I have seen is if you have a sequential id field, you can create a new field in which the value for each record is 1. Then you do a running sum of that field.
Add to your query
DSum("[New field with 1 in it]","[Table Name]","[ID field]<=" & [ID Field])
as counterthing
That should produce a sequential count in Access which is what I think you want.
HTH.
(Stolen from Rob Mills here:
http://www.access-programmers.co.uk/forums/showthread.php?p=160386)
Alright, I guess this comes close enough to constitute an answer: the following link specifies two approaches: http://www.techrepublic.com/blog/microsoft-office/an-access-query-that-returns-every-nth-record/
The first approach assumes that you have an ID value and uses DCount (similar to #mikeY's solution).
The second approach assumes you're OK creating a VBA function that will run once for EACH record in the recordset, and will need to be manually reset (with some VBA) every time you want to run the count - because it uses a "static" value to run its counter.
As long as you have reasonable numbers (hundreds, not thousands) or records, the second approach looks like the easiest/most powerful to me.
This function can be called from each record if available from a module.
Example: incrementingCounterTimeFlaged(10,[anyField]) should provide your query rows an int incrementing from 0.
'provides incrementing int values 0 to n
'resets to 0 some seconds after first call
Function incrementingCounterTimeFlaged(resetAfterSeconds As Integer,anyfield as variant) As Integer
Static resetAt As Date
Static i As Integer
'if reset date < now() set the flag and return 0
If DateDiff("s", resetAt, Now()) > 0 Then
resetAt = DateAdd("s", resetAfterSeconds, Now())
i = 0
incrementingCounterTimeFlaged = i
'if reset date > now increments and returns
Else
i = i + 1
incrementingCounterTimeFlaged = i
End If
End Function
autoincrement in SQL
SELECT (Select COUNT(*) FROM table A where A.id<=b.id),B.id,B.Section FROM table AS B ORDER BY B.ID Asc
You can use ROW_NUMBER() which is in SQL Server 2008
SELECT ROW_NUMBER() OVER (ORDER By ID DESC) RowNum,
ID,
Section
FROM myTable
Then RowNum displays sequence of row numbers.