MySQL: select columns based on other column value - mysql

I have a table named t,
focus, H, Li, Be, B, C
H, 1, 2, 3, 4, 5
Be, 6, 7, 8, 9, 10
I want to write a MySQL query to select columns in this table based on the values of focus column, for example, from table t it will return:
H, Be
1, 3
6, 8
I know it's very easy to write:
select H, Be from t
However, this is not a dynamic resolution if the content of table t changed. For example, assuming table t now is:
focus, H, Li, Be, B, C
B, 5, 0, 0, 4, 4
C, 8, 9, 1, 7, 3
Previous code doesn't work. It still returns the same result instead of:
B, C
4, 4
7, 3
My question is, is it possible, we wrote a MySQL script to select the columns based on the values of focus column?

instead os selected the column based on focus value, why do you select focus based on the column. See the below example
Declare #sql varchar(10),#SQLString varchar(100)
Set #SQL = ‘H’
Set #SQLString = ‘Select focus, ‘+#sql +
‘From tablet
Where focus = ‘+#SQL
Exec #SQLString

Related

SSRS Show table list in sequence

I have a list of data, with sequence id 1, 2, 3, ..., 9
I want to insert them into SSRS report in the form of 3x3 table, arranged by sequence id
1 2 3
4 5 6
7 8 9
I should use list, or table, or matrix? Any solution for this arrangement? Thanks
I would typically use a matrix for this. You can calculate the row and column numbers for each row of data in your table and then use that for your row and column groups in the matrix.
Here's an example...
Note: You might not have or want the 'label' field so just swap this out for whichever field you want to show when we get to the report design.
Here's some sample data with the Row and Col calculated
DECLARE #t TABLE (Seq int, Label varchar(10))
INSERT INTO #t VALUES
(1, 'AAAAA'), (5, 'BBBBB'), (10, 'CCCCC'),
(20, 'DDDDD'), (50, 'EEEEE'), (100, 'FFFFF'),
(101, 'GGGGG'), (102, 'HHHHH'), (210, 'IIIII')
SELECT
*
, ColN = (SeqOrder-1) %3
, RowN = (SeqOrder-1) / 3
FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY Seq) AS SeqOrder FROM #t) t
The inner query just assigns the SeqOrder as 1 thru 9. We then use this SeqOrder value to determine the row and column
This gives us the following dataset
Then we just add a matrix to our report
Next, drag the ColN field to the "Columns" placeholder, the RowN field to the "Rows" placeholder and the Label field (or whatever field you want to display) to the "Data" placeholder.
Run the report and you get this
Optionally, you can remove the first row and column (but NOT the associated group) and just leave the data cell.
Now when we run it, we get this.

SSRS -- Create Multi Chart by Project with 3 charts per row and infinite rows ifs necessary . (not subreport)

I have a report with the differnet projects and with columns of values, etc. The thing that i'm looking for and i failed many times is i want to create a auto multi circle chart by project having only 3 charts per row and infinite rows depending of the projects that i have.
I have taken a look to this example but it doesnt work in me report
Dynamic control of number of charts in SSRS reports
Example
This is the visual chart example for one project
Example Project A
What i want to take, is an automatic multi chart that copy and paste this example chart for each a project creating a grid with 3 columns (to limit the width of the SSRS) and the necessary rows to complete all projects
Result that i want
Example multichart
Can someone help me on this? i'm stuck on this
You can do this without using subreports. You first determine a row and column for each project and then use a matrix to display the results.
I've mocked up some data for the purpose of this example which you will have to update to suit your needs.
DECLARE #t TABLE (Project varchar(10))
INSERT INTO #t VALUES ('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H')
DECLARE #prjData TABLE (Project varchar(10), N INT, U INT, C INT, S INT)
INSERT INTO #prjData VALUES
('A', 10, 20, 30, 40),
('B', 11, 21, 31, 37),
('C', 12, 22, 32, 34),
('D', 13, 23, 33, 31),
('E', 14, 24, 34, 28),
('F', 15, 25, 35, 25),
('G', 16, 26, 36, 22),
('H', 17, 27, 37, 19)
SELECT pd.*, rc.iCol, rc.iRow FROM #prjData pd
JOIN (
SELECT
*
, iRowNumber = ROW_NUMBER() OVER(ORDER BY Project)
, iCol = ((ROW_NUMBER() OVER(ORDER BY Project)-1) % #MaxCols)
, iRow = CAST(((ROW_NUMBER() OVER(ORDER BY Project)-1) / #MaxCols) as INT)
FROM #t
) rc on pd.Project = rc.Project
The inner query (alias rc) works out the row and column number for each project and then we just join that to your existing data. table variable #t will be replaced by your own table containing a list of projects.
#MaxCols is a simple variable that you can use to adjust the number of columns if you need to, or your can just hardcode this value. In the output below I used '3'. I've not declared this in the query as this will be passed from the report as a parameter.
N, U C and S are to represent your 4 values in your chart.
This gives us the following results.
Now we have our dataset, add a matrix control, drag iRow from the dataset fields to the "Rows" placeholder, iCol to the "Columns" placeholder and in the "Data" placeholder insert a pie chart.
Resize the Chart cell to suit your needs...
Now click the chart and set the values, series etc as desired.
Set the chart title to the Project field (in this case it's just a letter)
You report design will look something like this..
When you run the report you should get something like this...
Here is the same report again with MaxCols set to 4
Finally you can remove the redundant rows/columns if required

PostGIS: Finding duplicate label within a radius

I have data in PostGIS that have value and geometry. If there is a same value within let say <10 m, I wanna detect or remove that value from my table. Here is the small example:
create table points (id serial primary key, val integer, label2);
select addGeometryColumn('points', 'geom', 1, 'point', 2);
insert into points (id, val, label2, geom) values
(1, 1, aaa, st_geomFromText('POINT(1 1)', 1)),
(2, 1, bbb, st_geomFromText('POINT(1 2)', 1)),
(3, 1, aaa, st_geomFromText('POINT(10 100)', 1)),
(4, 2, ccc, st_geomFromText('POINT(10 101)', 1));
because of data(id) 1 and 2 has the same value and distance<10m, so there just will be:
id |val| source | geom
-----+------------+------
3 | 1 | aaa | xxx
4 | 2 | ccc | xxx
Do you know how to query that in PostGIS?
First, I would consider what are the real requirements? E.g. consider points on a line with 8 meter distance: A, B, C and equal value. Do you want that to be reduced to A and C, or B? Both eliminate duplicates within 10 meters, but the result is different. What about A, B, C, D - would you like result to be A, C, or B, D, or A, D, or maybe B, C? Defining specific criteria is not trivial, and sometimes is hard to implement in SQL.
Or maybe you don't care, and just want to reduce point density? Then it is simpler, just compute snapped = ST_SnapToGrid with appropriate grid size, and group by equal values of snapped, value and chose arbitrary point from each group. Note that this does not guarantee there are no close points (points with similar coordinates can snap to different grid cells) but it does reduce most duplicates and it is very cheap computationally.

How do you create a query to find number of entries between a range in Microsoft Access?

I have a table right now that has a list of numbers (some repeating) that goes from 0 to 350.
Is it possible to write a query in Microsoft Access to return how many numbers in the set belong to a range?
For example, if the set of data is 0, 1, 2, 3, 4, 5, 6, 7, I want to create a query to return how many numbers below to the range between 3 and 6 (inclusive). In this example, it would return 4 since the numbers 3, 4, 5 and 6 are in this range.
Thanks.
Have you ever used the 'COUNT' function in a query?
If not, use the query builder to select your number column TWICE from the table.
Add a criteria on your range of 3 to 6.
Set the query to 'Totals' see toolbar), then change your Criteria column to 'WHERE' (in the 'Totals' row),and change the other column to 'COUNT' (in the Totals row)
i.e
SELECT Count(Table1.ID) AS CountOfID
FROM Table1
WHERE (((Table1.ID) Between 1 And 15));

Sorting in MySQL in a specific manner in one column

So I have a data where one of the column (section) contains the following:
A1LB
A1LC
A1RC
A2LB
A2LC
A2RC
B1LB
B1LC
but I want the data to look like this:
A1LC
A1LB
A1RC
A2LC
A2LB
A2RC
B1LC
B1LB
I have tried "ORDER BY CASE WHEN section like %LC" THEN 1 else 2 END"
but it comes out with all of the LCs on top
A1LC
A2LC
B1LC
B2LC
A1LB
A1RC
A2LB
A2RC
B1LB
B1RC
how can I do so without having all of the LC's on top but in the order I want it to be?
Looks like:
ORDER BY substring(section, 1, 3), substring(section, 4, 1) desc
I think you need to make a table mapping {LC -> 1, LB -> 2, RC -> 3}. You can try something like this:
CREATE TABLE map (ky VARCHAR(2), ord TINYINT, PRIMARY KEY(ky,ord))
INSERT INTO map VALUES ('LC', 1), ('LB', 2), ('RC', 3)
SELECT col
FROM table
JOIN map ON RIGHT(col, 2) = map.ky
ORDER BY LEFT(col, 2) ASC, map.ord DESC
You can do the sorting like this
Sort by the first 2 characters
Then list records having the last 2 characters of LC
Then sort by the last 2 characters in general
ORDER BY substring(section, 1, 2),
substring(section, 3, 2) 'LC',
substring(section, 3, 2)