I have a table similar to the following:
+----+------+----------+
| id | Order| SalesMan |
+----+------+----------+
| 1 | 1001 | 1 |
| 2 | 1001 | 2 |
| 3 | 1002 | 1 |
| 4 | 1002 | 4 |
| 5 | 1003 | 2 |
+----+------+----------+
I want to select the the orders sharing multiple Sales Men with an AND operator. Like showing only orders made by Salesmen 1 & 2.
A/c to what i understand about your question, there will be one order with multiple entries of salesman associated. If you just have to know about those orders which salesman 1 and 2 both are associated with then,you can do something like this:
SELECT `order`
FROM tbl
WHERE salesMan=1
AND `order`
IN (select `order` from tbl where salesMan=2)
Here's a SQL Fiddle for better understanding..
EDIT: I have changed the query a/c to your need:
SELECT orders
FROM tbl
WHERE salesMan
IN (1,2,3)
GROUP BY orders
HAVING count(orders)>1;
check the fiddle demo here
Use GROUP BY and HAVING Clause.
Query
SELECT salesMan
FROM tbl
GROUP BY salesMan
HAVING COUNT(salesMan) > 1;
Fiddle Demo
Try this
SELECT * FROM table WHERE SalesMan IN(1,2);
Related
I need to get AVG for every row in SQL for example:
this is the first table
+ ---+------+-------------+
| course_id | course_name |
+ ----------+-------------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | g |
+ ---+------+-------------+
This is the second table
I need to get AVG for both id 1 and 2. the result for example:
+ -------------------+------+----------+
| course_feedback_id | rate |course_id |
+ -================--+------+----------+
| 1 | 4 | 1 |
| 2 | 3 | 1 |
| 3 | 2 | 2 |
+ -------------------+------+----------+
this is the final answer that i need
+ ----------------------+
| course_id | AVG(rate) |
+ -=======--+-----------+
| 1 | 3.5 |
| 2 | 2 |
+ ----------------------+
I tried this soulution but it will give me only the first row not all records.
SELECT *, AVG(`rate`) from secondTable
please help
SELECT `id`, AVG(`rate`) FROM `your_table` GROUP BY `id`
Try this:
SELECT c.course_id, AVG(fb.rate)
FROM course AS c
INNER JOIN course_feedback AS fb ON fb.course_id = c.course_id
GROUP BY c.course_id
Select course_id,t2.rate from table1 where course_id,rate in (Select course_id,avg(rate) as rate from table group by course_id t2)
When you have multiple entries/redundant entries and you want to find some aggregation per each as in this case you got id containing redundant records, In such cases always try to use group by as group by as the name says will group records of the column to which it is applied and if you apply aggregation avg in this case will be groupwise column to which it is being applied not as a whole like for id 1 we have 2 redundant entries so itll apply avg(id1_entries)..likewise as a group.
Im trying to combine distinct and max assign_id or last assign_id in table below but doesnt get a right value.
Table_task
ASSIGN_ID | DRV_ID | VEHICLE_ID
--------------------------------------
1 | EFFA | 1000
2 | SAM | 1001
3 | FIZA | 1004
4 | JIJO | 1000
5 | LISA | 1000
How to get value display as below ?
ASSIGN_ID | DRV_ID | VEHICLE_ID
-----------------------------------------
2 | SAM | 1001
3 | FIZA | 1004
5 | LISA | 1000
Assuming assign_id doesn't have a ties, then you can use subquery :
select t.*
from table t
where assign_id = (select max(t1.assign_id)
from table t1
where t1.vehicle_id = t.vehicle_id
);
Have a sub-query that returns each vehicle_id's max assign_id. JOIN with that result:
select t1.*
from task t1
join (select vehicle_id, max(assign_id) assign_id
from task
group by vehicle_id) t2
on t1.vehicle_id= t2.vehicle_id
and t1.assign_id = t2.assign_id
We are looking to return rows of a query as groups and displaying all entries of the group in the sort order. Randomly based on the set_id... and then in order by the sort_id.
So, randomly it will show:
Carl,
Phil,
Wendy,
Tina,
Rick,
Joe
or
Tina,
Rick,
Joe,
Carl,
Phil,
Wendy
This query is always showing Tina/Rick/Joe first
SELECT * FROM products ORDER BY set_id, rand()
Any help would be appreciated
+---------+--------+-------+----------+
| id | set_id | name | sort_id |
+---------+--------+-------+----------+
| 1 | AA |Rick | 2 |
| 2 | BB |Carl | 1 |
| 3 | AA |Joe | 3 |
| 4 | AA |Tina | 1 |
| 5 | BB |Phil | 2 |
| 6 | BB |Wendy | 3 |
+---------+--------+-------+----------+
if you need a random comma separated name list this will do the trick.
This will keep the groups and the correct sorting within the group.
Query
SELECT
GROUP_CONCAT(Table_names_rand.names) as names
FROM (
SELECT
*
FROM (
SELECT
GROUP_CONCAT(name ORDER BY sort_id) as names
FROM
Table1
GROUP BY
set_id
)
AS Table1_names
ORDER BY
RAND()
)
AS Table_names_rand
Result
| names |
|-------------------------------|
| Carl,Phil,Wendy,Tina,Rick,Joe |
or
| names |
|-------------------------------|
| Tina,Rick,Joe,Carl,Phil,Wendy |
demo http://www.sqlfiddle.com/#!9/487ac9/9
if you need random names as records output.
Query
SELECT
Table1.name
FROM
Table1
CROSS JOIN (
SELECT
GROUP_CONCAT(Table_names_rand.names) as names
FROM (
SELECT
*
FROM (
SELECT
GROUP_CONCAT(name ORDER BY sort_id) as names
FROM
Table1
GROUP BY
set_id
)
AS Table1_names
ORDER BY
RAND()
)
AS Table_names_rand
)
AS Table_names_rand
ORDER BY
FIND_IN_SET(name, Table_names_rand.names)
Result
| name |
|-------|
| Carl |
| Phil |
| Wendy |
| Tina |
| Rick |
| Joe |
or
| name |
|-------|
| Tina |
| Rick |
| Joe |
| Carl |
| Phil |
| Wendy |
demo http://www.sqlfiddle.com/#!9/487ac9/28
If we strip away the randomness of the gorup ordering, your query would look like this:
SELECT
*
FROM
products
ORDER BY
set_id,
sort_id;
The ordering by set_id is necessary to "group" the results, without really grouping them. You do not want to group them, because then the rows with the same group would be aggregated, meaning that only one row per group would be put out.
Since you only want to randomize the groups, you need to write another query that assigns a random number to each group, like the one below:
SELECT
set_id,
RAND() as 'rnd'
FROM
products
GROUP BY
set_id
The GROUP BY clause makes sure, that each group is only selected once. The resultset will look like this:
| set_id | priority |
+--------+---------+
| AA | 0.21 |
| BB | 0.1 |
With that result we can then randomize the output, by combining both queries with a JOIN on the set_id field. This will add the randomly generated number from the second query to the result set of the first query and therefore extend the static set_id with the randomized, but still for all group members equal, rnd:
SELECT
products.*
FROM
products
JOIN (
SELECT
set_id,
RAND() as 'rnd'
FROM
products
GROUP BY
set_id
) as rnd ON rnd.set_id = products.set_id
ORDER BY
rnd.rnd,
products.set_id,
products.sort_id;
Keep in mind, that it is important to still group on products.set_id, because it may be possible that two groups get the same random number assigned. If the result would not be ordered by products.set_id those groups members would then be merged.
I have 2 tables (SALESMAN, SOLD), where the SALES table records what cars were sold each day. At night a job runs that must increase the SOLD count in the SALESMAN table. For example, here are two tables:
SALESMAN SALES
+-------------+-----------+------+ +------------+---------+
| SALESMANID | NAME | SOLD | | SALESMANID | VEHICLE |
| 1 | Bob | 1 | | 1 | GM |
| 2 | Charlie | 7 | | 1 | Chrys |
| 3 | Dave | 0 | | 1 | GM |
+-------------+-----------+------+ | 3 | Dodge |
| 3 | GM |
| 2 | Hummer |
+------------+---------+
After the UPDATE has run, Bob's sold count will increase to 4, Charlie's sold count will increase to 8, and Dave's sold count will increase to 2. I'm trying to create something like:
UPDATE SALESMAN SET SOLD=SOLD+(
SELECT COUNT(*)
FROM SALES
WHERE SALESMAN.SALESMANID = SALES.SALESMANID
)
Is this the right way to solve the problem?
I found a similar question here: (Updating one SQL table based on data in another table) but it's not clear if it will selectively updates table A in their example, or all records in table A.
UPDATE: I fixed the typo above but it still doesn't work. 0 rows affected when I run the query.
yes its right your query just change this
WHERE SALESAN.SALESMANID
to
WHERE SALESMAN.SALESMANID
your demo
i dont know why you didnt try it your self before asking a question.
INSERT INTO SALESMAN (SALESMANID, SOLD) (SELECT SALEMANID, COUNT(*) as c FROM SOLD GROUP BY SALEMANID) ON DUPLICATE KEY UPDATE SOLD = c
You may need to name the select and use .c
If the sales table is deleted after this nightly process runs, then this should work
Update m Set
Sold = sold +
(Select Count(*) From Sales
Where SalesmanId = m.SalesmanId)
From Salesman m
UPDATE SALESMAN a,
(SELECT SALESMANID, COUNT(*) SALE_COUNT
FROM SALES
group by SALESMANID) b
set a.SOLD=a.SOLD+ b.SALE_COUNT
WHERE a.SALESMANID = b.SALESMANID;
see SQL Fiddle
I have a customers table, and an orders table. Each customer can have many orders.
I want to select every customer, along with their earliest order number from the orders table (it is important that I select the earliest order number, not just any order). I want to list customers whether they have an order or not, and I don't want to include customers twice if they have more than one order.
I'm using this:
SELECT *
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id
GROUP BY customers.id
This gives me almost what I want, except it will pick whatever order ID it likes from the table. I need to be able to sort, and pick the smallest order ID.
Any ideas please?
Im pretty sure its something that's staring me in the face...
EDIT: Tables' Structures as requested
Customers:
| ID | Name | Address | Etc |
----------------------------------------
| 1 | Joe | 123 Fake Street | |
| 2 | Mike | 1600 Fake Road | |
| 3 | Bill | Red Square, Moscow | |
----------------------------------------
Orders:
| ID | Customer_ID | Date |
---------------------------
| 1 | 1 | ... |
| 2 | 2 | ... |
| 3 | 2 | ... |
| 4 | 1 | ... |
---------------------------
Create a virtual table (a/k/a subquery) with the lowest numerical order ID for each customer.
SELECT customer_id, min(order_id)
FROM orders
GROUP BY customer_id
Then join that table with the customer table, like so.
SELECT C.customer_id, firstorder.order_id
FROM CUSTOMERS as C
LEFT JOIN (
SELECT customer_id, min(order_id)
FROM orders
GROUP BY customer_id
) AS firstorder ON c.customer_id = firstorder.customer_id
Try this
Select customer.*,Order.OrderNo As EarilerORderNo
From Customers Left Join
(Select customer_id,orderid from orders order by customer_id,orderid desc) As Orders
ON Customers.Id=Orders.OrderID