Join 2 tables and sum with condition in codeigniter - mysql

I have 2 tables
Table1: customers:
-------------
| id | name |
-------------
| 1 | Mark |
-------------
| 2 | Tom |
-------------
| 3 | John |
Table2: sales:
-----------------------------------
|sid | customerid | price | state |
-----------------------------------
| 10 | 1 | 12000 | 0 |
-----------------------------------
| 11 | 2 | 13500 | 1 |
-----------------------------------
| 12 | 2 | 23000 | 1 |
-----------------------------------
| 13 | 3 | 26000 | 0 |
-----------------------------------
| 14 | 1 | 66000 | 1 |
-----------------------------------
the state column is 0=no dep and 1=dept
I want to list the customers that have DEPT by checking them in the sales table. Now i'm looping the customers and checking them one by one. and it works! but when the number of rows in the customer table grows the page slows down. i want to make this by an SQL query. can anyone help me please ?
the result will be like this:
Mark 66000
Tom 36500

By the Following query, you will get the same output as you want. The joining of tables will be executed on the filtered data using where condition
$this->db->select('customers.name,sum(sales.price)')
->from('customers')
->join('sales','sales.customerid = customers.id','left')
->where('sales.state !=0')
->group_by('customers.name');
->get()->result_array();

You can simply group by customer id in sales table. Code will be like this
return $this->db->select('MAX(customers.name) AS name, SUM(sales.price) as price')->join('sales', 'sales.customerid = customers.id')->where('sales.state', 1)->group_by('customers.id')->get('customers')->result();

Related

Replace ID number with corresponding name

Basically, I have a table which looks like this:
Table: People
------------------------
ID | Employee
------------------------
1 | Saul
2 | Jess
3 | Kenny
And then I have another table that looks like this:
Table: Projects
------------------------
ID | Project | Employee
------------------------
1 | pro1 | 3
2 | pro2 | 1
3 | pro3 | 2
The problem is that I want to replace the numbers in the Employee column of the Projects table with the corresponding names of the employees in the People table.
Thanks in advance.
You don't need to replace the ID Number, you need to build a query to show the results you want.
An example query can be:
SELECT Projects.ID, Projects.Project, People.Employee
FROM Projects
INNER JOIN People on Projects.Employee = People.ID
This will result in:
+----+---------+----------+
| ID | Project | Employee |
+----+---------+----------+
| 1 | pro1 | Kenny |
| 2 | pro2 | Saul |
| 3 | pro3 | Jess |
+----+---------+----------+

Join 3 tables up count the highest user in one table

I'm trying 3 join tables so i can report who has the highest calls for each single customer please see the tables below
+-------+--+------------+
| users | | |
+-------+--+------------+
| id | | first name |
| 1 | | Bill |
| 2 | | Ben |
| 3 | | Bob |
| 4 | | Barry |
+-------+--+------------+
the second table is a call customers table
+-----------+--+-------------------+
| customers | | |
+-----------+--+-------------------+
| id | | Company |
| 1 | | windows company |
| 2 | | glass company |
+-----------+--+-------------------+
the third table is the where to calls are record
+-------------+--+--------+--------------+
| callrecords | | | |
+-------------+--+--------+--------------+
| id | | userid | company id |
| 1 | | 1 | 1 |
| 2 | | 1 | 1 |
| 3 | | 1 | 1 |
| 4 | | 2 | 1 |
| 5 | | 2 | 2 |
| 6 | | 2 | 2 |
+-------------+--+--------+--------------+
So as you can see in the call record table company id 1 which is the windows company has had 4 calls but user 1 made the most so that company i need to display bill company id 2 which is the glass company need to display user id 2 because they made 2 calls in total and not user id 1 because they only made 1 call
so the mysql query i need to make needs to loop round so the report looks like this
windows company - most calls bill
glass company - most calls Ben
If you want a single query that will fetch records. You can check below query. This will give you highest calls for each single customer.
SELECT CR.companyid, C.company, U.first_name, COUNT(CR.userid) AS callCount
FROM callrecords AS CR
INNER JOIN customers AS C ON (C.id= CR.companyid)
INNER JOIN users AS U ON (U.id= CR.userid)
GROUP BY C.id,CR.userid HAVING callCount = (
SELECT count(callrecords.userid) as count
FROM callrecords
WHERE callrecords.companyid = CR.companyid
GROUP BY callrecords.userid
ORDER BY count DESC
LIMIT 1
);
Ignore spells of table or column name because table's or column's name in question is not well formatted.

Selecting rows with more than one occurence of column value from joined table

I have a query which returns a joined table, it looks like the following:
| id_cart | id_customer | date_add |
| ------- | ----------- | --------------------- |
| 1 | 100 | 2017-07-24 | 10:48:00 |
| ------- | ----------- | --------------------- |
| 2 | 101 | 2016-02-14 | 15:43:05 |
| ------- | ----------- | --------------------- |
| 3 | 100 | 2015-04-12 | 01:59:34 |
I'd like to make it so that from these results, only rows with more than one occurrence in the id_customer column are selected - I'm assuming it'd look something like this:
| id_cart | id_customer | date_add | customer_count |
| ------- | ----------- | --------------------- | -------------- |
| 1 | 100 | 2017-07-24 | 10:48:00 | 2 |
| ------- | ----------- | --------------------- | -------------- |
| 3 | 100 | 2015-04-12 | 01:59:34 | 2 |
The issue I'm facing is most questions I've seen from others are looking for the count of multiple value occurrences, whereas I'm only interested in seeing the results - not the count. Also by using a joined table I'm unsure on how to refer back to it as a variable/alias, instead of just copy & pasting a horribly long query over and over again.
For MySQL I would do this:
select *
from (select stuff from bigjoin) a1
inner join
(
select id_customer
from (select stuff from bigjoin)
group by id_customer
having count(*) > 1
) x1
on x1.id_Customer = a1.id_customer

MySql search ranking with criteria

I have table named customers that keeps the customer's data
id | fname | lname
--- | ------ | ------
1 | John | Smith
2 | Mike | Bolton
3 | Liz | John
4 | Mark | Jobs
And i have another table named calls that keeps each call made to each customer.
id | timestamp | customer_id | campaign | answered |
1 |2016-09-05 15:24:08| 1 | 2016-09 | 1 |
2 |2016-09-05 15:20:08| 2 | 2016-09 | 1 |
3 |2016-08-05 15:20:08| 2 | 2016-08 | 1 |
4 |2016-08-05 13:20:08| 3 | 2016-08 | 1 |
5 |2016-08-01 15:20:08| 3 | 2016-08 | 0 |
5 |2016-08-01 12:20:08| 4 | General | 1 |
Campaign General Doesn't count towards the calculations.
I need to get a list of customers ordered by ranking of calling quality based on each customer calling history.
This list is use to call the customers in order that:
Hasn't been called on the actual calling campaign (ex.2016-09)
Has fewer calls
Best % answered (total calls answered / total calls made)
It should look something like this:
| id | fname | lname | %ans | called actual campaign | total calls | rank |
|----|--------|-------|------|------------------------|-------------|------|
| 4 | Mark | Jobs | N/A | no | 0 | 1 |
| 3 | Liz | John | 50 | no | 2 | 2 |
| 1 | John | Smith | 100 | yes | 1 | 3 | No Show
| 2 | Mike | Bolton| 100 | yes | 2 | 4 | No Show
Please help me!
The query which counts for each customer total calls and answered calls for the specified campaign
select
c.id,
count(*) as total_calls,
sum(case when answered=1 then 1 else 0 end) as answered_calls
from customer c
join calls cs on c.id=cs.customer_id
where cs.campaign='2016-09'
group by c.id
Then you can use the query above as a subquery to order
select sub.id, (#rank:=#rank+1) as rank
from (the subquery above) sub, (select #rank:=1)
order by
case when sub.total_calls=0 then 0 else 1,
sub.total_calls,
sub.answered_calls*100/sub.total_calls
You can include any desired columns in the result query

sql query to find users with at least 2 types of accounts

I'm new to relational sql. I'm trying to figure out a query to return the names of customers who have more than one type of account.
customers:
+------------+--------------+
| cid | Name |
+------------+--------------+
| 1 | Bob |
| 2 | John |
| 3 | Jane |
+------------+--------------+
accounts:
+------------+--------------+
| aid | type |
+------------+--------------+
| 1 | Checking |
| 2 | Saving |
| 3 | CD |
+------------+--------------+
transactions:
+------------+--------------+--------------+
| tid | cid | aid |
+------------+--------------+--------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 3 |
| 5 | 3 | 1 |
+------------+--------------+--------------+
With these tables, the query should return Bob and John. I'm having some trouble with how to write such a query. More specifically, how do I keep count of how many accounts a customer has and how do I compare if the accounts are different without adding a new column to the table?
Okay, this seems to work in SQL Fiddle with my test data structure. Try it out with your real data structure and see if it gives you what you're looking for.
SELECT name FROM customers c WHERE EXISTS(
SELECT DISTINCT aid FROM transactions
WHERE cid = c.cid
HAVING COUNT(DISTINCT aid)>1
)