How can I shift and duplicate rows in MS Access? - ms-access

I am trying to make a calculation in MS Access that uses the previous periods values. My approach is to make a second field for each variable that I am working with and to shift the table.
If my query qry1 is:
aa bb cc
--- --- ---
12 34 56
78 91 01
My result should be:
aa0 bb0 cc0 aa1 bb1 cc1
---- ---- ---- ---- ---- ----
12 34 56 NULL NULL NULL
78 91 01 12 34 56
NULL NULL NULL 78 91 01
I am encountering two problems:
I tried adding the empty rows on top and at the bottom in two extra queries and somehow rows get lost.
When I combine these two queries using JOIN I get a cartesian product, i.e. the rows are not side by side.

You are trying to use Ms Access like you would use Excel. It might be easier to do what you want using an Excel macro + formulae.
The power of access is that it is a SQL system (which is why you get a Cartesian product when you JOIN, that's the idea) it sounds like what you want could be done pretty easily with a flat spreadsheet. You could write VBA to add rows for you if want to automate that part.
If that doesn't help, then maybe describe in more detail what you actually want to accomplish.
EDIT:
I missed the part in your post where you said you were doing a calculation using previous values. This is how I would accomplish that within a single Access query:
qry_Data: (based on tbl_Data)
id aa bb cc calc
--- --- --- --- ---
01 12 34 56 =Dlookup("aa","tbl_Data","id = " & [id] - 1)
02 78 91 01 =Dlookup("aa","tbl_Data","id = " & [id] - 1)
You shouldn't add columns to your table as part of a normal operation. Access tables should be set up to hold all the data they will ever need to (until your model changes). If you are designing an operation in access and it seems like it should add columns, then you should be adding a linked table or using a calculated field. In the above example, the field calc will be equal to the value "aa" from the record with an ID# one less than the current record. This will only work if there is a record with that ID#, so if there is a possibility of there being a gap in ID numbers you shouldn't use this exact method. Since you said that the table is sorted on specific critiera you might need to use a different method to determine the previous record. One way to do that in VBA would be like this:
Dim RS as Recordset
Set RS = CurrentDB.QueryDefs("myQuery")
RS.MoveLast
RS.MovePrevious
At that point you are on the second to last record and can access any values from that record with:
RS.Fields("aa")
Without knowing exactly what you're trying to do I can't make any more specific recommendations except that if you familiarize yourself with basic SQL concepts and you will find working in Access much easier.

Related

How to sum only one of repeated values from joined data in RDLC

I'm not sure if SSRS is dumb, or I am (I'm leaning towards both).
I have a dataset that (as a result of joins etc) has some columns with the same values duplicated across every row (fairly standard database stuff):
rid cnt bid flg1 flg2
-------------------------------
4 2882 1 17 3
5 2784 1 17 3
6 1293 1 17 3
18 9288 2 4 9
20 762 2 4 9
Reporting based on cnt is straightforward enough. I can also make a tablix that shows the following:
bid flg1 flg2
------------------
1 17 3
2 4 9
(Where the tablix is grouped by Fields!bid.Value and the columns are just Fields!flg1.Value and Fields!flg2.Value respectively.)
What I can't figure out is how to display the sum of these values -- specifically I want to show that the sum of flg1 is 21 and the sum of flg2 is 12 -- not the sum of every row in the dataset (counting each value more than once).
(Note that I'm not looking for a sum of distinct values, as they may not be unique. I want a sum of one value from each bid group, because it's from a table join so they will always have the same value.)
If possible, I'd also like to be able to do a similar calculation at the top level of the report (not in any tablix); although I'd settle for hiding the detail row if that's the only way.
Obviously, Sum(Fields!flg1.Value) isn't the answer, as this either returns 51 (if on the first row inside the group) or 59 (if outside it).
I also tried Sum(Fields!flg1.Value, "bid") but this wasn't considered a valid scope.
I also tried Sum(First(Fields!flg1.Value, "bid")) but apparently you're not allowed to sum first values for some weird reason (and may have had the same scope problem anyway).
Using Sum(Max(Fields!flg1.Value, "bid")) does work, but feels wrong. Is there a better way to do this?
(Related: is there a good way to save the result of that calculation so that I can later also show a Sum of those totals without an even hairier expression?)
There are two basic ways to do this.
Do what you have already done (Sum(Max(Fields!flg1.Value, "bid")))
Sum the rendered values. To do this check the name of the cell containing the data you want (check it's properties) and then use something like =SUM(ReportItems!flg1.Value) where flg1 is the name of the textbox, which is not necessarily always the same name as the field.

MS access: Merging 02 tables into 01

I am using Microsoft Access database (Please don’t suggest me to use some other database since I donot have that choice in this particular situation).
I have 02 tables as below.
Table 1: Purchase Order
PO_No, Item_No, Order_Qty
01 a 10
01 b 5
02 a 8
02 b 2
Table 2: Stock
Item_No, Batch_No, Qty
a 0001 5
a 0002 50
b 0003 10
=> I would like to create something like this:
Item_No, Batch_No, Ship_Qty, PO_No
a 0001 5 01
a 0002 5 01
a 0002 8 02
b 0003 5 01
b 0003 2 02
Please help!
EDIT: Make-Table Example
You still need to read through at least some of the links that I posted earlier but I don't think you we getting what I meant so here's an animation to show you the steps.
Create Select Query from the 2 tables
Test Select Query to make sure output is what you want in new table
Convert Select Query to Make Table Query
Execute Make Table Query
This is a very short process, just a few minutes.
Good Luck.
STEP 1:
First you need to create a Select Query using both of your tables, joined together, similar to this: (See the tutorials at the bottom of this answer.)
STEP 2:
Once the Select Query is producing the data how you want it organized, you can convert it to a Make Table query:
Tutorials:
Skillshare : Intro to Access - Microsoft Access Basics for Beginners
Microsoft.com : Introduction to Queries: Access
YouTube : Acccess 2016: Multi-table queries
Office.com : Join tables and queries
Microsoft.com : Create a make-table query
Well kudos to ashleedawg for that effort.
In the end - your very first step must be to make a select query that will result in the records/fields correctly of what will be the new table.
The second step is then to make a Make Table Query. But that can wait for now.
In glancing at your 2 source tables, and the resulting 3rd table you seek - - I did not see any join logic that would create that result. If you can explain what is the logic - - then help with that select query can be provided.

To calculate sum of the fields in a matrix with column grouping

I am working on a ssrs report with column grouping. the followin is my scenario.
Matrix 1:
ID 2012 2013
1 20 40
1 30 50
Total 50 90
Matrix 2:
ID 2012 2013
1 60 70
1 60 80
Total 120 150
I need the sum of matrix1 and matrix2 like below:
ID 2012 2013
1 170 240
But I got the result like :
ID 2012 2013
1 410 410
I have applied column grouping in all the 3 matrices and gave the expression to get sum for matrix 3 as: =Sum(Fields!amount1.Value, "dsmatrix1") + Sum(Fields!Tamount1.Value, "dsmatrix2")
Please help me to get a solution for this.
Thanks!
I think I know what's going on. Correct me if I'm wrong.
Based on what I'm seeing, I'm guessing that Matrix 1 and Matrix 2 only have three fields each, an ID field, an amount field (being "amount1" or "Tamount1"), and a year field.
Your column grouping is manipulating the display of the data to show all values broken out by year. This works fine when looking at data from a single dataset. However, your formula is specifying that the sum of everything in the Amount1 field of dsmatrix1 and the Tamount1 field of dsmatrix2 should be added. This does not take into account the column grouping. Your expression is essentially taking all of the values from both datasets and adding them together.
Not knowing more about your query structure or how the data is filtered, my best guess is that you need another SQL dataset. In this case, you would take the queries from your two previous datasets and union them with the "Union All" command. Note that you will want to use Union All and not just Union. More on that here: What is the difference between UNION and UNION ALL?
Your end result should look something like this:
--This will be your dsmatrix1 query copied and pasted
Select ...
Union All
--This will be your dsmatrix2 query copied and pasted
Select ...
--Place one single Order by clause at the bottom
Order by ...
Note: for your two queries to be unioned properly, you'll need to make sure that each have the same number of fields, each with the same data types. Then you can point your third matrix to the new dataset.
Hope that helps!

how to push data down a row in sql results

I would like help with sql query code to push the consequent data in a specific column down by a row.
For example in a random table like the following,
x column y column
6 6
9 4
89 30
34 15
the results should be "pushed" down a row, meaning
x column y column
6 null or 0 (preferably)
9 6
89 4
34 30
SQL tables have no inherent concept of ordering. Hence, the concept of "next row" does not make sense.
Your example has no column that specifies the order for the rows. There is no definition of next. So, what you want to do cannot be done.
I am not aware of a simple way to do this with the way you are showing the table being formatted. If your perhaps added two consecutively numbered integer fields that provide row number and row number + 1 values, you could join the table to itself and get that information.
After taking a backup of you table:
Make a PHP function that will:
- Load all values of Y into an array
- Set Y = 0 (MYSQL UPDATE)
- load the values back from PHP array to MYSQL

Compare two source and update sql server table in SSIS?

I have excel source and sql server table .
Excel Source column is
Mno Price1 Price2
111 10 20
222 30 25
333 40 30
444 34 09
555 23 abc
Sql server Table
Product table name
PId Mno Sprice BPrice
1 111 3 50
2 222 14 23
3 444 32 34
4 555 43 45
5 666 21 67
I want to compare excel source Mno(Model number) with sql server Product table Mno (Model number), and if it is same i want to update Sql server Product table SPrice and Bprice.
Please tell me what are the steps i want to do?
I want to validate that excel sheet also, because in excel Price2 column have string values
if it's string value i want to send mail which row data are wrong.
I am new for SSIS so please give me details.
Read your new data in a source, use a lookup component for existing data. Direct row matches to a oledb command for update, and a destination for your non-matches for inserts (if you want to enter new products).
Personally I think the simplest way to do this is to use a dataflow to bring the excel file into a staging table and do any clean up if need be. Then as the next step inmteh control flow have an Execute SQl task that does the update. Or if you need either an update or an insert if the record is new, use a Merge statement in the Execute SQl task.
You can use a Merge Join Transformation with a full outer join (remember to sort your datasets before they input to the Merge Join Transformation), then have the output go to a Conditional Split Transformation. The Conditional Split Transformation can determine whether or not a row needs to be updated, inserted, or deleted and direct the flow to the appropriate transform to do that.
This was off the top of my head, and there may be a simpler transform for this. I haven't had the opportunity to work with SSIS in almost a year, so I might be getting a bit rusty.