My table looks like this:
id city road_id
-------------------------
1 london 3
2 manchester 3
3 newcastle 3
4 glasgow 3
5 london 5
6 newcastle 5
I know values of two cities and road_id and need something like this:
UPDATE table SET anothercolumn=1 WHERE id>=(id for)london AND id<(id for)glasgow AND road_id=3
to affect only these rows:
1 london 3
2 manchester 3
3 newcastle 3
UPDATE your_table
SET anothercolumn = 1
WHERE id >= (select id from your_table where city = 'london')
AND id < (select id from your_table where city = 'glasgow')
AND road_id = 3
Related
From my SQL query, I get this output:
city
Street
housenumber
flatnumber
inhabitans
ACity
AStreet
1
1
2
ACity
AStreet
1
2
1
ACity
AStreet
1
3
5
ACity
AStreet
1
SUMFLAT
8
ACity
AStreet
2
1
2
ACity
AStreet
2
2
1
ACity
AStreet
2
3
5
ACity
AStreet
2
SUMFLAT
8
ACity
AStreet
SUMHOUSE
16
ACity
BStreet
...
What I want is to remove the double entry in each row, so that it looks like this:
city
Street
housenumber
flatnumber
inhabitans
ACity
AStreet
1
1
2
2
1
3
5
SUMFLAT
8
2
1
2
2
1
3
5
SUMFLAT
8
SUMHOUSE
16
BStreet
...
This is my current code:
SELECT
city, Street, housenumber, flatnumber, inhabitans
FROM
db
UNION
SELECT
city, Street, housenumber, 'SUMFLAT', SUM(inhabitans)
FROM
db
GROUP BY
city, street, housenumber
UNION
SELECT
city, Street, 'SUMHOUSE', '', SUM(inhabitans)
FROM
db
GROUP BY
city, street
ORDER BY
city, Street, housenumber, flatnumber;
My questions are:
is this possible (without changing the order of the lines of the result table)
if possible in SQL : how?
You should not use that many unions in the query, instead
simply get all data from database in to your page and using loop and conditions you can hide duplicate data also you can use html table rowspan for the same.
I have table with these data:
Id City Amount
1 London 25000
2 New York 20000
3 London 23000
4 Paris 22000
5 Moscow 18000
6 London 21000
7 New York 19000
8 Moscow 26000
9 London 24000
10 Moscow 16000
11 London 15000
12 Moscow 23000
13 Paris 19000
14 New York 15000
15 London 26000
I must create SQL as what to get the results as this?
Id City Amount
1 London 25000
2 New York 20000
3 London 23000
4 Paris 22000
5 Moscow 18000
7 New York 19000
8 Moscow 26000
13 Paris 19000
That means I just want to get the maximum of one city only to appear 2 times.
I just want to get the first two records or the last two records
Thanks!
If your MySQL version < 8.0, you can simulate RowNumber functionality, using Session variables. To achieve Partition By functionality based on grouping, we will use two session variables, one for the row number and the other for storing the old City to compare it with the current one, and increment it by 1, if belonging to same City group, else reset to 1.
Following code will get your first two records. You can easily change it to get first n records, by changing 2 to n in the query.
SET #row_number = 0;
SET #city_var = '';
SELECT inner_nest.Id,
inner_nest.City,
inner_nest.Amount
FROM (
SELECT
#row_number:=CASE
WHEN #city_var = City THEN #row_number + 1
ELSE 1
END AS num,
Id,
#city_var:=City as City,
Amount
FROM
table_name
ORDER BY
City) AS inner_nest
WHERE inner_nest.num <= 2
ORDER BY inner_nest.Id ASC
**
SQL Fiddle
**
I have 2 tables "quizzes" & "users", I need to return list of first user took each quiz, with quiz_id:
tables structure
"quizzes" structure:
id name
1 England
2 france
3 Japan
4 USA
5 UAE
6 Sweden
7 Italy
8 Brazil
9 South Korea
10 India
"users" structure:
id user_id quiz_id
1 1 1
2 1 2
3 2 1
4 3 4
5 1 4
6 5 9
7 2 9
8 3 8
9 3 9
10 3 7
I need to run query to return first "user_id" took each "quiz", (order by users.id ASC)
expected results:
quiz_id user_id
1 1
2 1
4 3
7 3
8 3
9 5
thanks,
You first group by quiz and pick minimal id and then select based on those ids:
select quiz_id, user_id
from users
where id in(select min(id) from users group by quiz_id)
Select quiz_id, user_id
from users
Where (quiz_id, id) in
(
Select quiz_id, min(id) id
From users
Group by quiz_id
)
Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.
This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.
I have table with data as follows:
Store ItemNo Type Billno Qty
London 1 A 1 10
London 1 A 2 5
London 1 S 1 7
London 1 A 3 5
London 1 S 2 7
London 2 A 1 19
London 2 S 2 5
London 2 A 3 11
Paris 1 A 1 15
Paris 1 S 2 8
Paris 1 A 3 9
Paris 2 A 1 10
Paris 2 S 2 5
Now i want to calculate TotalQty, such that totalqty of qty of an itemno under particular store is calculated based on type. i.e. if type is A, qty should be added to total and if it is S , subtracted from total as shown below. In the example below for store london ,itemno 1 row 3 is the last entry,so the totalQty gives the current quantity availble for that item in that particular store.
Store ItemNo Type BillNo Qty TotalQty
London 1 A 1 10 10
London 1 A 2 5 15
London 1 S 1 7 8
London 1 A 3 5 13
London 1 S 2 7 6
London 2 A 1 19 19
London 2 A 2 5 24
London 2 S 3 11 13
Paris 1 A 1 15 15
Paris 1 S 2 8 7
Paris 1 A 3 9 16
Paris 2 A 1 10 10
Paris 2 S 2 5 5
Assuming I understand you right, you need to use the IIf statement:
SELECT Store, ItemNo, SUM(IIF(Type = 'A', Qty, 0 - Qty)) AS TotalQty
FROM MyTable
GROUP BY Store, ItemNo
should produce the following (untested):
Store ItemNo TotalQty
London 1 8
London 2 13
Paris 1 16
Paris 2 5
I think this is it (I was confused but then spotted your data is inconsistent between table and results):
SELECT S1.Store, S1.ItemNo, S1.Type, S1.Billno, S1.Qty,
(
SELECT SUM(SWITCH(
S2.Type = 'A', S2.Qty,
S2.Type = 'S', 0 - S2.Qty,
TRUE, NULL
))
FROM StoreStuff AS S2
WHERE S2.Store = S1.Store
AND S2.ItemNo = S1.ItemNo
AND S2.Billno <= S1.Billno
) AS TotalQty
FROM StoreStuff AS S1
ORDER
BY S1.Store, S1.ItemNo, S1.Billno;