I have the following table which contains date and user shift.
In the view i have to display in tabular form from_date[Shift start date] to_date[Shift end date.] and shift_id of users.It seems to be complex.I'm thinking of using "group by" but I'm not able to do it.If you guys can just give me the query i can display it.
Any help will be greatly appreciated.
I suppose you are trying to select start date and end date in one query. If so, the query could be like this:
SELECT a.*, (SELECT min(shift_date) FROM shifts
WHERE user_id = a.user_id AND shift_date > a.shift_date) end_date
FROM shifts a
You can adapt this query according to your requirements and convert it into Rails' query.
Related
Sorry I know this question has been asked before - I have tried changing my query around but can't seem to get it to work as expected, doesn't look to be anything wrong..
I am simply trying to query data from one large master sheet into a few separate sheets. I am using importrange to get the data from sheet, and a simple select query to filter by one of the columns. If I do a select * I get all the data as expected, but can't use a WHERE clause with any column (I just need 1 of the columns, but I tried with a few different ones).
Appreciate any help!
Query:
=QUERY(IMPORTRANGE("1sNA9u2uQW-XjEKjrVS2a5LtTPCchwSuTkXfjhTJtvPk","Sheet1!B:I"),"select * WHERE 'Rank'='LTC' ")
Columns
Username Rank Time In Service TIS Time In Grade TIG Promotable Awards PLT/SQD
Source sheet: https://docs.google.com/spreadsheets/d/1sNA9u2uQW-XjEKjrVS2a5LtTPCchwSuTkXfjhTJtvPk
Test sheet: https://docs.google.com/spreadsheets/d/1UCucsfE0M4j95d_47iN0LrAhS0luv8wXVMHRVTHJHRQ
As player0 stated, you should refer to the columns by its number, you can select multiple columns and state multiple "where" statements by using Col1,Col2, etc respectively. In this case: Where Col2 = 'LTZ'
try:
=QUERY({IMPORTRANGE("1sNA9u2uQW-XjEKjrVS2a5LtTPCchwSuTkXfjhTJtvPk", "Sheet1!B:I")},
"where Col1='LTC' ", )
Using Zapier, when someone fills out my Wufoo application form, a line is created with some of the information, including Date Created as well as the locations selected from a checkbox field. On my second sheet, I want to be able to have a total of how many applications have come in just today for each location. I figured the easiest way to do this would be a CountIFs function, but I'm probably wrong.
https://docs.google.com/spreadsheets/d/1mIw_O6KT2QCyKeKmZ4aJ1EHzOkZ4xj49ZyjEMOCwUxA/edit#gid=0
Here is a copy of what I'm building if you'd like to experiment. I'm using =countifs(Sheet1!E2:T1000,"Buford,GA (Atlanta)",Sheet1!E2:T1000,"="&TODAY()) to try to find this, but always get 0 as the result.
try this:
You needed to specify the columns, in your case: E2:E to look up for the name, and S2:S for the date.
=countifs(Sheet1!E2:E,"Buford, GA (Atlanta)",Sheet1!S2:S,"="&TODAY())
You can also use Partial Match in order to use your headers in Sheet2 as the first criteria in your countifs:
=countifs(Sheet1!E2:E,"*"&B$1&"*",Sheet1!S2:S,"="&TODAY())
Then you had a problem of formatting in Sheet1 within the Date's Column (S): your second date in cell S3 was formatted as Date Time, as oppose to your first date in S2 which was formatted as Date only.
Note: The above suggestions are to resolve your personal attempt, otherwise they are other ways of achieving what you're looking for.
UDPATE (ALTERNATIVE SOLUTION):
you could use the following QUERY which summarize everything within a table and sort it in descending order:
= {"Names","Count";
ARRAYFORMULA(
SORT(
TRANSPOSE(
SUBSTITUTE(
QUERY(Applications!D1:T,
"Select Count(D),Count(E), Count(F), Count(G), Count(H), Count(I), Count(J),
Count(K), Count(L), Count(M), Count(N), Count(O), Count(P), Count(Q), Count(R), Count(S) where T = date '"&TEXT(today(),"yyyy-mm-dd")&"'
"),
"count", "")
),
2, 0)
)
}
I have the spreadsheet attached.
I'd like to find Client No from lookup sheet based on the date provided in the live sheet.
The same client can appear with a different client number, so i need to lookup the name and date (from live sheet) and find the corresponding client number in the lookup sheet where the date from live sheet falls between the 2 dates on the lookup sheet.
I hope this makes sense.
Any help appreciated.
Thank you
This might do what you're looking for.
=IFERROR(
QUERY(SORT(FILTER(Lookup!A$2:D,Lookup!C$2:C=B2,Lookup!A$2:A<=A2),1,0),
"SELECT * WHERE COL4 >= DATE '"&TEXT(A2,"YYYY-MM-DD")&"' LIMIT 1",0),
QUERY(SORT(FILTER(Lookup!A$2:D,Lookup!C$2:C=B2,Lookup!A$2:A<=A2),1,0),
"SELECT * LIMIT 1",0) )
I've added a tab Live-GK to your sheet, with this formula in C2. It has to be dragged down. There may be another approach where it can be done as an arrayformula, but I haven't figured that out.
Note that on my tab, I'm doing the lookups from Lookup-GK, since I could add more test data there. The above formula can be used as is, pasted into cell C2 in your Live tab.
Note that for debugging purposes, column H of my tab returns all of the columns, not just the client #, so the start and end dates can be verified.
Let me know if this helps you.
Explanation:
The inner filter selects all rows from the Lookup tab where:
i) the client name (column C in Lookup) matches the client name in column B (of Live), and,
ii) the start date (column A in Lookup) is less than or equal the client date in Live.
These records are sorted in descending date order.
Then the query selects the first record where the end date (column D in Lookup) is greater than the client date in Live.
If the Lookup record has no end date, this gives an error (empty query result) so IFERROR, a second query is run, but without the filtering by end date, selecting the one record with no end date, but an appropriate start date.
These seemed to work with the few test records I used. If there is a duplication of client dates, the first client # is returned. See client #1 and #7 in my test data. Some more error handling might be necessary if your client records might have overlapping date ranges, as CalculusWhiz asked.
I am trying to create a simple daily time recorded in google sheets. I have created a Google Form named "Time In Records". Each time a user submit a record using that form, it records their name and the time they inputted, as well as the automatic timestamp.
On my other Sheet, which is named "DTR Summary", I am extracting the data from the "Time-In Column" of the "Time In Records" Sheet. I use this code to do that:
=QUERY('TIME IN RECORDS'!$A$2:$E, "select C where E = '"&TEXT($C15,"dd/mm/yyyy")&"' and (B = '"&$A$2&"')")
Basically, I am taking the "Time-In" of a user based on their name(A2) and the date(C15). This works fine, the problem is that I have to manually do this code on the "Time-In Records" Sheet every time a new user submit a data. (The code below is inputted on column E of the "Time-In Records" Sheet) because every time a new data is added, the column E is left blank so I have to manually drag the first cell to the last added data.
=TEXT($A2,"dd/mm/yyyy")
What I would like to happen is to automatically convert the timestamp into a date so that it will match my QUERY. I thought of something like this:
=QUERY('TIME IN RECORDS'!$A$2:$E, "select C where text(A, "dd/mm/yyyy") = '"&TEXT($C15,"dd/mm/yyyy")&"' and (B = '"&$A$2&"')")
Sadly it does not work. I would appreciate any help. Thanks!
For all who are having the same concern, I have figure this out.
On the "Time-In Records" Sheet, I've put this code to column E:
=ArrayFormula(IF(A2:A<>"",TEXT($A2:A,"dd/mm/yyyy"),""))
This automatically converts the timestamp column into date, the current and newly added data.
formula syntax should be:
=QUERY('TIME IN RECORDS'!A2:E,
"select C
where E = date '"&TEXT(C15, "yyyy-mm-dd")&"'
and B = '"&A2&"'")
I am sharing this sheet with you so that you can have a look and give me a proper solution. Basically what I want to do it generate an invoice based on the data entered in "Purchases" but I don't know how to do it as the lot numbers aren't always in the proper sequence. Therefore, if you notice in the "Sale & Inventory" sheet, I have to create multiple entries for one invoice. Is there a simpler method where the invoices are generated automatically? I don't want more than 30 lots/invoice.
Thanks,
Huzaifa.
I can't leave a comment to ask a question, so I assumed you'd want all lots from a certain date. Please see the 'Invoice' tab from this spreadsheet (I duplicated yours). Changing the yellow drop-downs should fill the invoice for you. You can also create multiple pages so it stays A4 size.
I also had to make sure column C on the 'Purchases' tab was formatted as a date 'yyyy-mm-dd' by using the 'Format' menu.
I did this by adding =QUERY(Purchases!$A$2:$T$500,"Select B where B<>'' and C >= date '"&TEXT(REGEXEXTRACT(K3,"[0-9]{6}"),"20##-##-##")&"' and C <= date '"&TEXT(REGEXEXTRACT(L3,"[0-9]{6}"),"20##-##-##")&"' limit 30 offset "&IF(M6=1,0,(M6-1)*34)&"",0) to cell H8 (highlighted green) in 'Invoices' tab.
How it works:
=QUERY(
Purchases!$A$2:$T$500, //the data I want to query
"Select B //tells it which column i want data from
where B<>'' //make sure column B is not blank
and C >= date '"&TEXT(REGEXEXTRACT(K3,"[0-9]{6}"),"20##-##-##")&"'
//makes sure the date is greater than or equal to
//the start date found in your lot code and formats
//it as 'yyyy-mm-dd' which is necessary for the
//query function
and C <= date '"&TEXT(REGEXEXTRACT(L3,"[0-9]{6}"),"20##-##-##")&"'
//makes sure the date is less than or equal to
//the start date found in your lot code and formats
//it as 'yyyy-mm-dd' which is necessary for the
//query function
limit 30 //restricts the returned results to 30 so it does
//not exceed the number of lines on your invoice
offset "&IF(M6=1,0,(M6-1)*30)&" //this shows the 'page' of data that you select
")
Let me know if you have different criteria for invoices and I can help, but try and see if you can figure it out from here first.