Mysql, order by "pattern"? - mysql

I want to order by type, by a pattern. Records now:
type name
1 a
2 b
1 c
4 d
4 e
3 f
2 g
3 h
my pattern is 2,4,3,1 so I would like to get:
2 b
2 g
4 d
4 e
3 f
3 h
1 a
1 c

use order by field (type,2,4,3,1). This will give you the correct result.

Related

MySQL filter out subset by group by

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.
Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

How to read and bind column?

I have many files, but I can not find how to bind column.
For example, files are followed
[1.txt]
ID Score
A 1
B 2
C 3
D 4
[2.txt]
ID Score
A 2
B 2
C 3
D 4
[3.txt]
ID Score
A 4
B 4
C 5
D 3
I want to make
A 1 2 4
B 2 2 4
C 3 3 5
D 4 4 3
You could use cbind() as follows:
df_final <- cbind(cbind(df1, df2["Score"]), df3["Score"])
df_final
ID Score Score Score
1 A 1 2 4
2 B 2 2 4
3 C 3 3 5
4 D 4 4 3
Note that if you were trying to match IDs between data frames which did not coincidentally have the order you want, then you would be asking more for a database style join. In this case, R offers the merge() function from baseR.
Demo

how to get row between two records?

i need to retrieve row between empid=1 to next empid=1?
empid empname
1 a
2 b
3 c
1 d
2 e
4 f
3 g
32 h
1 i
Output like this
empid empname
2 b
3 c

how to join two different datasets in ssrs

I have two different datasets source and destination datsets
Source Dataset
Type A B C D E F G
X 1 2 3 4 5 6 7
Y 2 1 3 5 6 7 8
Z 3 4 5 6 7 8 9
Destination Dataset
Type A B C D E F G
X 0 2 3 6 3 7 9
Y 1 1 5 5 4 8 0
Z 2 3 4 4 5 9 9
Is it possible two create a report in the following format?
Type A B C D E F G
Source X 1 2 3 4 5 6 7
Destin X 0 2 3 6 3 7 9
Source Y 2 1 3 5 6 7 8
Destin Y 1 1 5 5 4 8 0
Source Z 3 4 5 6 7 8 9
Destin Z 2 3 4 4 5 9 9
Handle this in SQL itself with query like this:
SELECT * FROM
(SELECT 'Source' AS myField, Type, A, B, C, D, E, F, G
FROM Table1 T1
UNION ALL
SELECT 'Destination' AS myField, Type, A, B, C, D, E, F, G
FROM Table1 T2 ) A
ORDER BY myField Desc, Type
It will be better way instead of handling it in SSRS.
To solve it in SSRS, you would need to know if the Types in both the datasets are mutually exclusive or not. If there are Types which exists in one but not in other, then you would have to do lot of hardcoding. All changes in the input data you would need to change the report. If the types in both dataset are not mutually exclusive then you might be able to use Lookup functions.
You can use Lookup functionality,
OR instead of doing the join in SSRS it's better to do this is in SQL.

Arranging Data according to their page number in reports

I got this problem regarding the pages the data is supposed to be placed.
This is how they look on the database.
CODE PAGE
---- ----
A 1
A 2
A 1
B 2
B 2
C 3
C 3
C 4
D 4
D 4
D 4
D 3
My desired output is
CODE PAGE
---- ----
A 1
A 1
A 1
B 2
B 2
C 3
C 3
C 3
D 4
D 4
D 4
D 4
How can I do this?
Works in SQL SERVER
select Code,Dense_Rank() Over (order by [Code]) Page from TableName
DEMO
like this:
select CODE,PAGE from <myTABLE>
order by PAGE,CODE