Row value from another table - sql

I have a table that is having 2 duplicate rows (total of 3 rows), so I used the code below to get the duplicate value in the column
SELECT CustNo, COUNT(*) TotalCount
FROM Rental
GROUP BY CustNo
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
So once I get the repeated value, I need to get the CustNo derived as duplicate from the customer table. How do I go about taking this value and using it in the select statment all in the same query.
I also have the select statement prepared like this.
Select * from Customer where CustNo = 'T0002';
Thanks.

Select * from Customer
where CustNo IN
(
SELECT CustNo
FROM Rental
GROUP BY CustNo
HAVING COUNT(*) > 1
)

You can use join:
SELECT c.*
FROM (SELECT CustNo, COUNT(*) TotalCount
FROM Rental
GROUP BY CustNo
HAVING COUNT(*) > 1
) cc JOIN
Customer c
on cc.CustNo = c.CustNo;

Select C.* from Customer C RIGHT JOIN (
SELECT CustNo
FROM Rental
GROUP BY CustNo
HAVING COUNT(*) > 1) D
ON C.CustNo = D.CustNo

You can also try this,
With tblDups as(
select CustNo,count(CustNo) as TotalCount from a_rental
Group by CustNo
Having count(CustNo) >1)
select b.* from a_rental b
inner join tblDups a on a.CustNo = b.Custno

Related

Hive sql find out how many common customer in each country

I have a table called custtable, have 3 columns custid,country,date
there are 5 countrise in country: 'CH','US', 'UK','FR' and 'GE'
I hope have elegent query to find out how many unique [custid] in 5 countrise.
currently, I can use subquery and temporary table to find the overlapping set, but any suggestions for a more simple way.
here is my way to find out the overlapping for 2 countries and then I need to do another subquery
with t1 AS
(SELECT DISTINCT [custid]
FROM custtable
   where date>20140101
and country='CH'),
t2 as
(SELECT DISTINCT [custid]
FROM custtable
   where date>20140101
and country='FR'),
t3 AS
(SELECT DISTINCT [custid]
FROM custtable
   where date>20140101
and country='US'),
t4 as
(SELECT DISTINCT [custid]
FROM custtable
   where date>20140101
and country='UK')
select count (distinct t1.custid)
from t1
inner join t3
on (t1.custid=t3.custid)
inner join t2
on (t1.custid=t2.custid)
inner join t4
on (t1.custid=t4.custid)
     
thank you for any input
I think a better way is to count how many distinct countries each custid has and filter count >= 5, e.g.,
with count_table as (
select custid, count(distinct country) as cnt
from custtable
where date>20140101
)
select custid, cnt
from count_table
where cnt >= 5
then count your cusid
SELECT COUNTRY
, COUNT(DISTINCT CUSTID) AS CNT
FROM CUSTTABLE
GROUP BY COUNTRY
If you want customers in all five countries:
select custid
from custtable
where date > 20140101
group by custid
having count(distinct country) = 5;
If you want those particular five countries (as your query suggests):
select custid
from custtable
where date > 20140101 and
country in ('CH','US', 'UK','FR', 'GE')
group by custid
having count(distinct country) = 5;

get sum of columns in next column sql

<table border="1">
<tr><th>Pcode</th><th>TCode</th><th>TName</th><th>CCode</th><th>Rate</th><th>Total=sum(rates of all PCodes)</th></tr>
<b><tr><td>12345</td><td>200</td><td>200</td><td>12</td><td>2000</td><td>8000</td></tr>
<tr><td>12345</td><td>201</td><td>200</td><td>12</td><td>1000</td><td>8000</td></tr>
<tr><td>12345</td><td>202</td><td>200</td><td>12</td><td>2000</td><td>8000</td></tr>
<tr><td>12345</td><td>205</td><td>200</td><td>12</td><td>3000</td><td>8000</td></tr>
<tr><td>12346</td><td>200</td><td>200</td><td>12</td><td>4000</td><td>32000</td></tr>
<tr><td>12346</td><td>204</td><td>200</td><td>12</td><td>5000</td><td>32000</td></tr>
<tr><td>12346</td><td>208</td><td>200</td><td>12</td><td>5000</td><td>32000</td></tr>
<tr><td>12346</td><td>1235</td><td>200</td><td>12</td><td>3000</td><td>32000</td></tr>
<tr><td>12346</td><td>12</td><td>200</td><td>12</td><td>7000</td><td>32000</td></tr>
<tr><td>12346</td><td>100</td><td>200</td><td>12</td><td>8000</td><td>32000</td></tr>
</table>
<p>I have a table with Pcode, Tcode, TName, CCode, Rate. Need to find the sum under Pcode as Total in next column</p>
I want this table column '(Total)' to be fetched along with all columns in the below table(SQL).
I have tried ROLLUP and group by no solution yet.
the total to be group by Pcode and get that sum to the next column.
TIA,
You only need to use GROUP BY. This solution uses a Common Table Expression (replace tempTable with the name of your table):
;WITH TotalByPCode (PCode, Total) AS (
SELECT PCode, SUM(Rate) FROM tempTable GROUP BY PCode
)
SELECT R.PCode, R.TCode, R.TName, R.CCode, R.Rate, T.Total
FROM tempTable R JOIN TotalByPCode T ON R.PCode = T.PCode
;
With your_table_CTE as (
select C.x,A.y,B.x from DFG inner join A, inner join B, inner join C
)
select a.pcode, a.TCode, a.TName, a.CCode, a.Rate, b.Total
from your_table_CTE a
join (
select pcode, sum(rate) as Total
from your_table_CTE
group by pcode
) b
on a.pcode = b.pcode

Unique Customers with Duplicate Order Numbers

It seems a dataset I've been given has multiple customer IDs with the same Order Numbers. How would I go about identifying those customer who have been effected?
I did try the usual but then realised some Order Numbers are duplicated but with the same Cus ID?
SELECT Cus ID, Ord Num, COUNT(*)
FROM OUTPUT
GROUP BY Cus ID, Order Num, Amount
HAVING COUNT(*) > 1
You can find the orders using a subquery:
SELECT OrdNum, COUNT(DISTINCT CustId)
FROM OUTPUT
GROUP BY OrdNum
HAVING COUNT(DISTINCT CustId) > 1
You can get the details by joining the information back in:
SELECT o.*
FROM (SELECT OrdNum, COUNT(DISTINCT CustId)
FROM OUTPUT
GROUP BY OrdNum
HAVING COUNT(DISTINCT CustId) > 1
) as oc INNER JOIN
OUTPUT as o
on oc.OrdNum = o.OrdNum
EDIT:
In Access, you can do:
SELECT o.*
FROM (SELECT OrdNum
FROM OUTPUT
GROUP BY OrdNum
HAVING MIN(CustId) <> MAX(CustId)
) as oc INNER JOIN
OUTPUT as o
on oc.OrdNum = o.OrdNum;

sql query with two tables having reference key

I have two tables:
Discounts(disid primary key)
Cust(custid primary key, disid ref discount(disid))
Now I need a query to get custid having all disid(discount coupons) and the customer may contain the same disid more than once.
select custid, count(distinct disid) from cust
group by custid
having count(*) = (select count(*) from discounts)
SELECT COUNT(DISTINCT D.disid) FROM CUST C
INNER JOIN DISCOUNTS D ON D.disid=C.disid GROUP BY D.disid
try either of this solutions:
SELECT a.custid, COUNT(a.disid) totalCoupon
FROM cust a
INNER JOIN discounts b
ON b.disid = a.disid
GROUP BY a.custid
or
SELECT a.custid, COUNT(a.disid) totalCoupon
FROM cust a
INNER JOIN discounts b
ON b.disid = a.disid
GROUP BY a.custid
HAVING COUNT(a.disid) > 1 -- customers having the same (but more than 1)
-- CouponID will only be shown here

mysql query with double join

I have 3 tables, but I can only get to join another table count. See below.
The one below works like a charm, but I need to add another "count" from another table.
there is a 3rd table called "ci_nomatch" and contains a reference to ci_address_book.reference
which could have multiple entries (many on many) but I only need the count of that table.
so if ci_address_book would have an entries called "item1","item 2","item3"
and ci_nomatch would have "1,item1,user1","2,item1,user4"
I would like to have returned "2" for Item1 on the query.
Any ideas? I tried another join, but it tells me that the reference does not exist, while it does!
SELECT c.*, IFNULL(p.total, 0) AS matchcount
FROM ci_address_book c
LEFT JOIN (
SELECT addressbook_id, COUNT(match_id) AS total
FROM ci_matched_sanctions
GROUP BY addressbook_id
) AS p
ON c.id=p.addressbook_id
ORDER BY matchcount DESC
LIMIT 0,15
You could subquery it directly in the select
SELECT c.*, IFNULL(p.total, 0) AS matchcount,
(SELECT COUNT(*) FROM ci_nomatch n on n.reference = c.reference) AS othercount
FROM ci_address_book c
LEFT JOIN (
SELECT addressbook_id, COUNT(match_id) AS total
FROM ci_matched_sanctions
GROUP BY addressbook_id
) AS p
ON c.id=p.addressbook_id
ORDER BY matchcount DESC
LIMIT 0,15
#updated for comment. Including an extra column "(matchcount - othercount) AS deducted" would be best done by sub-querying.
SELECT *, matchcount - othercount AS deducted
FROM
(
SELECT c.* , IFNULL( p.total, 0 ) AS matchcount, (
SELECT COUNT( * ) FROM ci_falsepositives n
WHERE n.addressbook_id = c.reference ) AS othercount
FROM ci_address_book c
LEFT JOIN (
SELECT addressbook_id, COUNT( match_id ) AS total
FROM ci_matched_sanctions GROUP BY addressbook_id ) AS p
ON c.id = p.addressbook_id ORDER BY matchcount DESC LIMIT 0 , 15
) S