SSRS How to Concatenate Multiple Rows with Dynamic Columns - sql-server

Right now I have dynamic columns based off of a query, and then I have data attached to those columns that I want to populate in rows. They associate just fine, but the problem is that the rows hold whatever place they were in, instead of ascending to the top again, like so:
|column|column2|column3|
| row1 | | |
| | row2 | |
| | | row3 |
And the goal is:
|column|column2|column3|
| row1 | row2 | row3 |
| | | |
| | | |
I know that I could do this in the query, combining the headers with a dynamic query, but is there any SSRS magic that can achieve this without that?
EDIT 1
I am using a matrix, sorry about not specifying, I heard that the only way to do dynamic columns are matrices, so I thought it was implied.
EDIT 2
The rows come in like
Wanted_Column | Wanted Row
Column | data
Column2 | data
Column | data
and I want it so that the table will look like
|column|column2|
| data | data |
| data | |
| | |
for any number of columns/rows

I'm assuming you are using a matrix to do this. It's not clear from your question... Anyway, you'll need to add row grouping. If you don't have data that can be grouped by an actual column value then set the group expression to 1 and that should do it.
If this is not correct then please show you data as it comes from the dataset and the expected output.

Related

Merge multiple tabs in Google Sheets and add a column for where the data came from

I have a spreadsheet which contains multiple tabs with similar layouts. I want to use a formula to merge these into a single tab which has a new column naming the tab it came from.
Example
Tab: Area A
| Item | Status |
|------|-------------|
| Foo | Blocked |
| Bar | In Progress |
Tab: Area B
| Item | Status |
|--------|-----------|
| Foobar | Completed |
Tab: Merged
| Area | Item | Status |
|------|--------|-------------|
| A | Foo | Blocked |
| A | Bar | In Progress |
| B | Foobar | Completed |
Merging without new column
I can merge the data without the additional column, using this formula:
=ARRAYFORMULA(SORT({'Area A'!A2:B; 'Area B'!A2:B}))
Which looks like this:
|--------|-------------|
| Item | Status |
|--------|-------------|
| Foo | Blocked |
| Bar | In Progress |
| Foobar | Completed |
Adding the Area column
What's missing from the above formula is the addition of the area column. This would be possible by cross-referencing the item in every tab using a vlookup and labelling it. But that wouldn't be very efficient and some updates are already slow to re-calculate in this document. I expect this to have approx. 40 tabs with 10,000 rows in total to merge.
Eg:
=IFS(NOT(ISERROR(VLOOKUP(B2,'Area A'!A$2:A,1,FALSE))), "A", NOT(ISERROR(VLOOKUP(B2,'Area B'!A$2:A,1,FALSE))), "B")
Is there a better way to do this?
I'd like something like this, but it doesn't work as the constant I'm adding doesn't match the number of rows it needs to be:
=ARRAYFORMULA(SORT({{"A",'Area A'!A2:B}; {"B", 'Area B'!A2:B}}))
you can borrow empty column and do:
=ARRAYFORMULA(SORT({{'Area A'!X2:X&"A", 'Area A'!A2:B};
{'Area B'!X2:X&"B", 'Area B'!A2:B}}))
or you can add it to first column and then split it:
=ARRAYFORMULA(QUERY(SORT({SPLIT(
{"A♦"&'Area A'!A2:A;
"B♦"&'Area B'!A2:A}, "♦"),
{'Area A'!B2:B;
'Area B'!B2:B}}), "where Col2 is not null", 0))
see: https://stackoverflow.com/a/63496191/5632629

How to get data into a CTE and do an update

Using MS SQL Server, I want to:
Get data from my application
Perform preprocessing
Update a table
Steps 1 and 3 are giving me problems.
Simplifying the problem to its essence, my existing data looks like:
+--------+-------+
| Item | Usage |
+--------+-------+
| Part A | 10 |
| Part B | 15 |
| Part C | 8 |
+--------+-------+
and an example of the source data is:
+--------+
| Item |
+--------+
| Part A |
| Part B |
| Part B |
| Part B |
| Part A |
| Part A |
+--------+
My over all plan is to import the data into a CTE, do the preprocessing, then do an update.
Regarding getting the data, since "INTO" is not allowed in a CTE, how can I get the source data into the CTE. Or some other approach not using a CTE better?
Preprocessing is straightforward. Here is my SQL:
WITH MyData (Item, NewUsage)
AS
(
<Somehow get the data>
SELECT Item, Count(*) as NewUsage
FROM Items
GROUP BY Item
)
UPDATE Items
SET Usage = Usage + b.NewUsage
FROM Items as a JOIN MyData as b ON a.Item = b.Item;
The update is updating all the rows in Items by 1 instead of using the NewUsage column.
How do I get my data (into the CTE?) and how to write the SQL so it works properly?

Create/Update table in MS Access dynamically

EDIT:
Here's what I have: An Access database made up of 3 tables linked from SQL server. I need to create a new table in this database by querying the 3 source tables. Here are examples of the 3 tables I'm using:
PlanTable1
+------+------+------+------+---------+---------+
| Key1 | Key2 | Key3 | Key4 | PName | MainKey |
+------+------+------+------+---------+---------+
| 53 | 1 | 5 | -1 | Bikes | 536681 |
| 53 | 99 | -1 | -1 | Drinks | 536682 |
| 53 | 66 | 68 | -1 | Balls | 536683 |
+------+------+------+------+---------+---------+
SpTable
+----+---------+---------+
| ID | MainKey | SpName |
+----+---------+---------+
| 10 | 536681 | Wing1 |
| 11 | 536682 | Wing2 |
| 12 | 536683 | Wing3 |
+----+---------+---------+
LocTable
+-------+-------------+--------------+
| LocID | CenterState | CenterCity |
+--- ---+-------------+--------------+
| 10 | IN | Indianapolis |
| 11 | OH | Columbus |
| 12 | IL | Chicago |
+-------+-------------+--------------+
You can see the relationships between the tables. The NewMasterTable I need to create based off of these will look something like this:
NewMasterTable
+-------+--------+-------------+------+--------------+-------+-------+-------+
| LocID | PName | CenterState | Key4 | CenterCity | Wing1 | Wing2 | Wing3 |
+-------+--------+-------------+------+--------------+-------+-------+-------+
| 10 | Bikes | IN | -1 | Indianapolis | 1 | 0 | 0 |
| 11 | Drinks | OH | -1 | Columbus | 0 | 1 | 0 |
| 12 | Balls | IL | -1 | Chicago | 0 | 0 | 1 |
+-------+--------+-------------+------+--------------+-------+-------+-------+
The hard part for me is making this new table dynamic. In the future, rows may be added to the source tables. I need my NewMasterTable to reflect any changes/additions to the source. How do I go about building the NewMasterTable as described? Does this make any sort of sense?
Since an Access table is a necessary requirement, then probably the only way to go about it is to create a set of Update and Insert queries that are executed periodically. There is no built-in "dynamic" feature of Access that will monitor and update the table.
First, create the table. You could either 1) do this manually from scratch by defining the columns and constraints yourself, or 2) create a make-table query (i.e. SELECT... INTO) that generates most of the schema, then add any additional columns, edit necessary details and add appropriate indexes.
Define and save Update and Insert (and optional Delete) queries to keep the table synced. I'm not sharing actual code here, because that goes beyond your primary issue I think and requires specifics that you need to define. Due to some ambiguity with your key values (the field names and sample data still are not sufficient to reveal precise relationships and constraints), it is likely that you'll need multiple Update statements.
In particular, the "Wing" columns will likely require a transform statement.
You may not be able to update all columns appropriately using a single query. I recommend not trying to force such an "artificial" requirement. Multiple queries can actually be easier to understand and maintain.
In the event that you experience "query is not updateable" errors, you may need to define other "temporary" tables with appropriate indexes, into which you do initial inserts from the linked tables, then subsequent queries to update your master table from those.
Finally, and I think this is the key to solving your problem, you need to define some Access form (or other code) that periodically runs your set of "sync" queries. Access forms have a [Timer Interval] property and corresponding Timer event that fires periodically. Add VBA code in the Form_Timer sub that runs all your queries. I would suggest "wrapping" such VBA in a transaction and adding appropriate error handling and error logging, etc.

How to implement custom sort order with some items at fixed places in solr?

I have a list of drawers in a mysql table named Drawers like
| BOX | RANK |
|----------------|--------|
| Box1 | 1 |
| Box2 | 2 |
| Box3 | 3 |
| Box4 | 4 |
| Box5 | 5 |
Then I have another source which says some of these boxes contains jewel and should be placed at a specific position only(Lets call this table jewelboxes).
| BOX | RANK |
|----------------|--------|
| Box1 | 4 |
| Box3 | 1 |
| Box5 | 3 |
I have certain restrictions that needs to adhere to:
I cannot write a stored proc on these tables
I want to get a list of Boxes on Solr where position of the jewelboxes should be fixed irrespective of the calling order(ascending/descending). for example,
ascending order would be:
| BOX | RANK |
|----------------|--------|
| Box3 | 1 |
| Box2 | 2 |
| Box5 | 3 |
| Box1 | 4 |
| Box4 | 5 |
descending order would be:
| BOX | RANK |
|----------------|--------|
| Box3 | 1 |
| Box4 | 2 |
| Box5 | 3 |
| Box1 | 4 |
| Box2 | 5 |
I am importing these tables into solr from dih, and currently ripping my hair apart thinking about how to do this. I have 2 options in my mind, but both are not very clear, and would like you folks here to help me out. My options are:
Write a query in such a way that'll give me correct order. (this would need a master level querying skills because all we have is a select query in dih)
Write a CustomFieldComparator as described in the following link: http://sujitpal.blogspot.in/2011/05/custom-sorting-in-solr-using-external.html
Is there any third approach which can be followed to get the desired results ?
UPDATE:
I can work without the descending order criteria, but I still need the ascending one.
Thanks :-)
I would create a custom indexer in a language you are comfortable with (see https://wiki.apache.org/solr/IntegratingSolr - I work in Python and use mysolr) when you need more flexibility like this. Maybe create two fields, one for "ascending" and one for "descending"; make a list with the ranks, one with reverse ranks, then query the second source to use for overrides on both lists - or whatever you need to do to process the data. I guess the feasibility also depends on how many records you need to index. Then process each record, adding the information for the two ranking fields from the list, and send them to your Solr server in batches.

Making an Object Dependent Number of Fields for a Table in MS.Access

I'm trying to make a database that will hold a table of objects, and these objects are comprised of objects from a second table. One table is a table of possible sets, and the second is a table of possible components. The table of sets has to include fields for each of its components, but each set has an unknown number of components. How do I make a table with fields (Component 1, Component 2, Component 3, ...) that are dependent on each set to decide how many of the fields it needs?
Is there a way to do this just using the Access interface or will I actually have to get into the code behind it?
I think it would also solve my problem if there were a way to make a field in a column that acted as an ArrayList so if anyone could think of how to do that please let me know.
Assuming that a component can be part of more than one set, what you need here is a many-to-many relationship.
In a database you don't do this with an arbitrary number of columns, you use a junction table.
When you need a tabular representation, you use a Pivot / Crosstab query.
Your data model could look like this:
Sets
+--------+----------+
| Set_ID | Set_Name |
+--------+----------+
| 1 | foo |
| 2 | bar |
+--------+----------+
Components
+--------------+----------------+
| Component_ID | Component_Name |
+--------------+----------------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
+--------------+----------------+
Junction table
+----------+----------------+
| f_Set_ID | f_Component_ID |
+----------+----------------+
| 1 | 2 |
| 1 | 4 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
+----------+----------------+
(f_ as in Foreign Key)

Resources