Multiple Select to insert 1 colum with 1 result - mysql

SELECT
C.CompanyId,
CompanyName,
Server,
ServerUsers,
ServerUptime,
ServerHostName,
ServerType
FROM
CUSTOMERS AS C
INNER JOIN
USERS ON C.CompanyId = USERS.CompanyId
WHERE
USERS.UserEmail='matt' AND
USERS.UserPin='5153' AND
(SELECT Status FROM 4321_BlackBerryServices LIMIT 0,1)
LIMIT 0, 8
Currently my table is below
4321 T1 Solutions EXCH-01 392 47 days, 17 min exch01.myCorp.com ExchangeServices
4321 T1 Solutions EXCH-02 685 47 days, 17 min exch02.myCorp.com ExchangeServices
4321 T1 Solutions Lync-01 368 47 days, 17 min lync01.myCorp.com LyncServices
4321 T1 Solutions Lync-02 890 458 days, 58 min lync02.myCorp.com LyncServices
What i would like to do is add the last result from Status FROM 4321_BlackBerryServices so i would want to add the latest status for each server in my table
how is this possible ?

If I understand what you need, try this:
SELECT
C.CompanyId, CompanyName, Server, ServerUsers,
ServerUptime, ServerHostName, ServerType,
(SELECT Status FROM 4321_BlackBerryServices
ORDER BY field_you_know DESC LIMIT 0,1) AS BBS_Status
FROM CUSTOMERS AS C
INNER JOIN USERS
ON C.CompanyId = USERS.CompanyId
AND USERS.UserEmail = 'matt'
AND USERS.UserPin = '5153'
LIMIT 0, 8
In my query field_you_know field is the one you know you can sort the table to take the last one.

Related

MySQL Query trying to (CROSS?) JOIN on one table

Been trying to figure this out for a couple hours and hoping for some expert assistance:
I have a single Mysql table with data such as:
Date version amount
2021-03-01 A 100
2021-03-02 A 35
2021-03-02 B 80
2021-03-03 A 7
2021-03-03 B 90
2021-03-03 C 3
2021-03-03 A 8
2021-03-04 B 15
2021-03-04 C 90
2021-03-04 B 10
And trying to get output for each version for every day, with amount populated as '0' for null;
Result:
Date version SUM(amount)
2021-03-01 A 100
2021-03-01 B 0
2021-03-01 c 0
2021-03-02 A 35
2021-03-02 B 80
2021-03-02 C 0
2021-03-03 A 15
2021-03-03 B 90
2021-03-03 C 3
2021-03-04 A 0
2021-03-04 B 25
2021-03-04 C 90
I tried various 'JOIN', 'LEFT JOIN' and 'CROSS JOIN' permutations without success.
SELECT distinct c1.date, c2.version
FROM crash_log c1
LEFT OUTER JOIN crash_log c2 ON c1.date = c2.date
GROUP BY c1.date, c2.version
(not even messing with the SUM, just trying to get all the rows with this one)
For now, I have a script that does this by brute force: gets DISTINCT date, then get DISTINCT version, then do a nested loop and build an array for each combination. One trouble is it's not scalable and seems the web connection is timing out before the process finishes on a large set.
I'm thinking there's one (semi-?) efficient query that can do this, but I haven't been able to figure it out.
Write subqueries to get all the dates and versions. Cross join these to get every combination.
Then left join that with the table to get either the actual value or default to 0 when NULL.
SELECT d.date, v.version, IFNULL(c.sum, 0) AS sum
FROM (
SELECT DISTINCT date
FROM crash_log) AS d
CROSS JOIN (
SELECT DISTINCT version
FROM crash_log) AS v
LEFT JOIN (
SELECT date, version, SUM(amount) AS sum
FROM crash_log
GROUP BY date, version) AS c ON d.date = c.date AND v.version = c.version
ORDER BY d.date, v.version
Just like your script, but in SQL.
Cross join the distinct dates to the distinct versions and left join to the table and finally aggregation:
SELECT d.Date, v.version, COALESCE(SUM(t.amount), 0) sum_amount
FROM (SELECT DISTINCT Date FROM tablename) d
CROSS JOIN (SELECT DISTINCT version FROM tablename) v
LEFT JOIN tablename t
ON t.Date = d.Date AND t.version = v.version
GROUP BY d.Date, v.version

MYSQL JOIN and SUM - What am I doing wrong? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have exhausted all of my options. I cannot understand where I am going wrong with this. Please help me. I can not take much more headbanging. Here is where I am at...
Below is an explanation of my table structure.
locations_management table
contains data linking the locations_management_id, season_id, location_id and other unneeded information together
orders table
contains data about the order including the location_management_id and the order_id as well as other unneeded information
orders_products table
product data linked to the orders by order_id. This table only uses these columns: order_product_id, order_id, product_id, piece_qty
orders_adjustments table
used to track any adjustments to the inv_shipped. This table uses the order_id, product_id, piece_qty columns
Here is where I am at today. The query below pulls data from the tables above.
Basically I am asking for the location_management_id(s) from the locations_management table WHERE season_id = 12 AND location_id = 35. There can be more than one possible location_management_id that fits both the season_id and location_id. I then need to find the orders that match these location_management_id(s). Once the orders are found, I need to use the order_id(s) to find the products associated to them in the orders_products table.
This query does exactly that but when I take it a step further to combine/SUM the
piece_qty for a total inv_shipped, crazy things happen to the numbers.
SELECT
locations_management.season_id,
locations_management.location_id,
orders.order_id,
orders_products.product_id,
IFNULL((orders_products.piece_qty), 0) AS inv_shipped,
IFNULL((orders_adjustments.piece_qty), 0) AS inv_adjustments
FROM
locations_management
JOIN orders USING (location_management_id)
LEFT JOIN orders_products USING (order_id)
LEFT JOIN orders_adjustments ON (orders_adjustments.order_id = orders_products.order_id) AND (orders_adjustments.product_id = orders_products.product_id)
WHERE
locations_management.season_id = 12 AND locations_management.location_id = 35
GROUP BY
product_id, orders_products.order_id
When I run the query above, this is what I get...
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 220 0
12 35 2194 1 160 0
12 35 2127 3 312 0
12 35 2127 4 24 0
12 35 2127 5 180 0
12 35 2194 5 24 0
12 35 2127 7 144 0
12 35 2127 7 24 0
This is exactly what I would expect to get. Multiple order_id's grouped by the product_id and all the data is accurate. So now here becomes the problem. I want to add/SUM the product_id's together when they match and have a combined inv_shipped. So product_id 1 would now total 380 for inv_shipped.
When I take the same query from above and I add SUM to the inv_shipped and inv_adjustments (as seen below), I get this data output below. Notice how some of the values have doubled but also the matching product_id rows are not combined.
IFNULL(SUM(orders_products.piece_qty), 0) AS inv_shipped,
IFNULL(SUM(orders_adjustments.piece_qty), 0) AS inv_adjustments
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 440 0
12 35 2194 1 160 0
12 35 2127 3 624 0
12 35 2127 4 48 0
12 35 2127 5 360 0
12 35 2194 5 24 0
12 35 2127 7 288 0
12 35 2127 7 24 0
If I change the GROUP BY to product_id only, I get the follow data:
GROUP BY product_id
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 600 0
12 35 2127 3 624 0
12 35 2127 4 48 0
12 35 2127 5 384 0
12 35 2127 7 312 0
Again these inv_shipped totals are not correct. So where am I going wrong?
------------------------------------ Suggestions ------------------------------------
This query below was suggested but the data output for the inv_shipped is not added correctly either.
SELECT
locations_management.season_id,
locations_management.location_id,
orders.order_id,
products.product_id,
products.inv_shipped
FROM
locations_management
JOIN (SELECT location_management_id, order_id FROM orders group by order_id) AS orders ON orders.location_management_id = locations_management.location_management_id
JOIN (SELECT order_id, product_id, IFNULL(SUM(piece_qty), 0) AS inv_shipped FROM orders_products GROUP BY order_id, product_id) AS products ON products.order_id = orders.order_id
WHERE
locations_management.season_id = 12 AND locations_management.location_id = 35
ORDER BY
product_id, order_id
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 440 0
12 35 2194 1 160 0
12 35 2127 3 624 0
12 35 2127 4 48 0
12 35 2127 5 360 0
12 35 2194 5 24 0
12 35 2127 7 288 0
12 35 2127 7 24 0
First, find the root of the issue. What changes in between the correct and incorrect information?
Let's take a look at your second query. From what I can see, there are three things that changes:
You've joined with another sub-query.
You've added a SUM operation.
You've added a GROUP BY.
Nest step is to try with removing SUM and GROUP BY, like this:
SELECT
locations_management.season_id AS season_id,
locations_management.location_id AS location_id,
orders.order_id AS order_id,
products.product_id AS product_id,
products.piece_qty
FROM
locations_management
JOIN (SELECT location_management_id, order_id FROM orders group by order_id) AS orders ON orders.location_management_id = locations_management.location_management_id
JOIN (SELECT order_id, product_id, piece_qty FROM orders_products) AS products ON products.order_id = orders.order_id
WHERE
locations_management.season_id = 12 AND locations_management.location_id = 35
I assume that each product_id will return two (or more) rows. That's probably because of your second JOIN have two (or more) rows for order_id in orders_products table; it seems obvious because the first sub-query for table orders have group by order_id. So, now to quickly fix this, you need to do the SUM inside the second sub-query instead. Something like this:
SELECT
locations_management.season_id AS season_id,
locations_management.location_id AS location_id,
orders.order_id AS order_id,
products.product_id AS product_id,
products.inv_shipped
FROM
locations_management
JOIN (SELECT location_management_id, order_id FROM orders group by order_id) AS orders ON orders.location_management_id = locations_management.location_management_id
JOIN (SELECT order_id, product_id, IFNULL(SUM(products.piece_qty), 0) AS inv_shipped FROM orders_products GROUP BY order_id, product_id) AS products ON products.order_id = orders.order_id
WHERE
locations_management.season_id = 12 AND locations_management.location_id = 35;
This might return you the correct result however I personally will write the query like this:
SELECT lm.season_id, lm.location_id, o.order_id , p.product_id, p.inv_shipped
FROM locations_management AS lm
JOIN (SELECT location_management_id, order_id
FROM orders
GROUP BY location_management_id,order_id) AS o
ON o.location_management_id = lm.location_management_id
JOIN (SELECT order_id, product_id, IFNULL(SUM(products.piece_qty), 0) AS inv_shipped
FROM orders_products
GROUP BY order_id, product_id) AS p
ON p.order_id = o.order_id
WHERE
lm.season_id = 12 AND lm.location_id = 35;
You don't need to set alias if your alias is the same as column name; for example lm.season_id AS season_id. If you remove .. AS season_id, the column will be recognized as season_id nonetheless. You won't see it as lm.season_id.. well at least for most tools that I know of. Also, I personally think aliases are meant to shorten long table or column names but "to each their own".
GROUP BY should include all the non-aggregated column(s) in SELECT. Of course, if the sql_mode=only_full_group_by is turned off, you can run the query but the correct setting should be ON. You can read more of the reason why here.
With the additional columns added in GROUP BY, this query might not return the result you once had. That's depending on you data and if that happen, I suggest you edit your question and add a Minimal, reproducible example. At the moment, we're only seeing queries and no example table/data to work with. Its better if you can create a fiddle with a few rows of data.
I don't see the need for those nested subqueries. And if you want one row per order and product, then aggregation may not be necessary.
You seem to want Doesn't this do what you want?
select lm.season_id, lm.location_id,
op.order_id, op.product_id, op.piece_qty as inv_shipped
from locations_management lm
inner join orders o on o.location_management_id = lm.location_management_id
inner join order_products op on op.order_id = o.order_id
where lm.season_id = 12 and lm.location_id = 35
Or if you want one row per product:
select lm.season_id, lm.location_id,
op.product_id, coalesce(sum(op.piece_qty), 0) as inv_shipped
from locations_management lm
inner join orders o on o.location_management_id = lm.location_management_id
left join order_products op on op.order_id = o.order_id
where lm.season_id = 12 and lm.location_id = 35
group by lm.season_id, lm.location_id, op.product_id

SQL Sum Calculation Confusion

I am new with mysql and working to change a store application to make it have two stock. I created a table to store stock quantity:
Then I plan to create a view with stock quantity, per store, per SKU. I using the following query:
SELECT
`stockList`.`sku`,
SUM(A.`stockQty`) AS 'store1',
SUM(B.`stockQty`) AS 'store2',
SUM(`stockList`.`stockQty`) AS 'total'
FROM `stockList`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=1
) AS A
ON `stockList`.`sku`=A.`sku`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=2
) AS B
ON `stockList`.`sku`=B.`sku`
GROUP BY `stockList`.`sku`
Per resulting table, calculation is not proper and I could not identify the logic:
SKU 43 should show for store1 = 9 and for store2 = 10, total = 19. This is what they show if I execute the select queries alone. Please, let me know if I misunderstood how this sum logic works.
You might to use SUM on subquery to calculate Totle price by sku
LEFT JOIN may make some fields not match causing NULL so use IFNULL to preset value 0
You can try this.
SELECT
T.sku,
SUM(T.stockQty) as totle,
IFNULL(A.`store1`,0) AS `store1`,
IFNULL(B.`store2`,0) AS `store2`
FROM `stockList` AS T
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store1`
FROM `stockList`
WHERE `idStock`=1
GROUP BY sku
) as A ON A.sku = T.sku
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store2`
FROM `stockList`
WHERE `idStock`=2
GROUP BY sku
) AS B ON T.sku =B.sku
GROUP BY T.sku
sqlfiddle
Your query is much more complicated than it needs to be. You can just do this:
SELECT
sku,
SUM(stockQty) as total,
SUM(IF(idStock=1,stockQty,0)) AS `store1`,
SUM(IF(idStock=2,stockQty,0)) AS `store2`
FROM `stockList`
GROUP BY sku
Output:
sku total store1 store2
36 10 10 0
37 3 3 0
38 4 4 0
39 3 3 0
40 10 10 0
41 12 12 0
42 12 12 0
43 19 9 10

Mysql Get Max Number of a group than make new group by that max number

I have a table, it similar with this table
ID Name Age Status
1 John 32 Life
2 Andre 99 Life
3 Anton 89 Dead
4 Maria 99 Life
5 Mario 13 Life
6 Santi 89 Dead
7 Anggy 56 Dead
8 Amir 99 Life
I want to do something like this
1. Group rows by status (Life)
2. Get the max Age from that group (99) (only the max number need)
4. Make new group by age and sort it by ID.
The result will be
8 Amir 99 Life
4 Maria 99 Life
2 Andre 99 Life
Any way to use only 1 line query for that job? with some (php) data procesing its not to hard to get the result i want, but i want to make code as clean as posible, so maybe i can do that 3 step in just a single query?
I think the right logic is:
select t.id, t.name, t.age, t.status
from table t join
(select max(t2.age) from table t2 where t2.status = 'life') m
on t.age = m.age
where t.status = 'life'
order by id desc;
select id, name, age, status
from thetable
where age =
(select max(age)
from thetable
where status="Life"
)
where status="Life"
order by id desc
Use the below mentioned query :
SELECT
*
FROM
t4
CROSS JOIN
(SELECT
MAX(`age`) AS 'age'
FROM
t4
WHERE
`status` = 'Life') AS t5
WHERE
`status` = 'Life' AND t4.`age` = t5.age
ORDER BY `id` DESC;
Check SQLFiddle

MySQL nested, nested subquery not getting outter variable

I have a spendings table and a dates table, that are joined by date_id and id...
What I'm trying to do, is get from 1 query all the info from spendings, plus the sum of all the spendings but with a limit and/or offset
This is the query right now
SELECT spendings.id, spendings.price, spendings.title,
dates.date, users.username, currencies.value,
( SELECT SUM(sum_table.price)
FROM (
SELECT s.price
FROM spendings s, dates d
WHERE s.date_id = d.id
AND day(d.date) = 25
LIMIT 2 OFFSET 0
) as sum_table
) AS sum_price
FROM spendings, dates, users, currencies
WHERE spendings.date_id = dates.id
AND day(dates.date) = 25
AND spendings.user_id = users.id
AND spendings.curr_id = currencies.id
LIMIT 2 OFFSET 0
Output
id price title date username value sum_price
3 6.00 title1 2013-11-25 alex € 21.00
4 15.00 title2 2013-11-25 alex € 21.00
It works, but only if the date here day(d.date) = 25 is the same as the outer one here day(dates.date) = 25
If instead I put day(d.date) = day(dates.date) which seems the logic thing to do, I get #1054 - Unknown column 'dates.date' in 'where clause'
If anyone has an idea to make this simpler let me know :)
Try to join instead of using nested correlated subqueries:
SELECT spendings.id, spendings.price, spendings.title,
dates.date, users.username, currencies.value,
y.sum_price
FROM spendings, dates, users, currencies
JOIN (
SELECT day, SUM(sum_table.price) As sum_price
FROM (
SELECT day(d.date) As day,
s.price
FROM spendings s, dates d
WHERE s.date_id = d.id
AND day(d.date) = 25
LIMIT 2 OFFSET 0
) sum_table
GROUP BY day
) y
ON y.day = day(dates.date)
WHERE spendings.date_id = dates.id
-- AND day(dates.date) = 25 <== commented since it's redundant now
AND spendings.user_id = users.id
AND spendings.curr_id = currencies.id
Some remarks:
Using old join syntax with commas is not recommended: FROM table1,table2,table2 WHERE
The recommended way of expressing joins is "new" ANSI SQL join syntax:
FROM table1
[left|right|cross|[full] outer|natural] JOIN table2 {ON|USING} join_condition1
[left|right|cross|[full] outer|natural] JOIN table3 {ON|USING} join_condition2
....
Actually this "new syntax" is quite old now, since is has been published, as I remember, in 1992 - 22 years ago. In IT industry 22 years is like 22 ages.