In this Query, I would like to show the total of clients by state/county. Is this possible?
I guess a running total is ok in new column or if possible a break when the state/county changes to show the total of the previous count?
So output like this>
|first_name|last_name|description|description|
VIN ULTI New York Bronx 1
MEL MORRIS New York Bronx 2
BYRAM BROWN New York Bronx 3 or preferred a line total:
*** Total Clients Bronx New York = 3
SELMA MADDISON New York Queens 1
*** Total Clients New York Queens = 1
select ac.first_name, ac.last_name, st.description, co.description
from address ad
inner join all_clients_view ac
on ad.people_id = ac.people_id
inner join county co
on ad.county = co.county_id
inner join state st
on co.state_id = st.state_id
where ad.is_active = 1
order by st.description, co.description
You can try with these scripts:
1) Total of the previuos count:
select ac.first_name, ac.last_name, st.description, co.description,
(select count(*)
from address ad2
inner join all_clients_view ac2
on ad2.people_id = ac2.people_id
inner join county co2
on ad2.county = co2.county_id
inner join state st2
on co2.state_id = st2.state_id
where ad2.is_active = 1
and ad2.county = ad.county
and ad2.people_id <= ad.people_id)
as count_progress
from address ad
inner join all_clients_view ac
on ad.people_id = ac.people_id
inner join county co
on ad.county = co.county_id
inner join state st
on co.state_id = st.state_id
where ad.is_active = 1
order by st.description, co.description, ad.people_id;
FIRST LAST_NAM DESCRIPT DESCRI COUNT_PROGRESS
----- -------- -------- ------ --------------
VIN ULTI New York Bronx 1
MEL MORRIS New York Bronx 2
BYRAM BROWN New York Bronx 3
SELMA MADDISON New York Queens 1
2) Line Total:
select st.description as state, co.description as county, count(*)
from address ad
inner join all_clients_view ac
on ad.people_id = ac.people_id
inner join county co
on ad.county = co.county_id
inner join state st
on co.state_id = st.state_id
where ad.is_active = 1
group by st.description, co.description
order by st.description, co.description;
STATE COUNTY COUNT(*)
-------- ------ ----------
New York Bronx 3
New York Queens 1
Thank you.
Related
I have the following sales structure. Managers are assigned a salesperson per region but also sell themselves. The table below holds the link between managers and salespersons/regions.
TABLE: MSR
Manager
Sales
Region
MA
SA
EAST
MA
SB
EAST
MA
SB
NORTH
MB
SA
WEST
MB
SB
WEST
This table lists the orders that each person scored.
TABLE: ORD
OrderId
Sales
Region
1
MA
EAST
2
MA
WEST
3
SA
WEST
4
SA
EAST
5
SB
EAST
6
SB
WEST
7
MB
NORTH
8
SB
NORTH
I want an order output per manager. For example for manager MA it would look like this:
OrderId
Sales
Region
1
MA
EAST
2
MA
WEST
4
SA
EAST
5
SB
EAST
8
SB
NORTH
For MB it looks like this:
OrderId
Sales
Region
3
SA
WEST
6
SB
WEST
7
MB
NORTH
Is there an SQL statement that can produce this result?
If for any reason you need to return/use the manager, you can use something like the following:
SELECT ORD.*, IFNULL(MSR.Manager,ORD.Sales) as ActualManager
FROM ORD
LEFT JOIN MSR
ON (ORD.Sales = MSR.Sales and ORD.Region = MSR.Region)
WHERE IFNULL(MSR.Manager,ORD.Sales) = 'MA'
ORDER BY OrderId
The below query would get you the results you want, you would just have to swap out the where to be whatever manager that you were looking for, this returns what you're looking for, for MA.
SELECT Distinct A.OrderId, A.Sales, A.Region FROM ORD a
left join MSR b on (a.Sales = b.Sales OR B.Manager = A.Sales) and a.Region = B.Region
where B.Manager = 'MA' or A.Sales = 'MA'
order by orderid asc
With cte_MA as (
Select Distinct sales from mar where manager = 'MA'
Union
Select Distinct manager from mar where manager = 'MA' ),
Cte_MB as (
Select Distinct sales from mar where manager = 'MB'
Union
Select Distinct manager from mar where manager = 'MB'),
Cte_MAorder as (
Select a orderid, a.sales, a region from ord a inner cte_MA b on a.sales = b.sales),
Cte_MBorder as (
Select a.orderid, a.sales, a.region from ord a inner cte_MB b on a.sales = b.sales)
select * from cte_MAorder;
Select * from cte_MBorder;
You should run all query with to up select query if you run select * from cte_maorder query you get MA assigned salesperson order list
you run select * from cte_mborder query you get MB assigned salesperson order list
I have the following tables:
Table Offices:
OfficeID MainAddId SubAddId1 SubAddId2 PortAddId1
------- -------- -------- -------- --------
2 1 2 3 5
Table Address:
AddressID Street City ZipCode State
------- -------- -------- -------- --------
1 Forest Ave New York 10001 New York
2 Morris St Philadelphia 19019 Pennsylvania
3 David St Raleigh 27513 North Carolina
Table Port:
PortID PortName Street City ZipCode State
------- ------- -------- -------- -------- --------
5 New York Harbour Bay St New York 10001 New York
I want to write an SQL request such that if any of the addresses Id in the office table is not null,
it will return the list of addresses in a list like:
AddressID Street City ZipCode State
------- -------- -------- -------- --------
1 Forest Ave New York 10001 New York
2 Morris St Philadelphia 19019 Pennsylvania
3 David St Raleigh 27513 North Carolina
5 Bay St New York 10001 New York
Any help on how I can do this please? Thanks
Here is what I tried (partly cause it's not working):
select *
from Office offi
left join Address add1 on offi.MainAddId = add1.AddressID
left join Address add2 on offi.SubAddId1= add2.AddressID
where offi.OfficeID = 2;
However, this is returning the addresses on a single line.
Don't use multiple joins if you really need them. Instead of that, you can use OR after ON in join.
I solved using UNION:
select a.AddressID, a.street, a.city, a.zipcode, a.state
from offices o
inner join address a on (o.MainAddid = a.addressId or o.subaddid1 = a.addressid or o.subaddid2 = a.addressid)
union
select p.PortId, p.street, p.city, p.zipcode, p.state
from offices o
inner join port p on p.portId = o.PortAddId1
Check demo on DB<>FIDDLE
Try this one query :
SELECT *
FROM ( SELECT AddressID, Street, City, ZipCode, State
FROM address
UNION
SELECT PortID, Street, City, ZipCode, State
FROM Port) w
WHERE addressid IN ( SELECT Addresses
FROM ( SELECT OfficeID, MainAddId, SubAddId1, SubAddId2, PortAddId1
FROM Offices) p
UNPIVOT ( Addresses
FOR Offices IN (MainAddId, SubAddId1, SubAddId2, PortAddId1)) AS unpvt );
I have a query where you can see all the sales of a person divided in postal groups. But now I want to have the sum of all the sales per postal group.
I have the query like this:
SELECT
p.LastName,
ROW_NUMBER() OVER (PARTITION BY PostalCode
ORDER BY SUM(s.SalesYTD) DESC) AS 'Row Number',
CAST(s.SalesYTD AS INT) SalesYTD,
a.PostalCode
FROM
Sales.SalesPerson s
INNER JOIN
Person.Person p ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN
Person.Address a ON a.AddressID = p.BusinessEntityID
GROUP BY
p.LastName, a.PostalCode,s.SalesYTD;
--WHERE TerritoryID IS NOT NULL
--AND SalesYTD <> 0
But how to manage the total of each postal group?
Thank you
This is the ouput:
LastName Row Number SalesYTD PostalCode
Mitchell 1 4251369 98027
Blythe 2 3763178 98027
Carson 3 3189418 98027
Reiter 4 2315186 98027
Vargas 5 1453719 98027
Ansman-Wolfe 6 1352577 98027
Jiang 7 559698 98027
Pak 1 4116871 98055
Varkey Chudukatil 2 3121616 98055
Saraiva 3 2604541 98055
Ito 4 2458536 98055
Valdez 5 1827067 98055
Mensa-Annan 6 1576562 98055
Campbell 7 1573013 98055
Tsoflias 8 1421811 98055
Alberts 9 519906 98055
Abbas 10 172524 98055
So how to get the total of salesYTD of postal code: 98027?
Thank you
To get sales per postal code just:
SELECT PostalCode, SUM(SalesYTD) AS 'Summary sales'
FROM Sales.SalesPerson s
INNER JOIN Person.Person p ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN Person.Address a ON a.AddressID = p.BusinessEntityID
GROUP BY a.PostalCode
If you want sales of particular PostalCode you can even do it that way:
SELECT SUM(SalesYTD) AS 'Summary sales'
FROM Sales.SalesPerson s
INNER JOIN Person.Person p ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN Person.Address a ON a.AddressID = p.BusinessEntityID
WHERE PostalCode = 98027
Probably really simple but I can't quite figure it out.
Here's my SQL
select ag.product_id, COUNT(reviewer) as review_number
from PRODUCT ag
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123
group by ag.product_id
This is giving me total number of reviews made for one product as follows:
Product_id: 123
review_number: 3
but I now want the reviewers names as well like so:
Product_id: 123
review_number: 3
reviewer: Bob Smith
Product_id: 123
review_number: 3
reviewer: Peter Jones
Product_id: 123
review_number: 3
reviewer: Jane Green
How do I do this?
you could do something like this:
select
ag.product_id,
reviewer,
(select COUNT(reviewer)
from PRODUCT ag
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123) as review_number
from PRODUCT ag
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123
Try like this
select ag.product_id, COUNT(reviewer) as review_number, rr.reviewer_name
from PRODUCT ag
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123
group by ag.product_id,rr.reviewer_name
I have a "weather" table below with 3 cols:
City Temperature Date
New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005
New York 21 C 10/08/2005
Washington 20 C 10/08/2005
I want to find out the latest info on the City and Temperature in 3 cols as well (see example):
City Temperature Date
New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 17 C 10/09/2005
Can anyone help?
Find the maximum (latest) date for each city in a sub-query then join on the date and city:
select weather.*
from weather
inner join
(select city, max(date) from weather group by city) as latest
on weather.date = latest.date
and weather.city = latest.city
There are several methods. Personally, I think the following is the most expressive:
SELECT * FROM weather w1 WHERE NOT EXISTS
(SELECT * FROM weather w2 WHERE w2.city = w1.city AND w2.date > w1.date)
Option 1:
select city, temparature, date
from weather t1
where date = (select max(date)
from weather t2
where t2.city=t1.city)
Option 2:
select t1.city, t1.temp, t1.date
from weather t1
where not exists (select 1
from weather t2
where t2.date > t1.date and t1.city=t2.city)