Sales SUM per country, state, city and district from five Mysql tables - php

I have the five below tables, I want to show the sales SUM per country, state, city and district by using one mysql query if possible:
note: If there is no sales in the state, city, district, then I need the query result to show 0 or empty space (the state, city, district name has to be shown even if there is no sales for that specific state, city or district)
+------------+----------+
| country |
+------------+----------+
| country_id | country |
+------------+----------+
| 1 | country1 |
| 2 | country2 |
+------------+----------+
+----------------+--------+------------+
| state_province |
+----------------+--------+------------+
| state_id | state | country_id |
+----------------+--------+------------+
| 1 | state1 | 1 |
| 2 | state1 | 2 |
| 3 | state2 | 2 |
| 4 | state2 | 1 |
+----------------+--------+------------+
+---------+-------+----------+
| city |
+---------+-------+----------+
| city_id | city | state_id |
+---------+-------+----------+
| 1 | city1 | 1 |
| 2 | city2 | 1 |
| 3 | city1 | 3 |
| 4 | city2 | 3 |
| 5 | city1 | 4 |
| 6 | city2 | 4 |
+---------+-------+----------+
please note that state (state_id =2) in country (country_id = 2) has no cities in the above table.
+-------------+-----------+---------+
| district |
+-------------+-----------+---------+
| district_id | district | city_id |
+-------------+-----------+---------+
| 1 | district1 | 1 |
| 2 | district2 | 1 |
| 3 | district1 | 2 |
| 4 | district2 | 2 |
| 5 | district1 | 4 |
| 6 | district2 | 4 |
| 7 | district1 | 5 |
| 8 | district1 | 6 |
+-------------+-----------+---------+
+----------+------------+----------+---------+-------------+--------+
| sales |
+----------+------------+----------+---------+-------------+--------+
| sales_id | country_id | state_id | city_id | district_id | amount |
+----------+------------+----------+---------+-------------+--------+
| 1 | 1 | 0 | 0 | 0 | 1000 |
| 2 | 1 | 0 | 0 | 0 | 2000 |
| 3 | 1 | 1 | 0 | 0 | 300 |
| 4 | 1 | 1 | 0 | 0 | 70 |
| 5 | 1 | 1 | 1 | 0 | 50 |
| 6 | 1 | 1 | 1 | 1 | 25 |
| 7 | 1 | 4 | 1 | 1 | 25 |
| 8 | 1 | 4 | 5 | 0 | 25 |
| 9 | 2 | 0 | 0 | 0 | 3000 |
| 10 | 2 | 0 | 0 | 0 | 500 |
| 11 | 2 | 3 | 0 | 0 | 300 |
| 12 | 2 | 3 | 4 | 6 | 70 |
+----------+------------+----------+---------+-------------+--------+
Demo with my current attempt
Thank you

If you want to group by three column (city_id, state_id, district_id), it's pointless, since it won't change anything in your sales table. There you have already partitoned information.
Moreover, in sales table you have 0 in place of some IDs and there isn't any ID equal to 0 in any table. You need to rethink what you really want I guess.
I think you just need simple JOIN:
SELECT country,
state,
city,
district,
coalesce(amount, 0) amount
FROM country ctr
JOIN state_province st ON ctr.country_id = st.country_id
JOIN city c ON c.state_id = st.state_id
LEFT JOIN district d ON d.city_id = c.city_id
LEFT JOIN sales s ON
s.country_id = ctr.country_id AND
s.state_id = st.state_id AND
s.city_id = c.city_id AND
s.district_id = d.district_id
Demo

Related

Loop through every value of string

I get form db field (organisations.paths)the following strings:
/1/2/3/4
Each number is the id of organisation's name from this db table:
+----+-------------+----------+----------+----------------------+
| id | frameworkid | path | parentid | fullname |
+----+-------------+----------+----------+----------------------+
| 1 | 1 | /1 | 0 | NYC University |
| 2 | 1 | /2 | 0 | Board of directors |
| 3 | 1 | /1/2/3 | 1 | Math faculty |
| 4 | 1 | /1/2/3/4 | 3 | Statistic department |
| 5 | 1 | /1/2/3/5 | 2 | Linguist department |
+----+-------------+----------+----------+----------------------+
Then I have the description table for each organisation:
+----+----------+---------+----------------+
| id | data | fieldid | organisationid |
+----+----------+---------+----------------+
| 1 | HQ | 1 | 1 |
| 2 | advisory | 1 | 2 |
| 3 | advisory | 1 | 3 |
| 4 | bottom | 1 | 4 |
| 5 | advisory | 1 | 5 |
+----+----------+---------+----------------+
How to join the both description table and main table and loop only through organisations, which have HQ or advisory in their description? So it becomes:
NYC University, Board of directors, Math faculty (Statistic department-won't be shown, as it is with description bottom)
You need to use explode, IN and join Function of PHP and mysql.
$var = "/1/2/3/4";
$in = join(" , ", explode("/", ltrim($var, '/')));
$sql = "SELECT `dt`.`fullname` FROM `db_table` dt
LEFT JOIN `organization` o
ON `o`.`organisationid` = `dt`.`id`
WHERE `o`.`id` IN ($in) AND (`o`.`data` = 'HQ' OR `o`.`data` = 'advisory')";
Make a loop to get the names and show them as you want.

MySQL get Related and Unrelated Records from Table in One Query

I have a user and pages table, and these two have many to many relationship between them with the bridge table user privileges. What I want to get all the pages name and only those are marked that is assigned to that user.
want some think like this
+---------+----------------------------------+
| user_id | page_name | Assigned|
+---------+----------------------------------+
| 1 | Add Project | 0 |
| 1 | Department | 0 |
| 1 | Category | 1 |
| 1 | Item | 0 |
| 1 | Units | 1 |
| 1 | Stock In | 0 |
| 1 | Stock Card Report | 1 |
+---------+------------------------+---------|
for now my query is this;
Select up.user_id, p.page_name FROM user_privileges up, pages p where p.page_id = up.page_id and up.user_id = 1;
and it returns this;
+---------+------------------------+
| user_id | page_name |
+---------+------------------------+
| 1 | Add Project |
| 1 | Department |
| 1 | Category |
| 1 | Item |
| 1 | Units |
| 1 | Stock In |
| 1 | Stock Card Report |
+---------+------------------------+
The Scheme is this;
table - user
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 4 | saif |
| 1 | admin |
| 5 | taqi |
| 2 | rashid |
+---------+-----------+
table - pages
+---------+---------------+
| page_id | page_name |
+---------+---------------+
| 2 | Page 1 |
| 3 | Page 2 |
| 5 | Page 3 |
| 6 | Page 4 |
| 7 | Page 5 |
| 8 | Page 6 |
| 9 | Page 7 |
| 10 | Page 8 |
| 11 | Page 9 |
| 13 | Page 10 |
| 14 | Page 11 |
| 15 | Page 12 |
| 16 | Page 13 |
| 18 | Page 14 |
| 19 | Page 15 |
| 20 | Page 16 |
+---------+---------------+
and table user_privalges for user_id = 1 only.
+--------------------+---------+---------+
| user_privileges_id | user_id | page_id |
+--------------------+---------+---------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 5 |
| 4 | 1 | 6 |
| 5 | 1 | 7 |
| 6 | 1 | 8 |
| 7 | 1 | 9 |
| 8 | 1 | 10 |
| 9 | 1 | 11 |
| 10 | 1 | 13 |
| 11 | 1 | 14 |
| 12 | 1 | 15 |
| 13 | 1 | 16 |
| 14 | 1 | 18 |
| 15 | 1 | 19 |
| 16 | 1 | 20 |
+--------------------+---------+---------+
Select up.user_id, p.page_name FROM user_privileges up, pages p where p.page_id = up.page_id and up.user_id = 1 AND up.Assigned=1;
"What I want to get all the pages name and only those are marked that is assigned to that user." So with your edit you should try something like that with case statement
SELECT up.user_id, p.page_name,
CASE
WHEN up.page_id=p.page_id THEN '1'
ELSE '0'
END AS Assigned
FROM pages p left join user_privileges up
ON p.page_id = up.page_id
WHERE up.user_id = 1;
If you are only looking for the user_id = 1 you may do something as
select
1 as user_id,
p.page_name,
case when up.page_id is not null then 1 else 0 end as `Assigned`
from pages p
left join user_privalges up on up.page_id = p.page_id and up.user_id = 1
order by p.page_id
http://www.sqlfiddle.com/#!9/06f65/5

Concatenate and Count the multiple rows in single rows in mysql

I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values ​​and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |

SELECT records WHERE rows have difference on a specific column

I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories