Google Sheets Query - fill zeros for dates when no data was recorded - sql

How do I change my query in this google sheet so that it generates a table that has a column for every week, even if there is no data for that week? (display 0 in the values fields)
The problem I'm running into is that if there is a week or date with no data, it's not listed. I would like to generate a row/column with 0 for dates without data.
This is how the query is currently written:
=QUERY(A3:D9,"Select A, sum(C) group by A pivot D")
Here's the sheet (hyperlinked so you can see it):

The basic problem you need to solve is to know which data pieces are missing. Do you need the entries for every single day in a given date range? Only weekdays? Only weekdays, except public holidays? etc.
Once you know that, you can insert the missing data in the query itself, by concatenating the source table with literal data as below (where I'm manually adding a combination of Red with Nov 5), or with another query/resultset of another formula that gives you the missing data:
=QUERY({A3:D9; {"Red", date(2018,11,5), 0, weeknum(date(2018,11,5))}},
"Select Col1, sum(Col3) group by Col1 pivot Col4")

Related

Microsoft Access Query Max Vaule per Date

Ok, so my SQL skills and Access skills are very rusty. I have a excel sheet with some data that I would like to start tracking into a database. Currently I pull the data, clean it up a bit and transpose it and have some excel magic work.
Currently there are two fields. Field1 is just a normal number field, less than 10 digits. Field 2 is converted into an excel date format from a UNIX Epoch timestamp.
My goal is to have the Max of Field1 for each day. Most of the older data only had one data point per day, while the newer data will possibly have hundreds data points.
Example Data:
Field1 being normal number
Field2 being Excel Date format
Field1
Field2
21107
44200.88
31827
44201.5
31827
44201.5
29355
44202.13
29355
44202.13
Assuming that you have your data in a table called Sheet1, you can type the following query in SQL view in MsAccess:
SELECT int(Field2) as ExcelDay, max(Field1) as MaxOfField1
FROM Sheet1
group by int(Field2)
int(Field2) removes the fraction from the time, leaving the Day you requested.
If you need to return both date & time as output then could try-
SELECT First(Field2) AS [Date], Max(Field1) AS MaxValue
FROM ExcelLinkTable
GROUP BY Format(Field2,"m/d/\e");
If you only want date to display then try-
SELECT Format(Field2,"mm/dd/yyyy") AS [Date], Max(Field1) AS MaxValue
FROM ExcelLinkTable
GROUP BY Format(Field2,"mm/dd/yyyy");

Using range of cells as conditions in SQL Query

My company uses a SQL Server database.
Is it possible to use a range of cells as a condition in a SQL query if it equals ANY of those values? Can it even use date ranges on the same rows?
Reference Example:
Data Example:
Output Desired:
Question 1:
Can I reference an entire column?
SELECT ID, sum(units) FROM sales WHERE ID = any ID in Column A
Question 2:
Can I specify just a cell range?
SELECT ID, sum(units) FROM table WHERE ID = any value in A2:A10
Question 3:
Can I add a date range cell reference with the possibility that the same ID may appear more than once but have a different date range (see 747375 in sample) and return results for both ranges separately?
SELECT ID, sum(units) FROM table WHERE ID = any value in A2:A10 AND DATE >= date found in column B that is next to ID in the same row AND DATE <= date found in column C that is next to ID in the same row
You can use between as following
select
r.id,
sum(units) as units
from reference r
join data d
on r.id = d.id
where d.date between r.start and r.end
group by
r.id
Question 1: Can I reference an entire column?
Yes. A default select without a where clause will reference the entire column.
Your example SELECT ID, sum(units) FROM sales WHERE ID = any ID in Column A is not logically sound. From the select, I am presuming that you want the sum of units for each individual ID, not the sum of all the units without regard to the ID. For this, you want to use group by
select ID, sum(units) totalunits
from sales
group by ID
There is no need for a where clause because you want everything.
Question 2: Can I specify just a cell range?
Yes.
And no.
There is no direct concept of "cell range" in SQL (well, maybe top but not really). Data is stored unordered in SQL. In Excel, the cell range "A2:A10" means "whatever values just happen to be in those cells at this point in time". Often this will mean "the 2nd through 10th values entered in time", or "the first through 9th values entered in time" if there is a header row. But then later you can sort the data differently and now there is different data there. In SQL, there is no order in storage. You can specify an order for the output when you select data, but that is manually specified for each select.
However, the related concept is probably rather obvious. "A2:A10" is often going to mean "the first 9 values by date/time", or "the largest/smallest 9 values" etc.
Your example SELECT ID, sum(units) FROM table WHERE ID = any value in A2:A10 needs to change to define what values you expect to be in A2:A10. For example, if A2:A10 represents the first 9 values by date, you would do something like this: (untested)
select ID, sum(units) totalunits
from sales
where ID in (select top(9) ID
from sales
order by date
)
group by ID
This would provide the sum of units for each of the IDs that were amongst the first 9 IDs entered by date (what to do with a tie for 9th I will not go into here).
Question 3: Can I add a date range cell reference with the possibility that the same ID may appear more than once but have a different date range (see 747375 in sample) and return results for both ranges separately?
This one is difficult to understand. And it might be meaningless based on the answer to your 2nd question. However, you can setup a query that chooses the IDs you want, and in that query you can also select the min and max dates. Finally, you can use the information from that query as a subquery to get the information by ID that has the sum of units within the min/max dates and one that is the sum of units outside the min/max dates. This would require some effort and I will not at this time try to figure that out for you.

how to group rows based on a dates in a single column

I have a range of date (A1:CY7026) with a column for start dates. this column has a large amount of repeated dates within it. i need these dates group together based on the working week they are located in (eg. all values reading 16/7/18 - 22/07/18 would be one group and the following week would make up another).
Use below in B2 cell (Suppose you had a header column) and drag to the rest. Sort to obtain the desired result.
=CONCATENATE(YEAR(A2),"-",WEEKNUM(A2))

Include missing dates as "0" in SELECT ... A,count(A) group BY (Google Sheets, or SQL)

I am creating a histogram of dates using SQL and I want to have "0" values for the missing dates. The specific SQL system I'm using is Google Sheets. Here is my example:
What I really want is for it to look like this:
Is there any way to do this?
The query that I'm using is SELECT a,COUNT(a) GROUP BY a ORDER BY a.
Is there a way to do what I want?
Create a column of the dates you want. For instance, you can put in the first date, the right below it have a formula that adds one day, and copy the formula down.
Then, use COUNTIF or a similar function to get the counts you want, e.g.:
=COUNTIF($A$1:$A:$5,"=" & C2)

How to filter an excel columns which contain date/time as string

I have an excel sheet to filter a Column. The column relates to total experience of a person. The values are like 5years 2Months, 32Years 6Months etc... all the values are in String format. I need the following functionality.
when i enter >5 in a textbox(which i will create in a form), it should display only experience which are less than 5(filtering) . I need an idea how to do this in vba.
Can anyone help..? I just need a way to do this.
Consider the following screenshot. Column a has the unfortunate text with years and months.
Column B splits out the years. Column C splits out the months. Column D has the total number of months for the time frame. With this in place, you can filter by any of the columns using the filter options of the Autofilter built into an Excel table.
The formulas are as follows:
Years: =MID([#total],1,FIND("Years",[#total])-1)+0
Months: =MID(SUBSTITUTE([#total],"Months",""),FIND(" ",[#total])+1,99)+0
Duration in months: =([#years]*12)+[#months]
Now just use the filters in the drop down butttons of the column headers and there is no need for VBA at all.