I'm getting a strange error with the below code. No column was specified for column 2 of 'no1'.
As far as I can tell it should work. I'm simply trying to get a collection of details about customers where the customers exist more than once on the receipts tbl.
SELECT
CM.ClientID,
CPN.Birthdate,
CM.ClientPassword
FROM
dbo.ClientMaster AS CM
JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
WHERE
CM.ClientID IN (
SELECT
no1.ClientID
FROM
(
SELECT
CM.ClientID,
COUNT(*)
FROM
dbo.ClientMaster AS CM
JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
WHERE
CP.PolicyNo IN (SELECT PolicyNo FROM Receipts)
AND CM.ClientID IS NOT NULL
AND Birthdate IS NOT NULL
AND ClientPassword IS NOT NULL
GROUP BY
CM.ClientID
HAVING
COUNT(*)>1
) AS no1
)
SOLUTION
Ah did not realise you don't need the Count(*) in the Select. Thanks guys!
This is what I ended up going with.
SELECT DISTINCT
CM.ClientID,
CPN.Birthdate,
CM.ClientPassword
FROM
dbo.ClientMaster AS CM
JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
WHERE
CM.ClientID IN (
SELECT
ClientID
FROM
Receipts
GROUP BY
ClientID
HAVING
COUNT(*)>1
)
AND CM.ClientID IS NOT NULL
AND Birthdate IS NOT NULL
AND ClientPassword IS NOT NULL
Your COUNT(*) column needs a name, like:
COUNT(*) AS RecordCount
You don't need to nest the same query twice for that. You can do this instead:
SELECT
CM.ClientID,
CPN.Birthdate,
CM.ClientPassword
FROM
dbo.ClientMaster AS CM
JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
GROUP BY CM.ClientID, CPN.Birthdate, CM.ClientPassword
HAVING COUNT(CM.ClientID) > 1
Related
I have separate queries with me as follows:
SELECT c.id
FROM claim c,
company co,
customer cu
WHERE c.company_id = co.id
AND c.customer_id = cu.id
AND co.company_code = 'LTO'
AND cu.customer_no = '021540'
AND c.invoice_number IS NOT NULL
AND c.invoice_date IS NULL
AND c.invoice_number = '20170331'
SELECT Sum(price)
FROM replaced_part
WHERE claim_id IN ( 628099, 674047, 1182523, 1282549,
1479834, 1480585, 1487452, 1515238 );
SELECT Sum(price)
FROM allowance
WHERE claim_id IN ( 628099, 674047, 1182523, 1282549,
1479834, 1480585, 1487452, 1515238 );
I was trying to group together all the above queries into one using sql equi join as follows:
select co.company_Code,
cu.customer_No,
c.invoice_Number,
sum(r.price),
sum(a.price)
FROM claim c INNER JOIN company co ON c.COMPANY_ID=co.ID
INNER JOIN customer cu ON c.CUSTOMER_ID=cu.ID
INNER JOIN replaced_part r ON r.claim_id=c.id
INNER JOIN allowance a ON a.claim_id = c.id
WHERE co.company_code = 'LTO' and cu.customer_no='021540' and
c.INVOICE_NUMBER is not null and c.INVOICE_DATE is null and
c.INVOICE_NUMBER='20170331'
GROUP BY co.company_Code, cu.customer_No, c.invoice_Number
But I am not getting the desired result(Query execute successfully but the sum is incorrect) as I am getting after running the 3 separate queries defined above... What is the problem here in my query created using equi join??
Relationship between tables are claim ->(one to one) -> company ->(one to one) -> customer ->(one to many) ->Replaced_Part ->(one to many) -> allowance
Please try to use left join for Replaced_Part and allowance
select co.company_Code
, cu.customer_No
, c.invoice_Number
, (SELECT sum(r.price) FROM replaced_part r WHERE r.claim_id = c.id)
, (SELECT sum(a.price) FROM allowance a WHERE a.claim_id = c.id)
FROM claim c
INNER JOIN company co
ON c.COMPANY_ID=co.ID
INNER JOIN customer cu
ON c.CUSTOMER_ID=cu.ID
WHERE co.company_code = 'LTO'
AND cu.customer_no='021540'
AND c.INVOICE_DATE is null
AND c.INVOICE_NUMBER='20170331'
GROUP BY co.company_Code
, cu.customer_No
, c.invoice_Number
try to use temporary table, combine all the tables and make a temporary table, then fire your conditions on that table.
Example :
select co.company_Code,
cu.customer_No,
c.invoice_Number,
sum(r.price),
sum(a.price)
into #temp from (claim c
INNER JOIN company co ON c.COMPANY_ID=co.ID
INNER JOIN customer cu ON c.CUSTOMER_ID=cu.ID
INNER JOIN replaced_part r ON r.claim_id=c.id
INNER JOIN allowance a ON a.claim_id = c.id
and then use your conditions on #temp. like
select * from #temp
WHERE company_code = 'LTO' and customer_no='021540' and
INVOICE_NUMBER is not null and INVOICE_DATE is null and
INVOICE_NUMBER='20170331'
GROUP BY company_Code, customer_No, invoice_Number
Hope this will work for you.
i have these tables:
tblCountry - country_number , country_name
tblDivingClub - club_number , country_number(FK)
tblDiver - diver_number, diver_name, startWorkingDate, endrtWorkingDate.
tblWorks_for -diver_number(FK), club_number(FK)
i need to write a query, without views or temp tables, that will diplay a lits of countries with the fields: country_number , country_name, and the number of diving clubs in that country which have 25 or more working divers at the moment (end working date IS NULL).
thats the code i wrote so far:
SELECT country_number, country_name,
( SELECT count(distinct tblDivingClub.number)
FROM tblDivingClub
inner join tblCountry on tblDivingClub.country = tblCountry.country_number
WHERE (
( SELECT count(tblWorks_for.diver_number)
FROM tblWorks_for
INNER JOIN tblDivingClub on tblWorks_for.club_number = tblDivingClub.number
WHERE tblWorks_for.end_working_date IS null
and tblDivingClub.number = tblWorks_for.club_number) >25)
) as number_of_clubs
FROM tblCountry
INNER JOIN tblDivingClub on tblCountry.country_number = tblDivingClub.country
WHERE tblCountry.country_number = tblDivingClub.country
GROUP by country_number, country_name
It's a good thing SQL anticipates the need for aggregate conditionals - if you suspected there must be a much easier way, you were right. I think you want something like:
SELECT country_number, country_name, count(distinct tblDivingClub.number)
FROM tblCountry
INNER JOIN tblDivingClub on tblCountry.country_number = tblDivingClub.country
WHERE tblCountry.country_number = tblDivingClub.country
GROUP by country_number, country_name
HAVING count(case when tblWorks_for.end_working_date IS null then 1 else null end) > 25
Try something like this:
SELECT country_number, country_name, COUNT(DISTINCT club_number)
FROM
(
SELECT tc.country_number, tc.country_name, tdc.club_number, count(*) as tot_cnt
FROM tblCountry tc
INNER JOIN tblDivingClub AS tdc ON tdc.country_number = tc.country_number
INNER JOIN tblWorks_for AS tw ON tw.club_number = tdc.club_number
INNER JOIN tblDiver AS td ON td.diver_number = tw.diver_number
WHERE endrtWorkingDate IS NULL
GROUP BY tc.country_number, tc.country_name, tdc.club_number
HAVING count(*) >= 25
) as der
GROUP BY country_number, country_name;
Having trouble with this one. Getting syntax error at the arrow. I'm using subqueries on purpose. I'm wondering if it's possible to mix 'where' and 'having'?
;with books_not_ordered as
(
select BK.book_id
from bkinfo.books BK
where BK.book_id not in
(
select OD.book_id
from bkorders.order_details OD
)
)
select AU.author_id, AU.author_name_last
from bkinfo.authors AU
where exists
(
select BAU.author_id, count(*) as NumBooks
from bkinfo.book_authors BAU
group by BAU.author_id
having count(*) > 1
==>where AU.author_id = BAU.author_id
and
BAU.book_id in
(
select BK.book_id
from bkinfo.books BK
where BK.book_id in
(
select cte.book_id
from books_not_ordered cte
)
)
)
;
go
The where clause should come before your group by and having
select BAU.author_id, count(*) as NumBooks
from bkinfo.book_authors BAU
where AU.author_id = BAU.author_id
group by BAU.author_id
having count(*) > 1
Assuming that you are trying to get "The authors of the books that are not in order_details table, but who have written more than one book". The following query should work.
SELECT AU.author_id, AU.author_name_last
FROM bkinfo.books BK
JOIN bkinfo.book_authors BAU ON BAU.author_id = BK.author_id
JOIN bkinfo.authors AU ON BAU.author_id = AU.author_id
LEFT OUTER JOIN bkorders.order_details OD ON BK.book_id = OD.book_id
WHERE OD.book_id IS NULL
GROUP BY AU.author_id, AU.author_name_last
HAVING COUNT(BK.Book_id) > 1
Sigh ... can anyone help? In the SQL query below, the results I get are incorrect. There are three (3) labor records in [LaborDetail]
Hours / Cost
2.75 / 50.88
2.00 / 74.00
1.25 / 34.69
There are two (2) material records in [WorkOrderInventory]
Material Cost
42.75
35.94
The issue is that the query incorrectly returns the following:
sFunction cntWO sumLaborHours sumLaborCost sumMaterialCost
ROBOT HARNESS 1 12 319.14 236.07
What am I doing wrong in the query that is causing the sums to be multiplied? The correct values are sumLaborHours = 6, sumLaborCost = 159.57, and sumMaterialCost = 78.69. Thank you for your help.
SELECT CASE WHEN COALESCE(work_orders.location, Work_Orders_Archived.location) IS NULL
THEN '' ELSE COALESCE(work_orders.location, Work_Orders_Archived.location) END AS sFunction,
(SELECT COUNT(*)
FROM work_orders
FULL OUTER JOIN Work_Orders_Archived
ON work_orders.order_number = Work_Orders_Archived.order_number
WHERE COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = '919630') AS cntWO,
SUM(Laborhours) AS sumLaborHours,
SUM(LaborCost) AS sumLaborCost,
SUM(MaterialCost*MaterialQuanity) AS sumMaterialCost
FROM work_orders
FULL OUTER JOIN Work_Orders_Archived
ON work_orders.order_number = Work_Orders_Archived.order_number
LEFT OUTER JOIN
(SELECT HoursWorked AS Laborhours, TotalDollars AS LaborCost, WorkOrderNo
FROM LaborDetail) AS LD
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = LD.WorkOrderNo
LEFT OUTER JOIN
(SELECT UnitCost AS MaterialCost, Qty AS MaterialQuanity, OrderNumber
FROM WorkOrderInventory) AS WOI
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = WOI.OrderNumber
WHERE COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = '919630'
GROUP BY CASE WHEN COALESCE(work_orders.location, Work_Orders_Archived.location) IS NULL
THEN '' ELSE COALESCE(work_orders.location, Work_Orders_Archived.location) END
ORDER BY sFunction
Try using the SUM function inside a derived table subquery when doing the full join to "WorkOrderInventory" like so...
select
...
sum(hrs) as sumlaborhrs,
sum(cost) as sumlaborcost,
-- calculate material cost in subquery
summaterialcost
from labordetail a
full outer join
(select ordernumber, sum(materialcost) as summaterialcost
from WorkOrderInventory
group by ordernumber
) b on a.workorderno = b.ordernumber
i created a simple sql fiddle to demonstrate this (i simplified your query for examples sake)
Looks to me that work_orders and work_orders_archived contains the same thing and you need both tables as if they were one table. So you could instead of joining create a UNION and use it as if it was one table:
select location as sfunction
from
(select location
from work_orders
union location
from work_orders_archived)
Then you use it to join the rest. What DBMS are you on? You could use WITH. But this does not exist on MYSQL.
with wo as
(select location as sfunction, order_number
from work_orders
union location, order_number
from work_orders_archived)
select sfunction,
count(*)
SUM(Laborhours) AS sumLaborHours,
SUM(LaborCost) AS sumLaborCost,
SUM(MaterialCost*MaterialQuanity) AS sumMaterialCost
from wo
LEFT OUTER JOIN
(SELECT HoursWorked AS Laborhours, TotalDollars AS LaborCost, WorkOrderNo
FROM LaborDetail) AS LD
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = LD.WorkOrderNo
LEFT OUTER JOIN
(SELECT UnitCost AS MaterialCost, Qty AS MaterialQuanity, OrderNumber
FROM WorkOrderInventory) AS WOI
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = WOI.OrderNumber
where wo.order_number = '919630'
group by sfunction
order by sfunction
The best guess is that the work orders appear more than once in one of the tables. Try these queries to check for duplicates in the two most obvious candidate tables:
select cnt, COUNT(*), MIN(order_number), MAX(order_number)
from (select order_number, COUNT(*) as cnt
from work_orders
group by order_number
) t
group by cnt
order by 1;
select cnt, COUNT(*), MIN(order_number), MAX(order_number)
from (select order_number, COUNT(*) as cnt
from work_orders_archived
group by order_number
) t
group by cnt
order by 1;
If either returns a row where cnt is not 1, then you have duplicates in the tables.
How can I make a count to return also the values with 0 in it.
Example:
select count(1), equipment_name
from alarms.new_alarms
where equipment_name in (
select eqp from ne_db.ne_list)
Group by equipment_name
It is returning only the counts with values higher than 0 , but I need to know the records that are not returning anything.
Any help is greatly appreciated.
thanks,
Marco
Try using LEFT JOIN,
SELECT a.eqp, COUNT(b.equipment_name) totalCount
FROM ne_db.ne_list a
LEFT JOIN alarms.new_alarms b
ON a.eqp = b.equipment_name
GROUP BY a.eqp
If the table ne_list has no duplicates, then you can do a left join. That assumption may not be true, so the safest way to convert this is by removing duplicates in a subquery:
select count(1), ne.equipment_name
from alarms.new_alarms ne left outer join
(select distinct eqp
from ne_db.ne_list
) eqp
on ne.equipment_name = eqp.eqp
Group by ne.equipment_name
You could use a left join:
select ne.equipment_name
, count(na.equipment_name)
from ne_db.ne_list ne
left join
alarms.new_alarms na
on ne.eqp = na.equipment_name
group by
ne.equipment_name
This should work too
(select equipment_name, count(*) as cnt from alarms.new_alarms
where equipment_name in (
select eqp from ne_db.ne_list
) group by equipment_name
)union(
select equipment_name, 0 as cnt from alarms.new_alarms
where equipment_name not in (
select eqp from ne_db.ne_list
) group by equipment_name
) order by equipment_name