I am trying to achieve the following layout for my report based on one query.
+----+-------+----+-------+
| ID | Name | ID | Name |
+----+-------+----+-------+
| 1 | Danny | 2 | Dave |
| 3 | Sue | 4 | Jack |
| 5 | Rita | 6 | Sarah |
+----+-------+----+-------+
So I basically want one table, printing my data from left to right to save space on my page, rather than it printing one line and wasting all of the space on the right side of the paper, possibly even go 3 times across the width.
This is my data: http://sqlfiddle.com/#!3/5c911/1
I was maybe thinking a table with 4 columns. Cols 1 and 2 contain the odd row numbers, Cols 3 and 4 contain the even row numbers.
How could I achieve this, I did try something with the MOD function but it didn't seem to work properly, or I misunderstood what was happening.
Related:
How can I display two rows worth of data on one line side-by-side in Report Designer?
Thanks,
To print your data from left to right in a multi-column format, you need to fake it using multiple tables. To implement this hack, create the same number of tables as columns you want side by side that all point to your data set. On the Detail row of the first table, for the Visibility-Hidden property use the following formula:
=IIF((RowNumber(Nothing) Mod 4) = 1, False, True)
where 4 is the number of tables (columns) you have.
Do the same for each table, incrementing what the formula is equal to (so for the second column (RowNumber(Nothing) Mod 4) = 2 and so forth). In the last table (column) the formula equals 0.
This alternately hides the detail row, only displaying the appropriate rows for that column number.
You can achieve that look with query.
SELECT std1.id AS Student_Id,
std1.NAME AS Student_Name,
std2.id AS Student_Id,
std2.NAME AS Student_Name
FROM students std1, students std2
WHERE (std2.id - std1.id = 1
AND std1.id %2 = 1);
Related
2-column MySQL Table:
| id| class |
|---|---------|
| 1 | A,B |
| 2 | B,C,D |
| 3 | C,D,A,G |
| 4 | E,F,G |
| 5 | A,F,G |
| 6 | E,F,G,B |
Requirement is to generate a report/output which tells which individual CSV value of class column is in how many rows.
For example, A is present in 3 rows (with id 1,3,5), and C is present in 2 rows (with id 2,3), and G is in 4 rows (3,4,5,6) so the output report should be
A - 3
B - 3
C - 2
...
...
G - 4
Essentially, column id can be ignored.
The draft that I can think of - first all the values of class column need to picked, split on comma, then create a distinct list of each unique value (A,B,C...), and then count how many rows contain the unique value from that distinct list.
While I know basic SQL queries, this is way too complex for me. Am unable to match it with some CSV split function in MySQL. (Am new to SQL so don't know much).
An alternative approach I made it to work - Download class column values in a file, feed it to a perl script which will create a distinct array of A,B,C, then read the downloaded CSV file again foreach element in distinct array and increase the count, and finally publish the report. But this is in perl which will be a separate execution, while the client needs it in SQL report.
Help will be appreciated.
Thanks
You may try split-string-into-rows function to get distinct values and use COUNT function to find number of occurrences. Specifically check here
I am creating an unusual SSRS report that requires that the user be able to use parameters to select which of the (more than 250) fields appear in the report. So the number of columns in this report can vary greatly.
I've been mostly successful at implementing this, but am stuck at controlling how to change the order of the columns.
Here is (a simplified example) of my original data:
My data as a screen capture
CompanyID | Address | Website_URL | Date_Created | Date Modified |
1 |123 Main Street|www.fake.com | 3/14/2019 | 3/15/2019 |
2 |555 Park Ave |www.notreal.com|3/12/2019 | 3/13/2019 |
The first thing I've done is to unpivot my data within my dataset (i used cross apply to do this). The name of what the column used to be is kept in a column named something like "Col_1", and the value is kept in a column named something like "Val_1". The trick is, I have to do this multiple times, once for each data type that I'm dealing with. Because obviously you can't have dates and nvarchars in the same column. When I unpivot the data above, it looks like this:
CompanyID | Col_1 | Val_1 | Col_2 | Val_2 |
1 |Address |123 Main Street | Date_Created | 3/14/2019 |
1 |Website_URL |www.fake.com |Date Modified | 3/15/2019 |
2 |Address |555 Park Ave |Date_Created |3/12/2019 |
2 |Website_URL |www.notreal.com |Date Modified |3/13/2019 |
The point in doing this is now I can create a matrix is the SSRS report with the CompanyID as a row group. Then I create two adjacent column groups for Col_1, and Col_2, which have as their values Val_1 and Val_2, respectively.
Click here to see SSRS Groupings
Now, when this report runs, each column group (for example, Col_1) expands out to show all the column names I had under that column in my unpivoted data. This could be dozens of columns. This picture shows what my final data looks like. This is similar to what my original data looked like. But with the benefit of the fact that the columns are being displayed dynamically.
My resulting Matrix
So, the only problem I'm having is that the columns are stuck within their groups. Say I want to sort them alphabetically, I can only sort the nvarchars together, and the dates together. I cannot sort the across their groups. Is there a way I can do this?
The resulting Matrix I want, with columns sorted alphabetically
Thanks in advance for any ideas.
Using your original unpivoted data, the design of your report needs to have 4 column groups.
1.Address
2.Date created
3.Date modified
4.Website URL
I have two tables and i want to add there row based on its name. I've search on net but I only found how to combine the total value or two tables and combine them. The result will be added on a table named Result
Table 1 Table 2 Result
Name | Value Name | Value Name | Value
Apple | 2 Apple | 4 Apple | 6
Orange | 3 Orange | 2 Orange| 5
Thank you in advance
First of all I would like to say you must try to get solution at your own.
For your case, answer is very simple. Try this query :
SELECT table1.`name`, (table2.value + table1.value) AS `value` FROM table1
LEFT JOIN table2 ON table1.`name` = table2.`name` WHERE table1.`name` = table2.`name`
I am trying to achieve the following layout for my report based on one query.
+----+-------+----+-------+
| ID | Name | ID | Name |
+----+-------+----+-------+
| 1 | Danny | 2 | Dave |
| 3 | Sue | 4 | Jack |
| 5 | Rita | 6 | Sarah |
+----+-------+----+-------+
So I basically want one table, printing my data from left to right to save space on my page, rather than it printing one line and wasting all of the space on the right side of the paper, possibly even go 3 times across the width.
This is my data: http://sqlfiddle.com/#!3/5c911/1
I was maybe thinking a table with 4 columns. Cols 1 and 2 contain the odd row numbers, Cols 3 and 4 contain the even row numbers.
How could I achieve this, I did try something with the MOD function but it didn't seem to work properly, or I misunderstood what was happening.
Related:
How can I display two rows worth of data on one line side-by-side in Report Designer?
Thanks,
To print your data from left to right in a multi-column format, you need to fake it using multiple tables. To implement this hack, create the same number of tables as columns you want side by side that all point to your data set. On the Detail row of the first table, for the Visibility-Hidden property use the following formula:
=IIF((RowNumber(Nothing) Mod 4) = 1, False, True)
where 4 is the number of tables (columns) you have.
Do the same for each table, incrementing what the formula is equal to (so for the second column (RowNumber(Nothing) Mod 4) = 2 and so forth). In the last table (column) the formula equals 0.
This alternately hides the detail row, only displaying the appropriate rows for that column number.
You can achieve that look with query.
SELECT std1.id AS Student_Id,
std1.NAME AS Student_Name,
std2.id AS Student_Id,
std2.NAME AS Student_Name
FROM students std1, students std2
WHERE (std2.id - std1.id = 1
AND std1.id %2 = 1);
Lookup table - unique row identity
The other lookup tables just do not make sense as from what I have seen giving a row an ID then putting that id in another table which also has a id then adding these id's to some more tables which may reference them and still creating a lookup tables with more id's (this is how all the examples I can find seem) What I have done is this :
product_item - table
------------------------------------------
id | title | supplier | price
1 | title11 | suuplier1 | price1
etc.
it then goes on to include more items (sure you get it)
product_feature - table
--------------------------
id | title | iskeyfeature
1 | feature1 | true
feature_desc - table
-----------------------------
id | title | desc
1 | desc1 | text description
product_lookup - table
item_id | feature_id | feature_desc
1 | 1 | 1
1 | 2 | 2
1 | 3 | 3
1 |64 | 15
(as these only need to be referenced in the lookup the id's can be multiples per item or multiple items per feature)
What I want to do without adding item_id to every feature row or description row is retrieve only the columns from the multiple tables where their id is referenced in the same row of the lookup table. I want to know if it is possible to select all the referenced columns from the lookup row if I only know the item_id eg. Item_id = 1 return all rows where item_id = 1 with the columns referenced in the same row. Every item can have multiple features and also every feature could be attached to multiple items , this will not matter if I can just get the pattern right in how to construct this query from a single known value.
Any assistance or just some direction will be greatly appreciated. I'm using phpmyadmin, and sure this will be easier with some php voodoo I am learning mysql from tutorials ect and would like to know how to do it with sql directly.
Having a NULL value in a column is not the major concern that would lead to this design - it's the problem with adding new attribute columns in the future, at which MySQL is disgracefully bad.
If you want to make a query that returns everything about an item in one row, you need to LEFT OUTER JOIN back to the product_lookup table for each feature_id. This is about every 10th mysql question on Stack Overflow, so you should be able to find tons of examples.