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
Related
I have 3 tables, users, branches and states
TABLE USERS
name code_branch
1 ANA 100
2 BEN 101
3 CARLA 102
4 DAN 100
5 ED 100
TABLE BRANCHES
code_branch branch_name code_state
1 100 BRANCH 100 A
2 101 BRANCH 101 A
3 102 BRANCH 102 C
4 103 BRANCH 103 D
5 104 BRANCH 104 E
6 105 BRANCH 105 F
TABLE STATES
code_state state_name
1 A STATE A
2 B STATE B
3 C STATE C
4 D STATE D
5 E STATE E
and i want the result as per below
state_name total_user
1 STATE A 4
2 STATE B 0
3 STATE C 1
4 STATE D 0
5 STATE E 0
My sql
SELECT s.code_state, COUNT(u.code_branch ) AS total
FROM ((states s
LEFT JOIN branches b ON s.code_state = b.code_state )
LEFT JOIN users u.code_branch = b.code_branch)
GROUP BY s.code_state;
and the result is
STATE TOTAL
A 4
B 0
C 1
D 0
E 0
I Want to show state_name instead of code_state in my query result above.
then i change on the first line like this
SELECT **s.state_name**, COUNT(u.code_branch ) AS total
and like this
SELECT **s.state_name, s.code_state** , COUNT(u.code_branch ) AS total
but get the error
Msg 8120, Level 16, State 1, Line 44
Column 's.state_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Try adding the group by in the select statement?
SELECT **s.state_name, s.code_state** , COUNT(u.code_branch ) AS total
Group by **s.state_name, s.code_state**
So I have the following chart here where I have columns:
a b c d
1 1 1 3
1 1 1 4
1 1 1 5
2 2 2 1
2 2 2 3
2 2 2 3
3 3 3 4
3 3 3 5
3 3 3 6
What I want to do is add and average column d where columns a,b,c contain the same values. How would I go about doing this?
I imagine it would be something like
Select SUM(Table.d) where a = b AND b = c AS e
Try this:
Select SUM(Table.d), AVG(Table.d) from Table Group by Table.a, Table.b, Table.c
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.
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
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.