I have a table called transactions that looks like this:
transactions:
| id | PartNumber | Quantity |
I know that I can use the COUNT property in MySQL, which would give me the the duplicate part numbers in a new column called total_quantity:
SELECT COUNT(transactions.id) AS total_quantity
FROM transactions
GROUP BY transactions.PartNumber
However, now I already have an existing quantity column and want to compute a new count quantity taking into account the previous one as well and updating it in the existing quantity column
What's the most efficient way of doing this?
For example: I want to go from this:
transactions
| id | PartNumber | Quantity |
| 1 | 123 | 1 |
| 2 | 124 | 2 |
| 3 | 125 | 2 |
| 4 | 124 | 2 |
| 5 | 124 | 3 |
| 6 | 126 | 4 |
| 7 | 125 | 1 |
| 8 | 127 | 2 |
To this:
transactions
| id | PartNumber | Quantity |
| 1 | 123 | 1 |
| 2 | 124 | 7 |
| 3 | 125 | 3 |
| 4 | 126 | 4 |
| 5 | 127 | 2 |
You can use this sql request :
SELECT PartNumber, sum(Quantity) as 'SumQuantity' FROM transactions GROUP BY PartNumber
It will gives you sometings like this :
transactions
| PartNumber | SumQuantity |
| 123 | 1 |
| 124 | 7 |
| 125 | 3 |
| 126 | 4 |
| 127 | 2 |
You can find a code sample on SQL Fiddle
Related
Given the sample table of:
+----+----------+---------+------+
| id | userName | storeId | cost |
+----+----------+---------+------+
| 1 | foo | 1 | 10 |
| 2 | bar | 1 | 10 |
| 3 | baz | 5 | 5 |
| 4 | baz | 3 | 20 |
| 5 | qux | 1 | 5 |
| 6 | qux | 4 | 20 |
| 7 | qux | 15 | 30 |
| 8 | qux | 17 | 40 |
| 9 | qux | 3 | 5 |
| 10 | quux | 6 | 20 |
+----+----------+---------+------+
I would like to work out how many people purchased at each store and how many did not. I want the report to display the results grouped by store.
I know the statement select storeId, count(distinct username) as total from purchases group by storeId provides me with how many people purchased in each store, but I want to subtract the result of the query select count(distinct userName) from purchases; in another column. I would expect the sample output to display as follows.
+---------+-----------+--------------+
| storeId | purchased | notPurchased |
+---------+-----------+--------------+
| 1 | 3 | 2 |
| 3 | 2 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 4 |
| 6 | 1 | 4 |
| 15 | 1 | 4 |
| 17 | 1 | 4 |
+---------+-----------+--------------+
You can use NOT condition with IN() function
As long a subse3lect gives back only only one,
you can use following
SELECT
storeId,
COUNT(DISTINCT username) AS total,
((SELECT
COUNT(DISTINCT userName)
FROM
purchases) - COUNT(DISTINCT username)) notPurchased
FROM
purchases
GROUP BY storeId
storeId | total | notPurchased
------: | ----: | -----------:
1 | 3 | 2
3 | 2 | 3
4 | 1 | 4
5 | 1 | 4
6 | 1 | 4
15 | 1 | 4
17 | 1 | 4
db<>fiddle here
I have 4 tables: the first is the client table, which has customer info, and client_id as an auto-increment primary key.
The second and third are identical in structure: they are used to track attendance to 2 different therapy programs. They each have a primary key, and a client_id column to track the client. One of the fields contains units, which I want to sum.
The last table contains the therapists' info.
Basically I want to extract total amount of units for each client from the two attendance tables.
I have tried LEFT JOINS to no avail. I also tried a UNION ALL, but couldn't get it to sum the units.
This is how the tables look:
client:
+---------------------------------------+
| client_id | f_name | l_name | th_id |
|-----------|----------|--------|-------|
| 1 | sherlock | holmes | 1 |
| 2 | john | watson | 4 |
| 3 | hercule | poirot | 3 |
| 4 | jane | marple | 2 |
+---------------------------------------+
therapist:
+--------------------------+
| th_id | f_name | l_name |
|-------|---------|--------|
| 1 | james | kirk |
| 2 | mr | spock |
| 3 | bones | mccoy |
| 4 | nyota | uhura |
+--------------------------+
attendance it:
+-------------------------------+
| it_id | client_id | units |
|-----------|-----------|-------|
| 1 | 1 | 4 |
| 2 | 1 | 4 |
| 3 | 1 | 0 |
| 4 | 1 | 2 |
| 5 | 4 | 0 |
| 6 | 4 | 4 |
| 7 | 4 | 0 |
| 8 | 4 | 2 |
+-------------------------------+
attendance psr:
+-------------------------------+
| it_id | client_id | units |
|-----------|-----------|-------|
| 1 | 1 | 16 |
| 2 | 1 | 16 |
| 3 | 1 | 0 |
| 4 | 1 | 12 |
| 5 | 4 | 0 |
| 6 | 4 | 14 |
| 7 | 4 | 8 |
| 8 | 4 | 10 |
+-------------------------------+
The result should look like this:
+------------------------------------------------------------+
| client_id | total_units_it | total_units_psr | therapist |
|-----------|----------------|-----------------|-------------|
| 1 | 10 | 44 | james kirk |
| 4 | 6 | 32 | mr spock |
+------------------------------------------------------------+
Please excuse the primitive representations, and please don't ask why the tables are designed like that... ;-) Also, I obviously ignored many other fields which are not relevant to the question, such as dates, etc.
Any advice would be appreciated.
Thanks!
You can't use join or you will create Cartesian product and duplicate the rows.
Instead you do a subquery:
SELECT c.*
, (SELECT SUM(units) FROM attendance_it a WHERE a.client_id = c.client_id ) as total_units_it
, (SELECT SUM(units) FROM attendance psr a WHERE a.client_id = c.client_id ) as total_units_psr
, t.*
FROM client c
JOIN therapist t
ON c.th_id = t.th_id
use group by client_id to get the sum of each client. and no need to use join as you have already the ids in column.
I have a table like this and i want to output without duplication of the same user. If i use group by it shows only one record on the same column. iam also using left join for location and user name. A little help
+------+---------+----------+---------+
| user | work id | location | time |
+------+---------+----------+---------+
| 1 | 42 | 1 | 2hr |
| 1 | 42 | 1 | 10min |
| 1 | 42 | 1 | 30min |
| 2 | 42 | 1 | 4hr |
| 2 | 42 | 1 | 2.30min |
| 1 | 50 | 2 | 4min |
| 1 | 50 | 2 | 5min |
| 2 | 20 | 3 | 3hr |
| 1 | 20 | 3 | 6hr |
+------+---------+----------+---------+
Iam looking for this
+------+---------+----------+
| user | work id | location |
+------+---------+----------+
| 1 | 42 | 1 |
| 1 | 50 | 2 |
| 1 | 20 | 3 |
| 2 | 42 | 1 |
| 2 | 20 | 3 |
+------+---------+----------+
You simply need a distinct clause here -
SELECT DISTINCT user
,workid
,location
FROM YOUR_TABLE
ORDER BY user
,location
I have a simple MySQL table as such:
| CUST_ID | VISIT | PROD_ID |
|---------|-------|---------|
| 1 | 1 | 3473 |
| 1 | 2 | 324 |
| 1 | 2 | 324 |
| 2 | 1 | 426 |
| 2 | 2 | 4418 |
| 3 | 1 | 4523 |
| 4 | 1 | 976 |
| 4 | 1 | 86 |
| 4 | 2 | 3140 |
| 4 | 3 | 1013 |
And I would like to transform it to this:
| CUST_ID | VISIT | PROD_IDs |
|---------|-------|----------|
| 1 | 1 | 3473 |
| 1 | 2 | 324, 324 |
| 2 | 1 | 426 |
| 2 | 2 | 4418 |
| 3 | 1 | 4523 |
| 4 | 1 | 976, 86 |
| 4 | 2 | 3140 |
| 4 | 3 | 1013 |
This is kinda an ugly hack, I get it.
I have no idea how to cleanly create such a thing. I've tried a variety of unsuccessful grouping strategies. Even a clue or hint in the right direction would be great. Thanks.
If you're trying to group by cust_id + visit, then you can do that and use a GROUP CONCAT on the PROD_ID field, for example:
SELECT
CUST_ID,
VISIT,
GROUP_CONCAT(PROD_ID) PROD_IDS
FROM
table
GROUP BY
CUST_ID,
VISIT
Reference: GROUP CONCAT
I have a table that has some values in it, along with the time that value was taken against an associated ID from another table.
I am looking to retrieve the latest value for every item in that table, and then order by those latest values.
Here is an SQL fiddle, http://www.sqlfiddle.com/#!2/0be99
And here is text output.
'hist' table
| HIST_ID | HIST_ITEM_ID | HIST_VALUE | HIST_TIME |
|---------|--------------|------------|------------|
| 1 | 1 | 1 | 1420291000 |
| 2 | 1 | 2 | 1420292000 |
| 3 | 1 | 3 | 1420293000 |
| 4 | 1 | 5 | 1420294000 |
| 5 | 1 | 10 | 1420295000 |
| 6 | 1 | 50 | 1420296000 |
| 7 | 1 | 60 | 1420297000 |
| 8 | 1 | 77 | 1420298000 |
| 9 | 1 | 90 | 1420299000 |
| 10 | 1 | 101 | 1420300000 |
| 11 | 2 | 1 | 1420291000 |
| 12 | 2 | 3 | 1420292000 |
| 13 | 2 | 7 | 1420293000 |
| 14 | 2 | 9 | 1420294000 |
| 15 | 2 | 15 | 1420295000 |
| 16 | 2 | 21 | 1420296000 |
| 17 | 2 | 33 | 1420297000 |
| 18 | 2 | 35 | 1420298000 |
| 19 | 2 | 55 | 1420299000 |
| 20 | 2 | 91 | 1420300000 |
'items' table
| ITEM_ID | ITEM_TITLE |
|---------|------------|
| 1 | ABCD |
| 2 | XYZ123 |
So, I can do something like...
select * from hist
inner join items on hist_item_id = item_id
group by hist_item_id
order by hist_value desc
However this returns me a grouping that I cannot order. How can I order this grouping? I had a look at other similar questions on here but was unable to apply their solutions successfully to my query to produce the desire result.
The desired result here would be to return.
HIST_ITEM_ID | ITEM_TITLE | HIST_VALUE |
|------------|------------|------------|
| 1 | ABCD | 101 |
| 2 | XYZ123 | 91 |
You can use a join to get the most recent history item. Then you can join back to the history table and the item table to get additional information:
select h.*, i.item_title
from (select hist_item_id, max(hist_id) as max_hist_id
from hist
group by hist_item_id
) hh join
hist h
on h.hist_id = hh.max_hist_id join
items i
on i.item_id = hh.hist_item_id;
Here is a SQL Fiddle.
You should use MAX function and group by the item id. That would look like this:
SELECT i.item_id, i.item_title, MAX(h.hist_value)
FROM items AS i
INNER JOIN hist AS h
ON i.item_id = h.hist_item_id
GROUP BY i.item_id