I have a table like this:
item consumerID userID
A 1 1
B 1 1
C 1 2
D 2 2
E 2 2
F 2 3
G 4 4
H 5 6
I want to get all items where consumerID is not like userID grouped by userID
I am currently doing it programatically in PHP but I wonder if it'd be possible to do this with SQL directly. I am using MariaDB.
Desired output:
item consumerID userID
C 1 2
F 2 3
H 5 6
Are you simply looking for "not equals"?
select t.*
from t
where consumerId <> userId;
Here from this 2 .csv files filtering is done and common emailid's are deleted,I am able to get the total after deletion ,But is there any option that gives how many rows are deleted using pandas.
using mysql :
delete a from data a, data1 b where a.email=b.email; select row_count();
How can this be done using pandas
import pandas as pd
colnames=['id','emailid']
data=pd.read_csv("input.csv",names=colnames,header=None)
colnames=['email']
data1= pd.read_csv("compare.csv",names=colnames,header=None)
emailid_suppress1=data1['email'].str.lower()
suppress_md5=data[~data['emailid'].isin(emailid_suppress1)]
print suppress_md5.count()
I believe need sum of Trues values which are processes like 1:
data = pd.DataFrame({'id':list('abcde'), 'emailid':list('klmno')})
print (data)
id emailid
0 a k
1 b l
2 c m
3 d n
4 e o
data1 = pd.DataFrame({'email':list('ABCKLDEFG')})
print (data1)
email
0 A
1 B
2 C
3 K
4 L
5 D
6 E
7 F
8 G
emailid_suppress1=data1['email'].str.lower()
print ((~data['emailid'].isin(emailid_suppress1)).sum())
3
suppress_md5=data[~data['emailid'].isin(emailid_suppress1)]
print (suppress_md5)
id emailid
2 c m
3 d n
4 e o
EDIT:
print ((data['emailid'].isin(emailid_suppress1)).sum())
2
suppress_md5=data[data['emailid'].isin(emailid_suppress1)]
print (suppress_md5)
id emailid
0 a k
1 b l
Let's say we have this table
No A1 W1 A2 W2
1 R 4 B 1
2 C 5 A 0
3 B 6 R 0
4 A 1 C 4
Then the output should be
A W
C 9
Following query should work:
select t.A1 as A, (t.w1+t1.w2) as W
from t
inner join t as t1
on t.A1 = t1.A2
group by t.A1
order by (t.w1+t1.w2) desc
limit 1
;
Click here for Demo
Note: From the given description, I think the answer should be:
A | W
--------
C | 9
Please correct me if I wrong by commenting on this answer.
Hope it helps!
I have a table of data in MS Access 2007. There are 6 fields per record, thousands of records. I want to make a sort of pivot table like object. That is, if any two rows happens to be the same in the first 4 fields, then they will end up grouped together into one row. The column headers in this pivot table will be the values from the 5th field, and the value in the pivot table will be the 6th field, a dollar amount. Think of the 5th field as letters A, B, C, D, E, F, G. So, the table I start with might have a row with A in the 5th field and $3.48 in the 6th field. Another row may match in the first 4 fields, have B in the 5th field and $8.59 in the 6th field. Another may match in the first 4 fields, have E in the 5th field and $45.20 in the 6th field. I want all these rows to be turned into one row (in a new table) that starts with the first 4 fields where they match, then lists $3.48, $8.59, $0.00, $0.00, $45.20, $0.00, $0.00, corresponding to column headers A, B, C, D, E, F, G (since no records contained C, D, F, G, their corresponding values are $0.00), and then ends with one more field that totals up the money in that row.
Currently, I have some VBA code that does this, written by someone else a few years ago. It is extremely slow and I am hoping for a better way. I asked a previous question (but not very clearly so I was advised to create a new question), where I was asking if there was a better way to do this in VBA. My question asked about reading and writing large amounts of data all at once in Access through VBA, which I know is a good practice in Excel. That is, I was hoping to take my original table and just assign the entire thing to an array all at once (as in Excel, instead of cell by cell), then work with that array in VBA and create some new array and then write that entire array all at once to a new table (instead of record by record, field by field). From the answers in that question, it seems like that is not really a possibility in Access, but my best bet might be to use some sort of query. I tried the Query Wizard and found the Cross Tab query which is close to what I describe above. But, there appears to be a max of 3 fields used in the Row Heading, whereas here I have 4. And, instead of putting $0.00 when a value is not specified (like C, D, F, G in my example above), it just leaves a blank.
Update (in response to Remou's comment to give sample data): Here is some sample data.
ID a b c d e f
7 1 2 3 5 A 5
8 1 2 3 5 B 10
9 1 2 3 5 C 15
10 1 2 3 5 D 20
11 1 2 3 5 E 25
12 1 2 4 4 A 16
13 1 2 4 4 B 26
14 1 3 3 7 D 11
15 1 3 3 7 B 11
The result should be:
a b c d an bn cn dn en Total
1 2 3 5 5 10 15 20 25 75
1 2 4 4 16 26 0 0 0 42
1 3 3 7 0 11 0 11 0 22
But, when I copy and paste the SQL given by Remou, the only output I get is
a b c d an bn cn dn en
1 2 3 5 5 10 15 20 25
This is, I think, what you want, but it would be better to consider database design, because this is a spreadsheet-like solution.
SELECT t0.a,
t0.b,
t0.c,
t0.d,
Iif(Isnull([a1]), 0, [a1]) AS an,
Iif(Isnull([b1]), 0, [b1]) AS bn,
Iif(Isnull([c1]), 0, [c1]) AS cn,
Iif(Isnull([d1]), 0, [d1]) AS dn,
Iif(Isnull([e1]), 0, [e1]) AS en
FROM (((((SELECT DISTINCT t.a,
t.b,
t.c,
t.d
FROM table3 t) AS t0
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS a1
FROM table3 t
WHERE t.e = "A") AS a0
ON ( t0.d = a0.d )
AND ( t0.c = a0.c )
AND ( t0.b = a0.b )
AND ( t0.a = a0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS b1
FROM table3 t
WHERE t.e = "B") AS b0
ON ( t0.d = b0.d )
AND ( t0.c = b0.c )
AND ( t0.b = b0.b )
AND ( t0.a = b0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS c1
FROM table3 t
WHERE t.e = "C") AS c0
ON ( t0.d = c0.d )
AND ( t0.c = c0.c )
AND ( t0.b = c0.b )
AND ( t0.a = c0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS d1
FROM table3 t
WHERE t.e = "D") AS d0
ON ( t0.d = d0.d )
AND ( t0.c = d0.c )
AND ( t0.b = d0.b )
AND ( t0.a = d0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS e1
FROM table3 t
WHERE t.e = "E") AS e0
ON ( t0.d = e0.d )
AND ( t0.c = e0.c )
AND ( t0.b = e0.b )
AND ( t0.a = e0.a );
Table3
ID a b c d e f
1 1 2 3 4 a €10.00
2 1 2 3 4 b €10.00
3 1 2 3 4 c €10.00
4 1 2 3 4 d €10.00
5 1 2 3 4 e €10.00
6 1 2 3 5 a €10.00
7 1 2 3 5 b
8 1 2 3 5 c €10.00
9 1 2 3 5 d €10.00
10 1 2 3 5 e €10.00
Result
There are two rows, because there are only two different sets in the first four columns.
a b c d an bn cn dn en
1 2 3 4 €10.00 €10.00 €10.00 €10.00 €10.00
1 2 3 5 €10.00 €0.00 €10.00 €10.00 €10.00
The way the sql above is supposed to work, is that it selects each of the four definition columns and the currency column from the table where the sort column has a particular sort letter and labels the currency column with the sort letter, each of these sub queries are then assembled, however, you can take a sub query and look at the results. The last one is the part between the parentheses:
INNER JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS e1
FROM table3 t
WHERE t.e = "E") AS e0
For example I have this table:
TableX:
code name consecutive
0 a 0
2 b 1
3 c 2
1 d 3
5 e 0
4 f 1
6 g 2
7 h 0
9 i 1
10 j 2
8 k 3
I want to do this:
code name consecutive
(0,2,3,1) (a,b,c,d,e) (0,1,2,3)
(5,4,6) (e,f,g) (0,1,2)
(7,9,10) (h,i,j) (0,1,2)
(8) (k) (3)
SQL has no concept of "consecutive" in that sense. You'd have to define the grouping in your data model somewhere. There's nothing to say that your table wouldn't actually be stored in a different order like:
code name consecutive
0 a 0
2 b 1
6 g 2
8 k 3
5 e 0
4 f 1
3 c 2
1 d 3
7 h 0
9 i 1
10 j 2
If you want to specify ordering in a table, it can't rely on the "proximity" of other records because the way SQL databases store records is not in a consecutive structure like that at all behind the scenes.