Select a name in sql - sql

I selecting the category name from a category table using the below query
select Category.Name
from Product
inner join ProductCategory on ProductCategory.PID=Product.PID
inner join ProductMaterial on ProductMaterial.PID=Product.PID
left join Category on Category.NodeId=ProductCategory.CID
where PID in('2233','4432','5665','1252')
group by ProductCategory.CID, ProductMaterial.MID,Category.DanishName
the query is working the result of this query is
Electronics
Electronics
Electronics
Home and Garden
I want only select most number of category name, here I need only Electronics.How to get this.Thanks in advance for help...>>

Try this:
MySQL
SELECT A.name, COUNT(A.name) nameCnt
FROM (SELECT C.Name
FROM Product P
INNER JOIN ProductCategory PC ON PC.PID=P.PID
INNER JOIN ProductMaterial PM ON PM.PID=P.PID
INNER JOIN Category C ON C.NodeId=PC.CID
WHERE PID IN('2233','4432','5665','1252')
GROUP BY PC.CID, PM.MID, C.DanishName
) AS A
GROUP BY A.name
ORDER BY nameCnt DESC LIMIT 1;
SQL Server
SELECT TOP 1 A.name, COUNT(A.name) nameCnt
FROM (SELECT C.Name
FROM Product P
INNER JOIN ProductCategory PC ON PC.PID=P.PID
INNER JOIN ProductMaterial PM ON PM.PID=P.PID
INNER JOIN Category C ON C.NodeId=PC.CID
WHERE PID IN('2233','4432','5665','1252')
GROUP BY PC.CID, PM.MID, C.DanishName
) AS A
GROUP BY A.name
ORDER BY nameCnt DESC;

Related

Find the nth highest

SELECT CONCAT(C.CUSTOMER_FNAME, ' ',C.CUSTOMER_LNAME) AS FullNAME, SUM(QTY* PRICE)AS TOTAL_SPENDINGS
FROM (SELECT DISTINCT TOP 2 TOTAL_SPENDINGS) RESULT
Customers$ C INNER JOIN Invoices$ Inv ON C.CUSTOMER_ID=Inv.CUSTOMER_ID
INNER JOIN InvDetails$ InvD ON Inv.INVOICE_ID=InvD.INVOICE_ID
INNER JOIN Products$ P ON P.PRODUCT_ID=InvD.PRODUCT_ID
GROUP BY C.CUSTOMER_FNAME,C.CUSTOMER_LNAME
ORDER BY TOTAL_SPENDINGS DESC
I am trying to prin the k-highest spending customers this is what i have done until now but i get
Incorrect syntax near 'Customers$'.
You are missing the join condition between the calculated table RESULT and the Customers table:
SELECT CONCAT(C.CUSTOMER_FNAME, ' ',C.CUSTOMER_LNAME) AS FullNAME, SUM(QTY* PRICE)AS TOTAL_SPENDINGS
FROM (SELECT DISTINCT TOP 2 TOTAL_SPENDINGS) RESULT
<missing INNER / LEFT join here>
Customers$ C <missing ON here> INNER JOIN Invoices$ Inv ON C.CUSTOMER_ID=Inv.CUSTOMER_ID
INNER JOIN InvDetails$ InvD ON Inv.INVOICE_ID=InvD.INVOICE_ID
INNER JOIN Products$ P ON P.PRODUCT_ID=InvD.PRODUCT_ID
GROUP BY C.CUSTOMER_FNAME,C.CUSTOMER_LNAME
ORDER BY TOTAL_SPENDINGS DESC

SQL Select query for Select 3 different products

Good Evening,
I've been struggling with a select query on my database on which I want to determine the clients that have placed an order of 3 products from at least 3 different product categories.
Then I want to print the CustomerID and the number of the orders.
This is my Database diagram:
Any Answer would be helpful,
Sincerely Thanos.
I haven't tested this, but I believe it should be close to correct:
SELECT h.CustomerID, COUNT(DISTINCT h.SalesOrderID)
FROM SalesOrderHeader h
INNER JOIN SalesOrderDetail d1 ON h.SalesOrderID = d1.SalesOrderID
INNER JOIN SalesOrderDetail d2 ON h.SalesOrderID = d2.SalesOrderID
INNER JOIN SalesOrderDetail d3 ON h.SalesOrderID = d3.SalesOrderID
INNER JOIN Product p1 ON d1.ProductID = p1.ProductID
INNER JOIN Product p2 ON d2.ProductID = p2.ProductID
INNER JOIN Product p3 ON d3.ProductID = p3.ProductID
WHERE p1.ProductCategoryID <> p2.ProductCategoryID
AND p2.ProductCategoryID <> p3.ProductCategoryID
AND p1.ProductCategoryID <> p3.ProductCategoryID
GROUP BY h.CustomerID
select CustomerId, count(*)
from(
select CustomerId
from SalesOrderHeader soh
join SalesOrderDetail sod using (SalesOrderID)
join Product p using (ProductId)
group by CustomerId, SalesOrderId
having count(distinct ProductCategoryID) > 2
) a
group by CustomerId
I think this should get you the result
Select CustomerID,SalesOrderID , count(CategoryID) CatCount
From
SalesOrderHeader Head inner join SalesOrderDetail Det
on Head.SalesOrderID = Det.SalesOrderID
inner join Product Prod
on Prod.ProductID = Det.ProductID
inner join ProductCategory PCat
on Prod.CategoryID = PCat.CategoryID
Group By CustomerID,SalesOrderID
Having count(CategoryID) > 3
like for : "Can't solve this SQL query"
select CustomerID,count(*) as amount_of_order from
SalesOrder join
(
select SalesOrderID,count(distinct ProductCategoryID) CategoryCount
from SalesOrderDetail JOIN Product using (ProductId)
group by 1
) CatCount using (SalesOrderId)
group by 1
having bool_or(CategoryCount>=3) -- At least on CategoryCount>=3

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

I've a customer table and purchases table,
need to show cname, cid with max(customer_visits) from customer table
and sum of total_purchases by customer in purchases table.
I'm doing something like this
select p.cid, c.cname, sum(p.total_price)
from customers c where exists
(select max(visits_made) from customers having visits_made=max(visits_made)
and cid=p.cid)
inner join purchases p on p.cid=c.cid
group by p.cid,c.cname
and
select p.cid, c.cname, sum(p.total_price)
(select max(visits_made) from customers c where c.cid=p.cid)
from purchases p
inner join customers c on c.cid=p.cid
group by p.cid,c.cname
What's going wrong with these queries?
Found the solution, had to include where clause after inner join :D
I think this is just an aggregation query:
select p.cid, c.cname, sum(p.total_price) as total_price,
max(visits_made) as visits_made
from purchases p inner join
customers c
on c.cid = p.cid
group by p.cid, c.cname;

Pulling my hair with this Syntax Error

I seem to be running into a query syntax error but cannot seem to isolate it. I am using MS Access and when I run the queries I get a syntax error in FROM clause.
I have two tables and they are in a one to many relationship:
Table 1 called (customer) with the following fields:
ID
FirstName
Table 2 called (tblservice) with the following fields:
serviceID
Timing
Total
customerID <-Foreign Key
First Query:
select c.id, c.firstname, avg(s.Total) / (select count(id) from customer) as LifetimeValue
from tblservice as s join customer as c on s.id = c.id
group by s.id
Second Query(30 day span):
select c.id, c.firstname, avg(s.Total) / (select count(id) from customer) as LifetimeValue
from tblservice as s join customer as c on s.id = c.id
where (s.Timing)>=DateAdd("d",-30,Date())
group by s.id
Try this:
select c.id, c.firstname, avg(s.Total) / count(c.id) as LifetimeValue
from tblservice as s inner join customer as c on s.id = c.id
group by c.id, c.firstname
and
select c.id, c.firstname, avg(s.Total) / count(c.id) as LifetimeValue
from tblservice as s inner join customer as c on s.id = c.id
where (s.Timing)>=DateAdd("d",-30,Date())
group by c.id, c.firstname
You cannot select c.id and c.firstname unless you group by them. And you can use count(c.id) since you are already grouping by c.id. I have not used SQL in MS Access though. So I am not 100% sure. Try it out.
MS Access may require you to use INNER JOIN instead of just JOIN.

How to calculate a total of values from other tables in a column of the select statement

I have three tables:
CustOrder: id, CreateDate, Status
DenominationOrder: id, DenID, OrderID
Denomination: id, amount
I want to create a view based upon all these tables but there should be an additional column i.e. Total should be there which can calculate the sum of the amount of each order.
e.g.
order 1 total denominations 3, total amount = 250+250+250=750
order 2 total denominations 2, total amount = 250+250=500
Is it possible?
I try to guess your table relations (and data too, you did not provide any sample):
SELECT co.id,
COUNT(do.DenID) AS `Total denominations`,
SUM(d.amount) AS `Total amount`
FROM CustOrder co
INNER JOIN DenominationOrder do ON co.id = do.OrderId
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY co.id
Try this:
SELECT o.CreateDate, COUNT(o.id), SUM(d.amount) AS 'Total Amount'
FROM CustOrder o
INNER JOIN DenominationOrder do ON o.id = do.OrderID
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY o.CreateDate
DEMO
Another way to do this, by using CTE, like this:
;WITH CustomersTotalOrders
AS
(
SELECT o.id, SUM(d.amount) AS 'TotalAmount'
FROM CustOrder o
INNER JOIN DenominationOrder do ON o.id = do.OrderID
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY o.id
)
SELECT o.id, COUNT(ot.id) AS 'Orders Count', ot.TotalAmount
FROM CustOrder o
INNER JOIN CustomersTotalOrders ot on o.id = ot.id
INNER JOIN DenominationOrder do ON ot.id = do.OrderID
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY o.id, ot.TotalAmount
This will give you:
id | Orders Count | Total Amount
-------+---------------+-------------
1 3 750
2 2 500
DEMO using CTE