I was wondering if it's possible to do an index and match query but for SQL, and I'm not quite sure how to simply google it myself.
But an example would be two tables like so
Date ItemNumber Location Code
x 1 MyHouse 90
y 2 YourHouse 100
z 3 OurHouse 200
and
column integers represent itemnumbers
Code 1 2 3 4 5 6 7 8 9
90 MyHouse MyHouse MyHouse MyHouse OurHouse
100 Ourhouse OurHouse YourHouse YourHouse
200 MyHouse OurHouse YourHouse MyHouse YourHouse
After executing query, it should look something like
Date ItemNumber Location Code PerfectDistance
x 1 MyHouse 90 MyHouse
y 2 YourHouse 100 MyHouse
z 3 OurHouse 200 YourHouse
or something to that affect, where you index and match on the code and item number like you would in excel.
Not looking for full solution, just a function idea.
JOIN is what you're looking for.
Similar to Excel: index(return_value, match(lookup_item, lookup_range, 0)) the 0 represents an exact match. You need an exact match for the join to work
You must join the tables and use the function choose():
select t1.*,
choose(t1.itemnumber, t2.[1], t2.[2], t2.[3], t2.[4], t2.[5], t2.[6], t2.[7], t2.[8], t2.[9]) as PerfectDistance
from table1 as t1 inner join table2 as t2
on t2.Code = t1.Code
For this table1:
Date ItemNumber Location Code
x 1 MyHouse 90
y 2 YourHouse 100
z 3 OurHouse 200
and this table2:
Code 1 2 3 4 5 6 7 8 9
90 MyHouse MyHouse MyHouse MyHouse OurHouse
100 OurHouse OurHouse YourHouse
200 MyHouse OurHouse YourHouse MyHouse YourHouse
the results are:
Date ItemNumber Location Code PerfectDistance
x 1 MyHouse 90 MyHouse
y 2 YourHouse 100 OurHouse
z 3 OurHouse 200 YourHouse
Related
I have a table below named deposit
dep_id
deposit_amount
comp_id
1
100
1
2
100
1
3
300
2
4
200
2
5
100
2
6
500
3
When I update the table with the query below I get the following table, which is not what I want
UPDATE deposit
SET deposit_amount = (SELECT SUM(deposit_amount) - 50)
WHERE comp_id =1
What the query above does is to subtract 50 from each of the corresponding comp_id
dep_id
deposit_amount
comp_id
1
50
1
2
50
1
3
300
2
4
200
2
5
100
2
6
509
3
But the table below is what I need.
Because seeing the first table and with the query I provided where comp_id =1, we have 100 + 100 = 200, and then 200 - 50 = 150. So because comp_id has 1 IDs two times, therefore we have 75 and 75 because 75 +75 is 150. So we have the table below, which is what I need.
dep_id
deposit_amount
comp_id
1
75
1
2
75
1
3
300
2
4
200
2
5
100
2
6
500
3
The amount supposed to be evenly split amongst the deposits that share a comp_id, even if they weren't before.
Please how do I write the query to suit the table I need? Help!
You can divide the 50 by the count of the records for this id:
UPDATE deposit d1,
(SELECT *, count(*) over (partition by d2.comp_id) as c FROM deposit d2) x
SET d1.deposit_amount = (SELECT SUM(d1.deposit_amount) - 50/x.c)
WHERE d1.comp_id =1
see: DBFIDDLE
What I'm trying to do is to select a specific amount of tickets (max 2) and every person that has the sum of the number of tickets less of 3 and the valid field has to be != 'e'
I have this table:
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
4
330
1
e
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
9
443
1
s
10
444
1
s
11
445
2
s
Here is what I did:
SELECT m.id, m.id_person, m.nr_tickets, m.valid
FROM table m
JOIN table m1 ON m1.id <= m.id
WHERE m.nr_tickets > 0
GROUP BY m.id
HAVING SUM(case when m.valid != 'e' then m1.nr_tickets end) <= 10
This query gives me
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
As you can see the query it's almost right, the thing is that the person 220 in the results has the sum of the tickets is greater than 2.
What I'm trying to achieve is to bypass the ID 6, and to have instead the ID 9
select `id`,`id_person`, sum(`nr_tickets`) as `nr_tickets`, `valid`
from `test`
group by `id_person`
having sum(`nr_tickets`) < 3 and `valid`!="e"
Output:
id id_person nr_tickets valid
5 331 1 s
7 441 1 s
8 442 2 s
9 443 1 s
10 444 1 s
11 445 2 s
Table_1 has order_id, country_id details
table_ID order_id country_id
1 100 IN
2 200 USA
3 300 UK
4 400 IN
5 500 UK
6 600 UK
7 700 USA
8 800 USA
9 900 IN
10 1000 UK
Table_2 has shipment_id, order_id details
Shipment_ID order_id
1 100
2 100
3 100
4 200
5 200
6 300
7 300
8 400
9 500
11 500
12 600
13 700
14 700
15 700
16 700
17 800
18 800
19 800
20 900
21 900
22 1000
23 1000
24 1000
I used the following query to find out list of order_id which are for country_id='IN'
select `order_id`
from `Table_1`
where `country_id` = 'IN';
order_id
100
400
900
I need guidance to write the query to find the count of shipment_id which will are mapped to order_id from 'IN'
So order_id 100 has 3 shipment, 400 has 1 and 900 has 2 shipment
Desired final output
count_of_shipment_id
6
here is the query you need:
SELECT country_id, count(*) as count_of_shipment_id
FROM Table_1 a
inner join Table_2 b on a.`order_id` = b.`order_id`
group by country_id
if you need only one country you can always add "where" or "having" to filter the result.
here you can see the sample you posted:
http://sqlfiddle.com/#!9/c90424/2
I've got a table with repeated X and Y couples:
------------------------
ID | X | Y
------------------------
1 10 20
2 20 10
3 10 20
4 30 20
5 20 10
6 20 10
I would like to count the frequency of the same (X,Y) couples like this:
--------------------------
X | Y | COUNT
--------------------------
20 10 3
10 20 2
30 20 1
This is what I tried to do:
SELECT X,
Y,
COUNT(DISTINCT X, Y) AS FREQUENCY
FROM `ordini`
GROUP BY X, Y
ORDER BY `FREQUENCY` DESC
But the result is not what I expected: FREQUENCY returned is 1 for all the couples.
Where am I wrong?
Don't use this COUNT(DISTINCT X, Y) as DISTINCT removes all the same records and you are getting single value for same X,Y that's why you are getting 1
SELECT X,
Y,
COUNT(*) AS FREQUENCY
FROM ordini
GROUP BY X, Y
ORDER BY FREQUENCY DESC
Live Demo
http://sqlfiddle.com/#!9/beeff/2
Given a table like
id x y
-- --- ---
5 200 1
5 3000 2
5 224 3
6 135 1
6 2222 2
6 16 3
I can get all the x values where y = 1 with
SELECT x as y1
WHERE y = 1
and I can get all the x values where y = 2 with
SELECT x as y2
WHERE y = 2
but I would like to select these in one query so I get a result like
id y1 y2
-- --- ---
5 200 3000
6 135 2222
Can this be done in one query?
I've tried pretty much every variation I can think but all errors.
MySQL does not have a PIVOT function just like SQL Server. But still you can simulate it using MAX() and CASE() to test its value with in a row.
SELECT ID,
MAX(CASE WHEN y = 1 THEN x END) y1,
MAX(CASE WHEN y = 2 THEN x END) y2
FROM TableName
GROUP BY ID
SQLFiddle Demo
OUTPUT
╔════╦═════╦══════╗
║ ID ║ Y1 ║ Y2 ║
╠════╬═════╬══════╣
║ 5 ║ 200 ║ 3000 ║
║ 6 ║ 135 ║ 2222 ║
╚════╩═════╩══════╝