How to get total after Group By [duplicate] - mysql

This question already has answers here:
Add a summary row with totals
(6 answers)
Closed 4 years ago.
SELECT COUNT(*) AS customer, t.state
FROM tbl_parcel_item t
WHERE t.courier_name='Tnt'
AND t.date = '2018-03-12'
AND t.ship_status NOT IN (0,1,9,10)
GROUP BY t.state`
Based on the sql above the results would be:
enter image description here
How to make it get total of the result above for example :
enter image description here

Use with rollup:
SELECT COUNT(*) AS customer, t.state`
FROM tbl_parcel_item t
WHERE t.courier_name = 'Tnt' AND
t.date = '2018-03-12' AND
t.ship_status NOT IN (0, 1, 9, 10)
GROUP BY t.state WITH ROLLUP;

Use union all:
`SELECT COUNT(*) AS customer, t.state`
`FROM tbl_parcel_item t`
`WHERE t.courier_name='Tnt'`
`AND t.date = '2018-03-12'`
`AND t.ship_status NOT IN (0,1,9,10)`
`GROUP BY t.state`
`UNION ALL`
`SELECT COUNT(*) AS customer, 'Total' as state`
`FROM tbl_parcel_item t`
`WHERE t.courier_name='Tnt'`
`AND t.date = '2018-03-12'`
`AND t.ship_status NOT IN (0,1,9,10)`

Use ROLLUP:
SELECT
COALESCE(t.state, 'TOTAL') AS state,
COUNT(*) AS customer
FROM tbl_parcel_item t
WHERE
t.courier_name = 'Tnt' AND
t.date = '2018-03-12' AND
t.ship_status NOT IN (0,1,9,10)
GROUP BY t.state WITH ROLLUP;

01) WITH ROLLUP as Explained in the other answers
02)
Use Union Query
SELECT t.state, COUNT(*) AS customer FROM tbl_parcel_item t WHERE t.courier_name='Tnt' AND t.date = '2018-03-12' AND t.ship_status NOT IN (0,1,9,10) GROUP BY t.state
UNION
SELECT 'Total', COUNT(*) AS customer FROM tbl_parcel_item t WHERE t.courier_name='Tnt' AND t.date = '2018-03-12' AND t.ship_status NOT IN (0,1,9,10)

Related

2 SELECT in a query with ORDER BY

I need to create a MYSQL query that will return the following:
excel
this is my query
SELECT t1.fecha_salida,
t1.agencia,
t2.fecha_salida,
t2.directa
FROM (SELECT fecha_salida, COUNT(*) AS agencia
FROM ventas
WHERE ventas.eliminado = 0 AND ventas.cliente_id !=2
GROUP BY fecha_salida) AS t1,
(SELECT fecha_salida, COUNT(*) AS directa
FROM ventas
WHERE ventas.eliminado = 0 AND ventas.cliente_id =2
GROUP BY fecha_salida) AS t2
GROUP BY t1.fecha_salida
my table is:
|------ |Columna|Tipo|Nulo|Predeterminado |------
|//**venta_id**//|int(11)|No| |usuario_id|int(11)|No|
|tour_id|int(11)|No| |cliente_id|int(11)|No| |vehiculo_id|int(11)|No|
|cashflow_id|int(11)|No| |total_compra|int(6)|No|
|total_pagado|int(6)|No| |total_iva|int(6)|No|
|total_comision|int(11)|No| |a_pagar|int(11)|No|
|fecha_venta|datetime|No| |fecha_salida|date|No|
|concretada|tinyint(1)|No| |pasajeros|int(3)|No|
|nombre|varchar(500)|Sí|NULL |direccion|varchar(500)|Sí|NULL
|observaciones|varchar(500)|Sí|NULL |forma_pago|tinyint(2)|No|
|eliminado|tinyint(1)|No|
Can you help me
You can use UNION to combine two queries that have same structure , also you should select from combined result and then order them
SELECT * FROM (
SELECT fecha_salida, COUNT(*) AS agencia
FROM ventas
WHERE `fecha_venta` between '2012-03-11 00:00:00' and '2019-08-30 23:59:00'
and ventas.eliminado = 0
AND ventas.cliente_id = 2
GROUP BY fecha_salida
UNION
SELECT fecha_salida, COUNT(*) AS agencia
FROM ventas
WHERE `fecha_venta` between '2012-03-11 00:00:00' and '2019-08-30 23:59:00'
and ventas.eliminado = 0
AND ventas.cliente_id != 2
GROUP BY fecha_salida
) RESULT
ORDER BY agencia

Single Query to Count Data from Multiple Table Mysql

I have tables that contain same field, for example:
p_central_ticket p_south_ticket p_west_ticket
=====================================================================
- t_id - t_id - t_id
- t_req_type - t_req_type - t_req_type
- t_status - t_status - t_status
And i have one table :
m_event_type
=============
- ev_type
- ev_activity
My current query :
SELECT ev_activity AS Activity, COUNT( * ) AS Total
FROM m_event_type
LEFT JOIN p_central_ticket ON p_central_ticket.t_req_type = m_event_type.ev_type
WHERE t_status =9
GROUP BY ev_activity
Output from query above:
My question is, how should i do, if i want to total count from 3 tables above.
(For Example Activity Change Request total 18000, count from
p_central_ticket + p_south_ticket + p_west_ticket) etc.
Thanks...
Use union all in a subquery, then join it:
select t1.ev_Activity, count(t1.*) as total
from m_event_type t1
LEFT JOIN
(
select *
from p_central_ticket
WHERE t_status =9
union all
select *
from p_south_ticket
WHERE t_status =9
union all
select *
from p_west_ticket
WHERE t_status =9
) t2
ON t2.t_req_type = t1.ev_type
GROUP BY t1.ev_activity
Use a UNION ALL (to avoid removing duplicates) then SUM the quantities
Note that it also works if your tables have different column names you only need to alias them in the select.
SELECT ev_activity AS Activity, SUM(quantity) AS Total
FROM m_event_type met
LEFT JOIN (SELECT c.t_req_type, COUNT(*) as quantity
FROM p_central_ticket c
WHERE c.t_status =9
GROUP BY c.t_req_type
UNION ALL
SELECT s.t_req_type, COUNT(*)
FROM p_south_ticket s
WHERE s.t_status =9
GROUP BY s.t_req_type
UNION ALL
SELECT w.t_req_type, COUNT(*)
FROM p_west_ticket w
WHERE w.t_status =9
GROUP BY w.t_req_type) p ON
p.t_req_type = met.ev_type
GROUP BY ev_activity
You could use UNION ALL for select all the rows of the 3 tables
SELECT ev_activity AS Activity, COUNT( * ) AS Total
FROM m_event_type
LEFT JOIN (
select t_id, t_req_type, t_status
from p_central_ticket
union all
select t_id, t_req_type, t_status
from p_south_ticket
union all
select t_id, t_req_type, t_status
from p_west_ticket
) t ON t.t_req_type = m_event_type.ev_type
WHERE t.t_status =9
GROUP BY ev_activity

Translate Lead/Lag function to MySQL

How do I translated the below codes so that it runs in MySQL. I am new to MySQL and finding it difficult to understand few things.
CODE 1:
SELECT t1.user_id,
t1.visit_month LEAD (t1.visit_month, 1) OVER (partition BY t1.user_id ORDER BY t1.user_id, t1.visit_month)
FROM (SELECT
user_id,
month(date_time) as visit_month
FROM
tbl_activity
group by 1, 2
ORDER BY 1 , 2) t1;
Desired Output for Code 1
CODE 2:
SELECT user_id,
visit_month,
lead,
lead — visit_month AS time_diff
FROM table3
CODE 3:
SELECT user_id,
Visit_month,
lag(visit_month, 1) over (partition BY user_id ORDER BY user_id, visit_month)
FROM table
CODE 4:
SELECT user_id,
visit_month,
lag,
visit_month — lag AS time_diff
FROM table2
You could express the lead() and lag() function via subquery
select user_id, month(date_time) as visit_month,
month(date_time)-(select month(date_time) from tbl_activity
where user_id = a.user_id and
month(date_time) < month(a.date_time)
order by month(date_time) desc LIMIT 1) as time_diff -- This could be re-express via lag() function
from tbl_activity a
group by user_id, month(date_time);
In above you would need to just specify < / > to express the lead() and lag() functionality in subquery and don't forget to use order by clause
EDIT (Lead) :
select user_id, month(date_time) as visit_month,
(select month(date_time) from tbl_activity
where user_id = a.user_id and
month(date_time) > month(a.date_time)
order by month(date_time) LIMIT 1) as lead -- This could be re-express via lead() function
from tbl_activity a
group by user_id, month(date_time);

SQL Queries to analyse Employee Database

I am looking for queries, using which I can analyze a general employee database. This is for Data Analysis.
Tried this for monthly employee trend
SELECT
dt.FullDateAlternateKey as 'Date'
, count(1) as ActiveCount
FROM DimDate dt
LEFT JOIN (SELECT 'Active' as 'EmpStatus', * FROM DimEmployee) emp
-- regular active employees
ON (dt.FullDateAlternateKey between emp.StartDate and ISNULL(emp.EndDate,'9999-12-31'))
WHERE
dt.FullDateAlternateKey = EOMONTH(dt.FullDateAlternateKey)
GROUP BY
dt.FullDateAlternateKey
ORDER BY
1;
also found CTE use for finding employee hierarchy
WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level)
AS
(
-- Anchor member definition
SELECT e.ParentEmployeeKey, e.EmployeeKey, e.Title, e.DepartmentName,
0 AS Level
FROM DimEmployee AS e
WHERE e.ParentEmployeeKey IS NULL
UNION ALL
-- Recursive member definition
SELECT e.ParentEmployeeKey, e.EmployeeKey, e.Title, e.DepartmentName,
Level + 1
FROM DimEmployee AS e
INNER JOIN DirectReports AS d
ON e.ParentEmployeeKey = d.EmployeeID
)
-- Statement that executes the CTE
SELECT ManagerID, EmployeeID, Title, DeptID, Level
FROM DirectReports
WHERE DeptID = 'Information Services' OR Level = 0
also, some good queries to analyze the sales data
-- Show each sales average for Group, Country, and Region all in one query
SELECT DISTINCT
t.SalesTerritoryGroup
, t.SalesTerritoryCountry
, t.SalesTerritoryRegion
, AVG(s.SalesAmount) OVER(PARTITION BY t.SalesTerritoryGroup ) as 'GroupAvgSales'
, AVG(s.SalesAmount) OVER(PARTITION BY t.SalesTerritoryCountry ) as 'CountryAvgSales'
, AVG(s.SalesAmount) OVER(PARTITION BY t.SalesTerritoryRegion ) as 'RegionAvgSales'
FROM FactInternetSales s
JOIN DimSalesTerritory t ON
s.SalesTerritoryKey = t.SalesTerritoryKey
WHERE
YEAR(s.OrderDate) = 2013
ORDER BY
1,2,3
Use additional aggregations to understand more about product sales such as the distribution of sales etc..
SELECT
cat.EnglishProductCategoryName 'Category'
, sub.EnglishProductSubcategoryName 'SubCategory'
, count(1) 'Count' -- How many sales where there?
, sum(s.SalesAmount) 'Sales' -- How much sales did we have?
, avg(s.SalesAmount) 'Avg_SalesAmount' -- What was the Avg sale amount?
, min(s.SalesAmount) 'Min_SaleAmount' -- What was the Min sale amount?
, max(s.SalesAmount) 'Max_SaleAmount' -- What was the Max sale amount
FROM FactInternetSales s
LEFT JOIN DimProduct p ON s.ProductKey = p.ProductKey
LEFT JOIN DimProductSubcategory sub ON p.ProductSubcategoryKey = sub.ProductSubcategoryKey
LEFT JOIN DimProductCategory cat ON sub.ProductCategoryKey = cat.ProductCategoryKey
-- must use group by in order for aggregation to work properly
GROUP BY
cat.EnglishProductCategoryName -- column aliases aren't allowed
, sub.EnglishProductSubcategoryName
ORDER BY
cat.EnglishProductCategoryName
, sub.EnglishProductSubcategoryName
-- Calculate the customer acquisition funnel
SELECT
c.FirstName
, c.LastName
, c.DateFirstPurchase
, DATEDIFF(d,c.DateFirstPurchase,getdate()) as 'DaysSinceFirstPurchase' -- How long have they been a customer?
FROM DimCustomer c
ORDER BY 3 DESC
-- Calculate a Monthly average of customer tenure
SELECT
EOMONTH(c.DateFirstPurchase) as 'MonthOfFirstPurchase' -- What month did they become a customer?
, DATEDIFF(d,EOMONTH(c.DateFirstPurchase),getdate()) as 'DaysSinceFirstPurchase' -- How long have they been a customer?
, COUNT(1) as 'CustomerCount' -- How manY customers are there for this month?
FROM DimCustomer c
GROUP BY EOMONTH(c.DateFirstPurchase)
ORDER BY 2 DESC
-- Show the top product Sub Categories for each year
SELECT
count(DISTINCT s.SalesOrderNumber) 'OrderCount' -- use 1 instead of a field for faster performance
, RANK() OVER (PARTITION BY YEAR(s.OrderDate) ORDER BY sum(s.SalesAmount) DESC) 'SalesRank'
, sum(s.SalesAmount) 'TotalSales'
, cat.EnglishProductCategoryName 'Category'
, sub.EnglishProductSubcategoryName 'SubCategory'
, YEAR(s.OrderDate) 'Year'
FROM FactInternetSales s
INNER JOIN DimProduct p ON s.ProductKey = p.ProductKey
INNER JOIN DimProductSubcategory sub ON p.ProductSubcategoryKey = sub.ProductSubcategoryKey
INNER JOIN DimProductCategory cat ON sub.ProductCategoryKey = cat.ProductCategoryKey
-- must use group by in order for aggregation to work properly
GROUP BY
cat.EnglishProductCategoryName -- column aliases aren't allowed
, sub.EnglishProductSubcategoryName
, YEAR(s.OrderDate)
ORDER BY YEAR(s.OrderDate), SUM(s.SalesAmount) DESC;
-- first, create weekly sales totals
SELECT SUM(s.SalesAmount) 'WeeklySales'
, DATEPART(ww, s.OrderDate) as 'WeekNum'
FROM FactInternetSales s
WHERE YEAR(s.OrderDate) = 2013
GROUP BY
DATEPART(ww, s.OrderDate)
ORDER BY
DATEPART(ww, s.OrderDate) ASC
-- use that subquery as our source and calculate the moving average
SELECT
AVG(WeeklySales) OVER (ORDER BY WeekNum ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as AvgSales
, WeeklySales as 'TotalSales'
, WeekNum
FROM (
SELECT SUM(s.SalesAmount) 'WeeklySales'
, DATEPART(ww, s.OrderDate) as 'WeekNum'
FROM FactInternetSales s
WHERE YEAR(s.OrderDate) = 2013
GROUP BY
DATEPART(ww, s.OrderDate)
) AS s
GROUP BY
WeekNum, WeeklySales
ORDER BY
WeekNum ASC
-- Running Total
SELECT
SUM(MonthlySales) OVER (PARTITION BY SalesYear ORDER BY SalesMonth ROWS UNBOUNDED PRECEDING) as YTDSales
, MonthlySales as 'MonthlySales'
, SalesYear
, SalesMonth
FROM (
SELECT SUM(s.SalesAmount) 'MonthlySales'
, MONTH(s.OrderDate) as 'SalesMonth'
, year(s.OrderDate) as 'SalesYear'
FROM FactInternetSales s
GROUP BY
MONTH(s.OrderDate)
, year(s.OrderDate)
) AS s
GROUP BY
SalesMonth, SalesYear, MonthlySales
ORDER BY
SalesYear, SalesMonth ASC
-- Get Prev Year Sales
WITH MonthlySales (YearNum, MonthNum, Sales)
AS
(
SELECT d.CalendarYear, d.MonthNumberOfYear, SUM(s.SalesAmount)
FROM DimDate d
JOIN FactInternetSales s ON d.DateKey = s.OrderDateKey
GROUP BY d.CalendarYear, d.MonthNumberOfYear
)
-- Get Current Year and join to CTE for previous year
SELECT
d.CalendarYear
, d.MonthNumberOfYear
, ms.Sales PrevSales
, SUM(s.SalesAmount) CurrentSales
FROM DimDate d
JOIN FactInternetSales s ON d.DateKey = s.OrderDateKey
JOIN MonthlySales ms ON
d.CalendarYear-1 = ms.YearNum AND
d.MonthNumberOfYear = ms.MonthNum
GROUP BY
d.CalendarYear
, d.MonthNumberOfYear
, ms.Sales
ORDER BY
1 DESC, 2 DESC
-- Now calculate the % change Year over Year
WITH MonthlySales (YearNum, MonthNum, Sales)
AS
(
SELECT d.CalendarYear, d.MonthNumberOfYear, SUM(s.SalesAmount)
FROM DimDate d
JOIN FactInternetSales s ON d.DateKey = s.OrderDateKey
GROUP BY d.CalendarYear, d.MonthNumberOfYear
)
-- Get Current Year and join to CTE for previous year
SELECT
d.CalendarYear
, d.MonthNumberOfYear
, ms.Sales PrevSales
, SUM(s.SalesAmount) CurrentSales
, (SUM(s.SalesAmount) - ms.Sales) / SUM(s.SalesAmount) 'PctGrowth'
FROM DimDate d
JOIN FactInternetSales s ON d.DateKey = s.OrderDateKey
JOIN MonthlySales ms ON
d.CalendarYear-1 = ms.YearNum AND
d.MonthNumberOfYear = ms.MonthNum
GROUP BY
d.CalendarYear
, d.MonthNumberOfYear
, ms.Sales
ORDER BY
1 DESC, 2 DESC

SQL where exists

I am trying to get the total amount spent on transactions with a specific product sold during the period below.
SELECT
i.Customer,
SUM(i.GrandTotal)
FROM
transaction i
WHERE EXISTS
(SELECT
1
FROM
transactionline il
INNER JOIN product p
ON p.ProductId = il.ProductId
WHERE i.InvoiceDate >= '2016-09-01 00:00:00'
AND i.InvoiceDate <= '2017-08-31 23:59:59'
AND p.TableProductType = 25)
GROUP BY i.Customer;
My problem is that this query has a result the total amount spent during that period and not only transactions with that product involved
I started of with something like this :
SELECT
i.Customer,
SUM(i.GrandTotal)
FROM
transaction i,transactionline il , product p
WHERE i.InvoiceDate >= '2016-09-01 00:00:00'
AND i.InvoiceDate <= '2017-08-31 23:59:59'
AND p.TableProductType = 25);
But this also took much longer and the final result was wrong (cause of the multiplication caused by the join between transaction and transaction line)
few ways, all solutions using the with following as its data source directly before query, but substitute with your table name
with dat
as
(
select 1 tranid,'N' Tableproducttype,9.00 GrandTotal union all
select 1,'N',11.12 union all
select 1,'N',14.23 union all
select 1,'25',8.88 union all
select 1,'N',7.77 union all
select 1,'Y',6.66 union all
select 2,'N',3.21 union all
select 2,'N',19.13 union all
select 2,'Y',1.23 union all
select 3,'Y',4.31 union all
select 4,'Y',15.43 union all
select 4,'Y',15.12 union all
select 5,'N',14.32)
1.) works using an IN
select tranid,sum(GrandTotal) GrandTotal
from dat
where tranid in (select tranid from dat where TableProductType = '25')
group by tranid
2.) using an exists
select tranid,sum(GrandTotal) GrandTotal
from dat
where exists (select 'x' from dat dat_inner
where dat_inner.TableProductType = '25'
and dat_inner.tranid = dat.tranid)
group by tranid
3.) using having, but could be slow
select tranid,sum(GrandTotal) GrandTotal
from dat
group by tranid
having sum(case when TableProductType = '25' then 1 else 0 end)>0 /* at least one product of type 25 */