Selecting data from multiple rows into a single row
Quote_ws table
quote_ws_id quote_id gross tax
101 79 98.25 13.0
102 79 91.25 12.5
103 79 94.25 11.0
104 79 92.25 11.5
105 79 96.25 12.0
Section_table
section_id quote_id lsb(quote_ws_id) non_lsb(quote_ws_id)
1 79 101 null
2 79 102 103
3 79 104 105
I am trying to create a MYSQL query that will return gross and tax values from table "Quote_ws" based on lsb and non lsb values in 'Section' table.The results would be:
section_id quote_id gross tax
1 79 98.25 13.0
2 79 91.25 + 94.25 12.5+11.0
(lsb 102,non-lsb 103) (lsb 102,non-lsb 103)
3 79 92.25 + 96.25 11.5+12.0
(lsb 104,non-lsb 105) (lsb 104,non-lsb 105)
I am new to SQL. How can I do that? Please help me ??
SELECT
s.section_id,
s.quote_id,
(IFNULL(q_lbs.gross, 0)+IFNULL(q_non_lbs.gross, 0)) as gross,
(IFNULL(q_lbs.tax, 0)+IFNULL(q_non_lbs.tax, 0)) as tax,
FROM
Section_table s
LEFT JOIN Quote_ws q_lbs
ON q.quote_id = q.quote_id
LEFT JOIN Quote_ws q_non_lbs
ON q.quote_id = q.quote_id;
Related
I am trying to show a starting order table for speed climbing competition, for multiple categories (children). PHP/Mysql. There are two lanes that are climbed simuntaniously, A and B. Each competitor climbes both lanes. The starting order should be in starting number order for lane A and for lane B in the same order as Lane A with a stagger of 50%, rounding down where there is an odd number of starters.
Sample: Let's say I have a category with 9 competitors, starting numbers from 101 to 109. The output starting order table should be like:
Lane A Lane B
101 105
102 106
103 107
104 108
105 109
106 101
107 102
108 103
109 104
Any ideas? Query or few loops or...
Thanks
Edit:
Query to get table in order Lane A then Lane B:
create temporary table laneA
select * from competitors where category = 1 order by stNo;
create temporary table laneB
select * FROM competitors where category = 1 order by (case when stNo > (select avg(stNo) from competitors )then 1 else 2 end), stNo;
select 1 as rank, x.* from laneA as x
union ALL
select 2 as rank, y.* from laneB as y
order by rank;
drop temporary table laneA;
drop temporary table laneB;
Put the data in a zero-based array, loop over it. Calculate what index you need to access the corresponding elements to show in the second column:
$data = range(101, 109); // fake data array, [0 => 101, 1 => 102, ...]
$c = count($data);
for($i=0; $i<$c; ++$i) {
echo $data[$i] . ' - ' . $data[ ($i + $c / 2) % $c ] . "<br>\n";
}
Echo the current element in the first column, and the one with the index set off by half of the count of elements, “clamped down” to the actual index range by modulo of the count again. (With an uneven count, the inner calculation results in a float with .5 at the end, but the modulo operation automatically equalizes that.)
Result:
101 - 105
102 - 106
103 - 107
104 - 108
105 - 109
106 - 101
107 - 102
108 - 103
109 - 104
And if you add one more, so you have an even number of items, it’ll be
101 - 106
102 - 107
103 - 108
104 - 109
105 - 110
106 - 101
107 - 102
108 - 103
109 - 104
110 - 105
This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.
I want find duplicate records in table
table_name: billing
there are 7 cols, but I am intrested in only 3 cols values.
I want find rows, where 3 cols (suite, comment, amount) values are similar
For example:
suite comment amount other col
11 44 0.00 88
11 44 0.00 33
17 48 1.00 11
17 48 1.00 35
17 48 1.00 21
Not particularly efficient, but if you don't need to do this quickly.
Select b.* from
(SELECT
Suite, comment, amount
FROM
YourTable
GROUP BY
Suite, comment, amount
Having count(1) > 1) a
Inner join
YourTable b
On a.suite = b.suite and a.comment = b.comment and a.amount = b.amount
I want to get the yearly max values and the record id information for further joins with other tables.
Consider the following table:
tur_id Datum SZ Art VW StV TV NSP
189 23.06.2010 09:40:00 S 1 -37 -35 46
7 11.05.2012 08:40:00 S 1 -19,9 -21 45
140 02.07.2011 10:30:00 S 1 -25 -26 45
62 31.07.2013 31.07.2013 S 1 -16 -16 42
136 12.07.2011 11:20:00 S 1 -21,4 -23 41
181 04.08.2010 10:00:00 S 1 -30,1 -28 41
195 24.10.2009 09:40:00 S 1 -45 -47 41
90 22.10.2013 22.10.2013 S 1 -14,2 -16 40
11 16.06.2012 10:50:00 S 1 -17 -18 40
153 13.05.2011 09:25:00 S 1 -27,4 -29 40
1 23.07.2014 23.07.2014 S 1 -13,6 -14 39
56 15.06.2013 15.06.2013 S 1 -17,3 -18 39
45 26.10.2012 26.10.2012 S 1 -17,4 -18 39
.....
The following query returns the yearly max values without record id (in my case turid).
SELECT year(datum) rok, max(nsp) FROM turniere GROUP BY year(datum)
Result:
rok max(nsp)
2009 41
2010 46
2011 45
2012 45
2013 42
2014 39
How can I get the info of the turid or the datum value?
You are half-way there. Join the original data back in:
SELECT t.*
FROM turniere t JOIN
(SELECT year(datum) as rok, max(nsp) as maxnsp
FROM turniere
GROUP BY year(datum)
) tt
ON year(t.datum) = tt.rok and t.nsp = tt.maxnsp;
Afternoon,
I have a couple of tables in mysql
The first holds the ticket information and status of ticket
The second holds the items and costs for each item.
Each ticket can have multiple items which are stored into the items table.
example table 1
Ticket id Manufacturer status
102 man-A 10
103 man-A 20
104 man-A 10
105 man-C 10
106 man-B 20
example table 2
Ticket id Item Price
102 item_a 100.00
103 item_a 100.00
103 item_b 110.00
103 item_c 115.00
104 item_c 115.00
105 item_b 110.00
106 item_a 100.00
106 item_c 115.00
now on a quick stats page i need to show.
Manufacturer Qty(status 10) Qty(status 20) Value
man-A 2 1 530.00
man-B 0 1 225.00
man-C 1 0 110.00
If there are no tickets with status 10 or status 20 i do not need to show that manufacturer.
I would like to do 1 mysql request to get the stats i need to show.
Many Thanks
Try this with join and using SUM()
SELECT
t1.Manufacturer,
SUM(t1.status =10) `Qty(status 10)`,
SUM(t1.status =20) `Qty(status 20)`,
SUM(t2.price) `Value`
FROM table1 t1
JOIN table2 t2 ON (t1.`Ticket id` =t1.`Ticket id`)
GROUP BY t1.Manufacturer
Fiddle Demo