SQL Select statement from mutiple tables to fill datagridview - sql

I have multiple tables, the tables themselves are named after the date they were created on; so for example 4/01/2021, 5/01/2021.. etc
The tables contain all the same columns.
But I'd like to create a SQL statement that allows me to return all the tables that were created between two dates and fill a Datagridview with all the records in those tables.
Ideally I want a "Created Last Week", "Created This week", "Created This Month" options. I can work out th syntax for the start and end dates. But I'm not sure what the correct way is to return the tables that fall between the dates.
I have looked at a few examples but none seem to work for me or be exactly what I'm after. Not sure if I can use sys.tables or if I need to use inner joins/left join etc to get this to work.
My tables are in a Acccess.MDB file.

You will need a union query:
Use a union query to combine multiple queries into a single result
However, as the tables included will vary, you must create the SQL of the query dynamically and then call the query to fill your datagridview.
Note: This is a terrible setup. You should, at the soonest and as suggested by #June7, change your schema to have one table only with a field holding your dates (your current table names).

Related

How do I combine two tables that have only a few similar columns?

I'm attempting to combine two tables, both of which aren't related in any way except for a few columns (ID, Created Date, Country, etc.). In essence, I simply want to append one table to another. However, I would like to combine the columns that are similar and add on the columns that are not similar. I've attempted a Union, but my tables don't have the same number of columns. Currently, I'm working with this:
SELECT * FROM `leads`, `opportunity`
where `leads`.`Id` = `opportunity`.`Id`
which doesn't really work when I want to use this new query as a subquery elsewhere. Additionally, the fields in each table can change at any time, so I’m never sure which columns are matching or non-matching. I simply want to append the rows from one table onto the other while automatically combining columns with identical names. I feel like I'm missing something obvious...
NOTE: I am doing this within DOMO, so I have a few more limitations than I normally would.
You can use joins
SELECT * FROM `leads` JOIN `opportunity`
on `leads`.`Id` = `opportunity`.`Id`
and to get only selected columns
SELECT leads.column_name, opportunity.column_name FROM `leads` JOIN `opportunity`
on `leads`.`Id` = `opportunity`.`Id`

How can I add blank columns to the results of a SQL query?

I'm working on a query that pulls demographic information for people who have visited a location. However, the fields required for this report aren't all available in the DB, and some will need to be added manually from a separate Excel file after I've exported the results of my query. The person who will be responsible for merging the two files asked if it would be possible to create blank columns so they can more easily see where the missing data needs to go, and I wasn't sure how to go about that. Obviously those blank columns could just be created in the exported spreadsheet, but I wondered if there was a way to add them in the SQL query itself.
My SELECT statement currently looks something like this—I've just added comments to mark the missing fields so I can keep track of what order all the fields for this report need to be in.
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
--StaffFName,
--StaffLName,
FacilityPhone,
FacilityAddress,
...and so on
Since those two staff name fields don't exist in my DB, I obviously can't actually include them in the SELECT list. But is there a way to still include those two fields as blank columns in my query results? Something along the lines of "select [nothing] as StaffFName"?
Just add literal nulls to the select clause:
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
null as StaffFName,
null as StaffLName,
FacilityPhone,
FacilityAddress,
...
Or, if you prefer, you can use empty strings instead:
...
'' as StaffFName,
'' as StaffLName,
...
But null is the canonical way to represent the absence of data.

Why isn't "union all" doing what I expect?

I created 2 summary tables form the same source data for different date ranges.
Now that I have these multiple summary tables, I want to put those tables together
so that I will be able to run a summary on the combined table.
It's creating the summary table that is presenting the problem.
scratch.table_1 has 809,598 records.
scratch.table_2 has 1,228,176 records.
They both have the same set of fields from the source table,
plus a "record_number" field I created on each table using count(1).
The code I used to put these two tables together was:
create table scratch.table_1_and_2
select * from scratch.table_1
union all
select * from scratch.table_2
I assumed that there would be 809,598 + 1,228,176 records in the new table (2,037,774 records).
But there are only 1,960,769 records in the new table.
What am i doing wrong?
One way to troubleshoot would be to identify some of the missing records and see what might be different about the data in those that would cause them to be left out. A UNION ALL should include duplicate records so duplicates shouldn't be the issue. Maybe there is some data issue that's causing those records to be dropped. Also I'm assuming there isn't any funny business with Views going on in the underlying tables and that no data loads are affecting your record counts.

select only specific number of columns from Table - Hive

How to select only specific number of columns from a table in hive. For Example, If I have Table with 50 Columns, then how Can I just select first 25 columns ? Is there any easy way to do it rather than hard coading the column names.
I guess that you're asking about using the order in which you defined your columns in your CREATE TABLE statement. No, that's not possible in Hive for the moment.
You could do the trick by adding a new column COLUMN_NUMBER and use that in your WHERE statements, but in that case I would really think twice of the trade off between spending some more time typing your queries and messing your whole table design by adding unnecessary columns. Apart from the fact that if you need to change your table schema in the future (for instance, by adding a new column), adapting your previous code with different column numbers would be painful.

Filter Rows - Pentaho

We are Getting inputs from two different tables and passing it to the Filter rows.
But we are getting the below error.
The DATE_ADDED Table has only one column DATE_ADDED and similarly the TODAYS_DATE Table has a single column TODAYS_DATE .
The condition given in the Filter is DATE_ADDED < TODAYS_DATE .
The transaformation is
Can someone tell, where I am doing the mistake
It won't work like this. You expect a join of two streams (like SQL JOIN of two tables) but actually you will have a union (like SQL UNION).
When two streams are intersected on a step they must have identical columns - names, order and types - and the result will be the union of both streams with the same structure as origins.
When you intersect streams with different structures - different column names in your case - you will have unpredictable column names and actually only one column - nothing to compare with.
To do what you need use the Merge Join step (do not forget to sort streams on the joining key)
Both the column names and types should be identical if you wanna merge the columns in single step, right click on both steps and click output fields to verify the datatypes.
if datatype issues arrives OR you want to rename the columns, you can place select step(for each table steps) after table steps and select the DATE Type(in your case)in the Meta-data tab, and rename the fields as well.
Hope this helps... :)