MYSQL Union Operation with Gross, Cut & Net Amounts - mysql

I need to print Gross allocation, Cut Allocation & Net Allocation issued for various offices under relevant votes. I used the following tables.
1) Total issues (Gross Allocation) are in the Table, Named "issues_tot"
+---------+------+------------+
| v_code | oid | amount |
+---------+------+------------+
| 1 | 2 | 200,000.00 |
| 1 | 3 | 80,000.00 |
| 2 | 1 | 40,000.00 |
| 3 | 2 | 150,000.00 |
+---------+------+------------+
2) Cut amounts (Cut Allocation) are in the Table, Named "cp_tot"
+--------+-----+-----------+
| v_code | oid | amount |
+--------+-----+-----------+
| 1 | 2 | 68,000.00 |
| 1 | 3 | 50,000.00 |
| 3 | 2 | 75,000.00 |
+--------+-----+-----------+
3) Table, Named "vote"
+--------+-------------------------+
| v_code | vote |
+--------+-------------------------+
| 1 | 001-2-6-3-2502 |
| 2 | 001-1-4-21-2202 |
| 3 | 101-1-2-0-1405 |
+--------+-------------------------+
4) Table, Named "office"
+-----+----------------------+
| oid | office |
+-----+----------------------+
| 1 | Weeraketiya |
| 2 | Tissamaharama |
| 3 | District Sec |
+-----+----------------------+
And desired output as follows:
+--------+------------+-----------+------------+
| v_code | Gross | Cut | Net |
+--------+------------+-----------+------------+
| 1 | 200,000.00 | 68,000.00 | 132,000.00 |
| 1 | 80,000.00 | 50,000.00 | 30,000.00 |
| 2 | 40,000.00 | 0.00 | 40,000.00 |
| 3 | 150,000.00 | 75,000.00 | 75,000.00 |
+--------+------------+-----------+------------+
02) I used the following script to generate that output
select `vote`.`vote` AS `vote`,`office`.`office` AS `office`,
`issues_tot`.`amount` AS `Gross`,
coalesce(`cp_tot`.`amount`,0) AS `Cut`,
(`issues_tot`.`amount` - coalesce(`cp_tot`.`amount`,0)) AS `Net`
from (((`vote` join `issues_tot` on((`vote`.`v_code` = `issues_tot`.`v_code`))) join
`office` on((`office`.`oid` = `issues_tot`.`oid`))) left join
`cp_tot` on((`issues_tot`.`v_code` = `cp_tot`.`v_code`)))
But it generates the following output with repeated records:
+------------+----------------+--------------+-------------+--------------+
| Vote | Office | Gross | Cut | Net |
+---------------+-------------+--------------+-------------+--------------+
| 001-2-6-3-2502| Tissamaharama | 200,000.00 | 68,000.00 |132,000.00 |
| 001-2-6-3-2502| Tissamaharama | 200,000.00 | 50,000.00 | 150,000.00 |
| 001-2-6-3-2502| District Sec | 80,000.00 | 68,000.00 | 12,000.00 |
| 001-2-6-3-2502| District Sec | 80,000.00 | 50,000.00 | 30,000.00 |
| 001-1-4-21-2202| Weeraketiya | 40,000.00 | - | 40,000.00 |
| 101-1-2-0-1405 | Tissamaharama | 150,000.00 | 75,000.00 | 75,000.00 |
+------------+-----------------+--------------+-------------+--------------+
I can not understand what was going wrong. Can anyone help me?

Firstly, the following query will get what you want:
select
v.vote,
o.office,
it.amount as Gross,
coalesce(ct.amount , 0) as Cut,
it.amount - coalesce(ct.amount, 0) as Net
from issues_tot it
left join cp_tot ct
on it.v_code = ct.v_code
and it.oid = ct.oid
left join vote v
on it.v_code = v.v_code
left join office o
on it.oid = o.oid
order by it.v_code
and SQLFiddle Demo Here, the only issue is that you forget to match oid between issues_tot and cp_tot.
If there is no oid criteria, the match will do like following:
# issues_tot # cp_tot
| v_code | oid | amount | | v_code | oid | amount |
+---------+------+------------+ +--------+-----+-----------+
| 1 | 2 | 200,000.00 | -> | 1 | 2 | 68,000.00 |
| 1 | 3 | 50,000.00 |
| 1 | 3 | 80,000.00 | -> | 1 | 2 | 68,000.00 |
| 1 | 3 | 50,000.00 |
| 2 | 1 | 40,000.00 | -> no record match
| 3 | 2 | 150,000.00 | -> | 3 | 2 | 75,000.00 |
so 6 records by your query is it.

Related

Access Query Count Records in Another Table without Requery?

I'm trying to make a report where I need to know the the count of items from another table like this
+----------+--------+--------------------------------------------+
| Sale No. | Widget | Total Sold |
+----------+--------+--------------------------------------------+
| 123 | foo | Dcount(another table where widget = "foo") |
| 456 | bar | Dcount(another table where widget = "bar") |
+----------+--------+--------------------------------------------+
.
SELECT [Sale No.]
, Widget
, Dcount("SELECT foo from whatever where widget = " & widget) as [Total Sold]
FROM sometable
Unfortunately this queries the database for every record, for such a report that must be run daily this isn't really efficient.
Is there a way to query this once, and either through VBA or some SQL I don't know to hold the query in memory or the counts of each unique item. Basically query the other table just the one time instead of N times.
Here's a more accurate table that reflects my data closer
+----------+------------+---------+------------------+
| Employee | Department | Policy | Review Requested |
+----------+------------+---------+------------------+
| 123 | Sales | PlanABC | TRUE |
| 456 | Sales | PlanABC | TRUE |
| 789 | Accounting | PlanXYZ | FALSE |
| 101112 | Accounting | PlanXYZ | TRUE |
| 131415 | Sales | PlanXYZ | FALSE |
| 161718 | Admin | PlanJKL | TRUE |
+----------+------------+---------+------------------+
And the result I'm going for
+------------+----------+---------+----------------------+
| Department | Employee | Policy | Count of All Polices |
+------------+----------+---------+----------------------+
| Sales | 123 | PlanABC | 2 |
| Sales | 456 | PlanABC | 2 |
| Accounting | 101112 | PlanXYZ | 3 |
| Admin | 161718 | PlanJKL | 1 |
+------------+----------+---------+----------------------+
If your table is set up as below:
| Sale No | Widget |
|---------|--------|
| 1 | Foo |
| 2 | Bar |
| 3 | Foo |
| 4 | Foo |
| 5 | Bar |
| 6 | Foo |
| 7 | Foo |
| 8 | Bar |
| 9 | Bar |
| 10 | Foo |
You can't include the Sale No as it will group the values on that.
SELECT Widget
, COUNT(Widget) AS [Total Sold]
FROM sometable
GROUP BY Widget
Just adding the Widgets and grouping on them will return:
| Widget | Total Sold |
|--------|------------|
| Bar | 4 |
| Foo | 6 |
If, on the other hand, your Sale No field is duplicated then you can get a count per Sale No.
| Sale No | Widget |
|---------|--------|
| 1 | Foo |
| 1 | Bar |
| 5 | Foo |
| 5 | Foo |
| 5 | Bar |
| 7 | Foo |
| 7 | Foo |
| 7 | Bar |
| 7 | Bar |
| 10 | Foo |
Here the Sale No is added and the query is grouped by all fields that are not being aggregated.
SELECT [Sale No]
, Widget
, COUNT(Widget) As [Total Sold]
FROM sometable
GROUP BY [Sale No]
, Widget
This would return this table:
| Sale No | Widget | Total Sold |
|---------|--------|------------|
| 1 | Bar | 1 |
| 1 | Foo | 1 |
| 5 | Bar | 1 |
| 5 | Foo | 2 |
| 7 | Bar | 2 |
| 7 | Foo | 2 |
| 10 | Foo | 1 |
Edit:
Based on the provided table this SQL should give the correct result:
SELECT T1.Department
, T1.Employee
, T1.Policy
, COUNT(T2.Policy)
FROM sometable T1 INNER JOIN sometable T2 ON T1.Policy = T2.Policy
GROUP BY T1.Department
, T1.Employee
, T1.Policy
Resulting table:
| Department | Employee | Policy | Expr1003 |
|------------|----------|---------|----------|
| Accounting | 789 | PlanXYZ | 3 |
| Accounting | 101112 | PLanXYZ | 3 |
| Admin | 161718 | PLanJKL | 1 |
| Sales | 123 | PlanABC | 1 |
| Sales | 456 | PalnABC | 1 |
| Sales | 131415 | PlanXYZ | 3 |

Selected Child Categories based on parent categories from another table

I have two tables
rules
With three step hierarchy
|id | name | parent |
|---|-------|--------|
| 1 | A | 0 |
| 2 | B | 0 |
| 3 | A(1) | 1 |
| 4 | A(2) | 1 |
| 5 | B(1) | 2 |
| 6 | A(1.1)| 3 |
| 7 | A(1.2)| 3 |
| 8 | A(2.1)| 4 |
| 9 | B(1.1)| 5 |
| 10| A(3) | 1 |
Subject
|id | date | rules | group |
|---|---------------------|-------|-------|
| 1 | 2016-05-20 18:24:20 | 2 | AQR48 |
| 2 | 2016-05-20 19:31:17 | 5 | AQR52 |
| 3 | 2016-05-21 18:11:37 | 6,7,4 | AQR48 |
I need to get second step rules based on group and ruleid(first step) of subject table data
When group = 'AQR48' and rules.parent=1 result should be
|id | name | parent |
|---|-------|--------|
| 3 | A(1) | 1 |
| 4 | A(2) | 1 |
I tried it like this but with out success.
select rules.id,rules.name,rules.parent from rules left join subject on find_in_set(rules.id,subject.rules) where rules.parent=1 AND subject.group='AQR48'
With this I get output as
|id | name | parent |
|---|-------|--------|
| 4 | A(2) | 1 |
Anyone could help me with this

How to get count of combinations from database?

How to get count of combinations from database?
I have to database tables and want to get the count of combinations. Does anybody know how to put this in a database query, therefore I haven't a db request for each trip?
Trips
| ID | Driver | Date |
|----|--------|------------|
| 1 | A | 2015-12-15 |
| 2 | A | 2015-12-16 |
| 3 | B | 2015-12-17 |
| 4 | A | 2015-12-18 |
| 5 | A | 2015-12-19 |
Passengers
| ID | PassengerID | TripID |
|----|-------------|--------|
| 1 | B | 1 |
| 2 | C | 1 |
| 3 | D | 1 |
| 4 | B | 2 |
| 5 | D | 2 |
| 6 | A | 3 |
| 7 | B | 4 |
| 8 | D | 4 |
| 9 | B | 5 |
| 10 | C | 5 |
Expected result
| Driver | B-C-D | B-D | A | B-C |
|--------|-------|-----|---|-----|
| A | 1 | 2 | - | 1 |
| B | - | - | 1 | - |
Alternative
| Driver | Passengers | Count |
|--------|------------|-------|
| A | B-C-D | 1 |
| A | B-D | 2 |
| A | B-C | 1 |
| B | A | 1 |
Has anybody an idea?
Thanks a lot!
Try this:
SELECT Driver, Passengers, COUNT(*) AS `Count`
FROM (
SELECT t.ID, t.Driver,
GROUP_CONCAT(p.PassengerID
ORDER BY p.PassengerID
SEPARATOR '-') AS Passengers
FROM Trips AS t
INNER JOIN Passengers AS p ON t.ID = p.TripID
GROUP BY t.ID, t.Driver) AS t
GROUP BY Driver, Passengers
The above query will produce the alternative result set. The other result set can only be achieved using dynamic sql.
Demo here

select data from one table based on the data status on other table

My first data table is couponsnmaster
+-----------+----------+------------+
|couponsnid | couponid | couponsn |
+-----------+----------+------------+
| 1 | 1 | 1000 |
| 2 | 1 | 1001 |
| 3 | 1 | 1002 |
| 4 | 1 | 1003 |
| 5 | 1 | 1004 |
| 6 | 1 | 1005 |
+-----------+----------+------------+
My second data table is distribute
+-----------+--------------+--------------+--------------+
| distid | couponid | couponsnid | status |
+-----------+--------------+--------------+--------------+
| 1 | 1 | 1 | distribute |
| 2 | 1 | 2 | distribute |
| 3 | 1 | 3 | distribute |
| 4 | 1 | 1 | returned |
+-----------+--------------+--------------+--------------+
I want to fetch all "couponsn" from "couponsnmaster" with respect to "couponid" except status is " distribute" or "sold" or "bonus" in table "distribute"....
Try this query:
SELECT c.couponsn FROM
couponsmaster c INNER JOIN distribute d
ON c.couponsid = d.couponsid
WHERE d.status NOT IN('distribute','sold','bonus')

Select rows with dates and article points (without relation)

Iv got following tables:
articles
+----+-------------+-----------------------------+--------------+
| ID | ID_group_AG | Title | Date_publish |
+----+-------------+-----------------------------+--------------+
| 1 | 10 | O obrotach sfer niebieskich | 2009-05-07 |
| 2 | 11 | Technologia betonu | 2011-03-21 |
| 3 | 12 | test | 2008-01-13 |
+----+-------------+-----------------------------+--------------+
employee
+----+-----------+-----------+
| ID | Name | Surname |
+----+-----------+-----------+
| 1 | Andrzej | Gacek |
| 2 | Leszek | Ksiazek |
| 3 | Krzysztof | Skibinski |
| 4 | Andrzej | Inny |
+----+-----------+-----------+
articlesGroup
+----+----------+---------------+----------------+
| ID | ID_group | ID_employee | Points |
+----+----------+---------------+----------------+
| 1 | 10 | 1 | 3 |
| 2 | 10 | 3 | 3 |
| 3 | 11 | 1 | 2 |
| 4 | 11 | 2 | 2 |
| 5 | 11 | 4 | 2 |
| 6 | 12 | 4 | 6 |
+----+----------+---------------+----------------+
And following relations:
articles.ID_group_AG => articlesGroup.ID_group
articlesGroup.ID_employee => employee.ID
What I need to do is to print all article points related to a employee, article and publish date, so I use folowing query:
SELECT
p.Name,
p.Surname,
a.Date_publish,
ag.Points
FROM
employee p,
articles a,
articlesGroup ag
WHERE
(ag.ID_group = a.ID_group_AG) AND
(ag.ID_employee = p.ID)
and I get:
+-----------+-----------+--------------+----------------+
| Name | Surname | Date_publish | Points |
+-----------+-----------+--------------+----------------+
| Andrzej | Gacek | 2009-05-07 | 3 |
| Andrzej | Gacek | 2011-03-21 | 2 |
| Leszek | Ksiazek | 2011-03-21 | 2 |
| Krzysztof | Skibinski | 2009-05-07 | 3 |
| Andrzej | Inny | 2011-03-21 | 2 |
| Andrzej | Inny | 2008-01-13 | 6 |
+-----------+-----------+--------------+----------------+
Now lets get to the problem :)
Im using pChart library to make charts.
I want to put on Y axis Points, on X axis all Dates regarding each employee.
So Points array for employee "Andrzej Gacek" will be: [3,2]
for employee "Krzysztof Skibinski" will be: [3]
and Date array (sorted): ["2008-01-13","2009-05-07","2011-03-21"]
I need to add zero points to employee Points array for ex. for "Andrzej Gacek" array should look like: [0,3,2]
so Point will correlate with Dates.
How to form a query to add zeroes to points so the output of the query would look like this:
+-----------+-----------+--------------+----------------+
| Name | Surname | Date_publish | Points |
+-----------+-----------+--------------+----------------+
| Andrzej | Gacek | 2009-05-07 | 3 |
| Andrzej | Gacek | 2011-03-21 | 2 |
| Andrzej | Gacek | 2008-01-13 | 0 |
| Leszek | Ksiazek | 2011-03-21 | 2 |
| Leszek | Ksiazek | 2009-05-07 | 0 |
| Leszek | Ksiazek | 2008-01-13 | 0 |
| Krzysztof | Skibinski | 2009-05-07 | 3 |
| Krzysztof | Skibinski | 2011-03-21 | 0 |
| Krzysztof | Skibinski | 2008-01-13 | 0 |
| Andrzej | Inny | 2011-03-21 | 2 |
| Andrzej | Inny | 2008-01-13 | 6 |
| Andrzej | Inny | 2009-05-07 | 0 |
+-----------+-----------+--------------+----------------+
You have to create a Cartesian Product to get the results you are looking for. I've used a CROSS JOIN to get the dates for each employee.
Give this a try:
SELECT DISTINCT E.Name,
E.SurName,
a.Date_Publish,
IFNULL(AG.Points,0) Points
FROM Articles A CROSS JOIN
Employee E LEFT JOIN
ArticlesGroup AG ON ag.ID_group = a.ID_group_AG
AND E.Id = ag.ID_employee
And here is the SQL Fiddle.