How to create a simple crosstab query in MySQL - mysql

I have two tables containing fields as below.
Table 1
| SetID | InQty | Day |
| 1 | 10 | 1 |
| 2 | 10 | 2 |
| 3 | 10 | 3 |
Table 2
| SetID | OtQty | Day |
| 1 | 1 | 5 |
| 1 | 2 | 6 |
| 1 | 3 | 7 |
SetID in table 2 is linked with SetId in table 1. Day is placed in place of date, just for convenience only. Expected Output,
| Day | InQty | OtQty |
| 1 | 10 | |
| 5 | | 1 |
| 6 | | 2 |
| 7 | | 3 |
Blank Space can be filled with NULL or Zero.

It appears you are querying ONLY for set ID = 1 otherwise, I would expect to see in/out values for Set 2 and 3. You should be able to get with a simple UNION
select t1.Day, t1.InQty, 0 OutQty
from Table1 t1
where SetID = 1
order by t1.Day
union select t2.Day, 0, t2.OtQty
from Table2 t2
where SetID = 1
Now, if you want totals spanning different "setID"s and keeping them differentiated from each other, just add the setID as a column and also add to the group by clause as well.

Related

Selecting rows that are not defined

How to select rows which are not defined? Like row 2 have undefined day 3 and row 3 have undefined day 1. I want them to be 0 in result set.
+----+-----+-------+
| id | day | count |
+----+-----+-------+
| 1 | 1 | 262 |
| 1 | 2 | 685 |
| 1 | 3 | 984 |
| 2 | 1 | 692 |
| 2 | 2 | 962 |
| 3 | 2 | 355 |
| 3 | 3 | 741 |
+----+-----+-------+
EDIT:
I want select count from days 1, 2 and 3 (not whole table) and display 0 on undefined day.
We can get all unique id values in a Derived Table.
For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.
CROSS JOIN between them to get all possible combinations.
LEFT JOIN from all_combinations table to the main table on id and day.
We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table
Try the following:
SELECT all_combinations.id,
all_combinations.day,
COALESCE(t.count, 0) AS count
FROM
(
SELECT ids.id, days.day
FROM
(SELECT DISTINCT id FROM your_table) AS ids
CROSS JOIN
(SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
) AS all_combinations
LEFT JOIN your_table AS t
ON t.id = all_combinations.id AND
t.day = all_combinations.day
Result:
| id | day | count |
| --- | --- | ----- |
| 1 | 1 | 262 |
| 2 | 1 | 692 |
| 3 | 1 | 0 |
| 1 | 2 | 685 |
| 2 | 2 | 962 |
| 3 | 2 | 355 |
| 1 | 3 | 984 |
| 2 | 3 | 0 |
| 3 | 3 | 741 |
View on DB Fiddle

How to insert a specify row in mysql?

Suppose I have a table t1 like
mysql> select * from t1;
+------+-------+------+
| id | level | gap |
+------+-------+------+
| 1 | 6 | 50 |
| 1 | 5 | 10 |
| 2 | 5 | 12 |
| 2 | 5 | 10 |
| 3 | 8 | 4 |
| 3 | 9 | 1 |
| 3 | 9 | 3 |
| 3 | 7 | 2 |
+------+-------+------+
I want to insert a row (3,6,7) into here.I mean it is below in first 5 row.
Is it possible in mysql?
Just do
INSERT INTO t1 (id, level,gap) VALUES (3,6,7)
Records in a table do not have a prescribed order. The order has to be defined during a SELECT by supplying a suitable ORDER BY clause.
So, if you want the new record to be listed in 5th position use ORDER BY id, level.

MySQL - Recursive Reorder Algorithm / UPDATE with Variable Incrementation

Sorry for the kind of meaningless title, but I couldn't come up with a more fitting one.
I have a MySQL table, which looks like this:
SELECT * FROM `table`
+----+-----------+----------+-------+
| id | dimension | order_by | value |
+----+-----------+----------+-------+
| 1 | 1 | 1 | 1st |
| 2 | 1 | 100 | 3rd |
| 3 | 2 | 300 | 5th |
| 4 | 3 | 999 | 6th |
| 5 | 1 | 2 | 2nd |
| 6 | 2 | 1 | 4th |
+----+-----------+----------+-------+
I am listing all entries ordered by dimension (first) and order_by (second), which looks like this:
SELECT * FROM `table` ORDER BY `dimension`, `order_by`
+----+-----------+----------+-------+
| id | dimension | order_by | value |
+----+-----------+----------+-------+
| 1 | 1 | 1 | 1st |
| 5 | 1 | 2 | 2nd |
| 2 | 1 | 100 | 3rd |
| 6 | 2 | 1 | 4th |
| 3 | 2 | 300 | 5th |
| 4 | 3 | 999 | 6th |
+----+-----------+----------+-------+
Now I'd like to write a function, that rearranges the order_by, if possible with just one update query, to make it look that way:
SELECT * FROM `table` ORDER BY `dimension`, `order_by`
+----+-----------+----------+-------+
| id | dimension | order_by | value |
+----+-----------+----------+-------+
| 1 | 1 | 1 | 1st |
| 5 | 1 | 2 | 2nd |
| 2 | 1 | 3 | 3rd |
| 6 | 2 | 1 | 4th |
| 3 | 2 | 2 | 5th |
| 4 | 3 | 1 | 6th |
+----+-----------+----------+-------+
What I got so far (which, unfortunately, doesn't start recounting for each dimension):
UPDATE `table` AS `l`
JOIN (SELECT #i=1 FROM `table`) AS `i`
SET `order_by` = #i:=i
Now, my question would be: Is it possible to do it with just one UPDATE query?
You have to introduce another variable holding the value of the previous row.
UPDATE Table1 t
INNER JOIN (
SELECT
id, /*your primary key I assume*/
#new_ob:=if(#prev != dimension, 1, #new_ob + 1) as new_ob,
#prev := dimension /*In this line, the value of the current row is assigned. In the previous line, the variable still holds the value of the previous row*/
FROM
Table1
, (SELECT #prev := null, #new_ob := 0) var_init_subquery
ORDER BY dimension, order_by
) st ON t.id = st.id
SET t.order_by = st.new_ob;
see it working live in an sqlfiddle

SQL, difficult fetching data query

Suppose I have such a table:
+-----+---------+-------+
| ID | TIME | DAY |
+-----+---------+-------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 1 | 1 | 2 |
| 2 | 2 | 2 |
| 3 | 3 | 2 |
| 1 | 1 | 3 |
| 2 | 2 | 3 |
| 3 | 3 | 3 |
| 1 | 1 | 4 |
| 2 | 2 | 4 |
| 3 | 3 | 4 |
| 1 | 1 | 5 |
| 2 | 2 | 5 |
| 3 | 3 | 5 |
+-----+---------+-------+
I want to fetch a table which represents 2 IDs which got the largest sum of TIME within the last 3 days (means from 3 to 5 in a DAY column)
So the correct result would be:
+-----+---------+
| ID | SUM |
+-----+---------+
| 3 | 9 |
| 2 | 6 |
+-----+---------+
The original table is much larger and more complex. So i need a generic approach.
Thanks in advance.
And so I just learned that MySQL used LIMIT instead of TOP...
fiddle
CREATE TABLE tbl (ID INT,tm INT,dy INT);
INSERT INTO tbl (id, tm, dy) VALUES
(1,1,1)
,(2,2,1)
,(3,3,1)
,(1,1,2)
,(1,1,1)
SELECT ID
,SUM(SumTimeForDay) SumTimeFromLastThreeDays
FROM (SELECT ID
,SUM(tm) SumTimeForDay
FROM tbl
GROUP BY ID, dy
HAVING dy > MAX(dy) -3) a
GROUP BY id
ORDER BY SUM(SumTimeForDay) DESC
LIMIT 2
select t1.`id`, sum(t1.`time`) as `sum`
from `table` t1
inner join ( select distinct `day` from `table` order by `day` desc limit 3 ) t2
on t2.`da`y = t1.`day`
group by t1.`id`
order by sum(t1.`time`) desc
limit 2

Getting individual values using GROUP BY - MySQL

I have a table called lottery_winners with the following useful colums:
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
id is the unique, primary id for the table. plid refers to the past_lotteries table (i.e I can get all the lottery winners from a specific lottery in the past this way. zbid is the id of the member/user (the winner). amount is the sum of money they won in the lottery, and finally numbers is a VARCHAR CSV field with their lottery numbers.
Here's an example of what rows could be in the table:
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 1 | 1 | 2 | 1 | 1,2,3 |
+----+------+------+--------+---------+
| 2 | 1 | 4 | 5 | 4,5,6 |
+----+------+------+--------+---------+
| 3 | 1 | 3 | 7 | 3,4,5 |
+----+------+------+--------+---------+
| 4 | 1 | 2 | 3 | 7,8,9 |
+----+------+------+--------+---------+
| 5 | 2 | 2 | 8 | 8,9,10 |
+----+------+------+--------+---------+
Now, I want to run a SELECT statement which will bring back all the rows but in a really specific order. The rows should be in grouped by zbid as such (in this case I have added a WHERE plid=1 clause):
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 1 | 1 | 2 | 1 | 1,2,3 |
+----+------+------+--------+---------+
| 4 | 1 | 2 | 3 | 7,8,9 |
+----+------+------+--------+---------+
| 2 | 1 | 4 | 5 | 4,5,6 |
+----+------+------+--------+---------+
| 3 | 1 | 3 | 7 | 3,4,5 |
+----+------+------+--------+---------+
Next criteria is that not only should they be grouped by zbid, but within this grouping they should be ordered by amount DESC. This is what it would now look like:
+----+------+------+--------+---------+
| id | plid | zbid | amount | numbers |
+----+------+------+--------+---------+
| 4 | 1 | 2 | 3 | 7,8,9 |
+----+------+------+--------+---------+
| 1 | 1 | 2 | 1 | 1,2,3 |
+----+------+------+--------+---------+
| 2 | 1 | 4 | 5 | 4,5,6 |
+----+------+------+--------+---------+
| 3 | 1 | 3 | 7 | 3,4,5 |
+----+------+------+--------+---------+
The top two rows have swapped around.
One more criteria. As you can see, although they are grouped by zbid, there's no specific order to them. I want it to group by zbid, but the order should be based on sum(amount) for each group.
The following table shows the totals for each zbid in no specific order (taking into account that plid=1:
+------+-------------+
| zbid | sum(amount) |
+------+-------------+
| 2 | 4 |
+------+-------------+
| 3 | 7 |
+------+-------------+
| 4 | 5 |
+------+-------------+
So using this information the final result using the SELECT statement should be the following (with an added sum(amount) column):
+----+------+------+--------+---------+-------------+
| id | plid | zbid | amount | numbers | sum(amount) |
+----+------+------+--------+---------+-------------+
| 3 | 1 | 3 | 7 | 3,4,5 | 7 |
+----+------+------+--------+---------+-------------+
| 2 | 1 | 4 | 5 | 4,5,6 | 5 |
+----+------+------+--------+---------+-------------+
| 4 | 1 | 2 | 3 | 7,8,9 | 4 |
+----+------+------+--------+---------+-------------+
| 1 | 1 | 2 | 1 | 1,2,3 | 4 |
+----+------+------+--------+---------+-------------+
That's it! Now I've tried a couple of things myself, but I'm not exactly sure how to get the full final result. I have tried:
SELECT id,plid,zbid,amount,numbers,sum(amount) FROM lottery_winners GROUP BY zbid ORDER BY sum(amount) DESC
Now that seemed to meet the final criterion, but it didn't give me individual results for the table.
Please also note that as these results will be paginated, I will need to be adding LIMIT $start,$perpage to the end of the query.
have this a try:
SELECT a.id,
a.plid,
a.zbid,
a.amount,
a.numbers,
c.totalAmount
FROM lottery_winners a
INNER JOIN (
SELECT b.zbid,
SUM(b.amount) totalAmount
FROM lottery_winner b
WHERE b.plid = 1
GROUP BY b.zbid
) c
ON a.zbid = c.zbid
WHERE a.plid = 1
ORDER BY c.totalAmount desc