Cross-Tab Query - Date Column Sort Order - ms-access

I have an Access Cross Tab Query which uses 2 Tables (1) an Accruals Raw Data table and (2) a linked 'Month Order' table.
I am trying to sort the columns in the Cross-Tab query results in date order using the linked 'Month Order' table which links to the Accruals Raw Data table.
However, the query is sorting the Date columns alphabetically as follows instead of in date order.
The Cross Tab query is designed as follows:
Table 1 - Accruals Raw Data Cross Tab
The join between the 2 tables is as follows:
The static 'Month Order' Table has the following fields
The Accruals Raw Data table has a link back to the 'Month Order' table with the following Month Order field:
I know you can enter month names in the Column Headings section below, but I don't want to use that and want to use a separate table for sorting (the 'Month Order' table below) which can be added to instead.
I would appreciate your help.
Thanks

The simple method is to sort columns on year-month, like 2019-03 from, say:
YearMonth: Format([Posted Date], "yyyy-mm")
It will never fail and is easy to read.

Related

How to apply transformation on the lookup target during lookup?

My source task has following columns read from csv file:
Sale_id, sale_date, order_no, sale_amt
This is followed by lookup task that looks into the sales sql table (having same column names) and the join is on order_no column.
The issue is that order_no data in sql sale table has value like 'ABC-12345', 'WXYZ-32111' (there are couple of characters prepended to the order number).
Where as in the csv there is '12345' without any characters prepended.
Hence I cannot do a lookup as there is no direct match. Is there any way to remove the characters and the hyphen from sale sql table data (temporarily) for performing the lookup join.
1st Data Flow Task - Use Flat File Source to LookUp and OLE DB Source via SQL Command with the following to LookUp.
select Sale_id, sale_date, order_no, sale_amt,
substring(order_no,charindex('-',order_no,0),len(order_no)) as [key]
from sql sale table
Use [Key] for your look up transformation.
The functions should provide the numeric values you are looking for.
Problem restatement
In your source, you have order numbers coming in that look like numbers. You need to be able to lookup against a source that has a text string prepended to an order number. We can assume the numeric part of the database's order number is unique.
Setup
I created a simplified version of your table and populated it with data
DROP TABLE IF EXISTS dbo.so_66446302;
CREATE TABLE dbo.so_66446302
(
sales_id int
, order_no varchar(20)
);
INSERT INTO dbo.so_66446302
(sales_id, order_no)
VALUES
(1, 'ABC-12345')
, (2, 'WXYZ-32111')
, (3, 'REALLY_LONG-654321');
A critical piece in using Lookup components is getting the data types to agree. I'm going to assume that the order number from the file is a DT_STR and not an integer.
By default, people pick a table in the Lookup component's Connection tab, dbo.so_66446302 but if you check "Use results of a SQL query", you'll have what you're looking for.
Similar query to what Jatin shows below but I find "showing my work" along the way helps me debug when it goes sideways. In this query, that's why I have the intermediate cross apply steps.
SELECT
S.*
, D0.order_no_as_string_digits
FROM
dbo.so_66446302 AS S
CROSS APPLY
(
SELECT
-- Length of the string less where we find the first dash
LEN(S.order_no) - CHARINDEX('-', S.order_no, 1)
)D(dash_location)
CROSS APPLY
(
SELECT RIGHT(S.order_no, D.dash_location)
)D0 (order_no_as_string_digits);
The results of that query are
sales_id order_no order_no_as_string_digits
1 ABC-12345 12345
2 WXYZ-32111 32111
3 REALLY_LONG-654321 654321
Now you can match the derived order number in the database to the one in your file by dragging the columns together. Check any/all columns that you need to retrieve from the database and send the data to the intended destination.

how to query the same columns from all tables in the schema with given criteria in mysql?

I have a database "warehouse" including tables of daily inventory records, one table for each day.
Now, I need to check the historic change of the inventory level. The output will print the inventory of each day given certain criteria.
I am not sure how to describe it, so I created a simplified sample of the schema, its tables and the expected output.
The schema "warehouse" has a list of tables:
Each table contains the same columns for product ID and inventory, below is table 101
For each table, I need to do a query:
select count(*) as num_of_product_with_inventory from [table name]. After I have the query result from each table, I should have an output like in below:
Can anyone show me how the query should look like to get the final output? I only know the basic queries and have no clue how to put these together. Thank you!
The data model you have is making your work harder than it should be.
If you must keep it, you will need to use a stored procedure or do the loop in your code (not in sql).
But you should really do is change the data model.
It is not recommended at all to create a table per day!
It's a mix of DATA with METADATA. The table structure should represent different types of data that you store, while the fact that you had different inventory on date X vs date Y should be in your data.
So, recommend to create one table with columns date, product_id and warehouse_inventory. If it gets too big, you can partition it by date (week/month/..). Then you can easily get your data with something like:
SELECT date, count(*) AS num_of_products_with_inventory
FROM daily_inventory i
WHERE i.date BETWEEN '<some date>' and '<some date>'
GROUP BY date

Date/time field/column need query to have distinct dates (without times) & sorted correctly

I have a list of records that have a date/time field/column; I want a query to get just the distinct days (removing the time, like 1/1/14, 1/2/14, etc) & sorted correctly. Its going into a form's combo box row source so a user can filter/narrow-down the info on the screen per day
Here is my original query:
SELECT TOP 1 "*** ALL ***" AS ord_tdate FROM qryOrderEntriesNotBilled
UNION SELECT DISTINCT FORMAT(ord_tdate, "mm/dd/yy") FROM qryOrderEntriesNotBilled
ORDER BY 1;
Here is a simplified view for the sake of the question at hand:
SELECT DISTINCT FORMAT(ord_tdate, "mm/dd/yy") FROM qryOrderEntriesNotBilled
ORDER BY 1;
Both of the above work, but the order is wrong, its obviously sorting by number (1st one) rather than overall value as seen here:
Things I have tried unsuccessfully:
DATEVALUE(ord_tdate) solved the simplified view, but with original query (union) it doesn't work correctly as seen below
CDate(Datevalue(ord_tdate)) & CDate(FORMAT(ord_tdate, "mm/dd/yy")) had same results as above
Include a second field expression, DateValue(ord_tdate), in the row source query. Sort by that field. In the combo properties, select 2 as the column count and set the column width for that second column to zero.
This query returns what I think you want in Access 2007 with qryOrderEntriesNotBilled as an actual table instead of a query.
SELECT DISTINCT
FORMAT(ord_tdate, "mm/dd/yy") AS date_as_text,
DateValue(ord_tdate) AS date_as_date
FROM qryOrderEntriesNotBilled
UNION ALL
SELECT TOP 1
"*** ALL ***",
Null
FROM qryOrderEntriesNotBilled
ORDER BY 2;
My personal preference is to use a custom single-row Dual table for the "fabricated" query row. If you would like to try that approach you can find a procedure to create your own here. But a dedicated table is absolutely not a requirement for this. It's purely a matter of developer preference.

How would I display two separate tables within the same query/report without combining each entry?

As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.

Difficult MS Access Query - how to combine them?

I am trying to build an access report based on data from multiple different tables within the database.
I have 3 columns which perform calculations, and I am wondering how to put this query together. All 3 columns deal with dates, but calculate them differently.
The first column retrieves the most recent date of action for a userid if the type of action is "B":
select pid, Max(date) as most_recent
from actions
where ref = 'B'
group by pid;
The second column performs a calculation based on 2 fields, one is a date and one is a number in months. I am unsure how to add these two fields so that the number is added to the date as a number of months.
what i have so far is:
select nummonths,Max(lastvisit) from users
the third column I need to select the first date thats in the future for each user (next appointment date), there will be dates before and after this date so its a little difficult:
select uid,date from visits
The code for the last 2 queries needs to be slightly modified, and I was wondering what the best approach would be to join these all together? A type of join?
If you need to build a report with data from the 3 queries, you will need related data to join them. In that case, please send the structure of the tables.
If you need to show 3 lists in one report, you can use subreports: create a new empty report. In design mode, you can add 3 subreports from the toolbox bar. To each of the subreport assign the record source property to the corresponding sql.
regards
I am unsure how to add these two fields so that the number is added to the date as a number of months.
Use the DateAdd() function:
SELECT DateAdd("m", 2, LastVisit) FROM ...
Results in a date two months from the LastVisit date.