Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed, gen.qty_seed, gen.price
FROM (SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod FROM gen GROUP BY hhid, country) AS temp, gen
WHERE (((temp.hhid)=gen.hhid) And ((temp.country)=gen.country) And ((temp.max_prod)=gen.total_prod) And ((temp.max_area)=gen.area))
ORDER BY temp.hhid;
Why do some of the results were not seen?
I have atleast 100 hhid , each one has 3 area , 3 productions , quantity of seed , price etc ...
all of the hhid was shown in the ouput query except for 1 hhid ,
what might be wrong ?
The problem is that the maximum of prod and the maximum of area are rarely in the same row.
You should also learn to use explicit join syntax. A simple rule: never use a comma in the from clause.
This may be what you want:
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed,
gen.qty_seed, gen.price
FROM gen INNER JOIN
(SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod
FROM gen
GROUP BY hhid, country
) AS temp
ON temp.hhid = gen.hhid AND temp.country = gen.country
WHERE (temp.max_prod = gen.total_prod or temp.max_area = gen.area)
ORDER BY temp.hhid;
I left the WHERE clause in, because MS Access has strange restrictions on joins. Logically it should go in the ON clause, but I'm not 100% sure that Access accepts that syntax.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I need to combine the results from two different CTEs into a single table. Hence, I have written the following SQL query wherein I attempted to create a left join on two different fields (patient_id, quarter in example below).
Unfortunately, I am getting a SQL compilation error (Unknown user defined function A.YR_QTR). But I have declared this variable in the code so am at a loss to why this error is cropping up. My code follows below:
WITH op_clms AS
(
SELECT
MC.UNIV_MBR_ID AS univ_mbr_id,
MC.YR_QTR AS YR_QTR,
COUNT DISTINCT MC.ENC_KEY AS op_clms
FROM
CRF.MED_CLAIMS AS MC
WHERE
MC.SERVICE BETWEEN '2016-01-01' AND '2020-12-31'
AND PLACE_OF_SERVICE ! = '23'
),
ed_clms AS
(
SELECT
MC.UNIV_MBR_ID AS univ_mbr_id,
MC.YR_QTR AS YR_QTR,
COUNT DISTINCT MC.ENC_KEY AS ed_clms
FROM
CRF.MED_CLAIMS AS MC
WHERE
MC.SERVICE BETWEEN '2016-01-01' AND '2020-12-31'
AND PLACE_OF_SERVICE = '23'
)
SELECT
a.UNIV_MBR_ID, a.YR_QTR
(IFNULL(op_clms, 0)) AS op_clms,
(IFNULL(ed_clms,0)) AS ed_clms,
1 as cohort
FROM
(SELECT UNIV_MBR_ID, YR_QTR
FROM op_clms
UNION
SELECT UNIV_MBR_ID, YR_QTR
FROM ed_clms) a
LEFT JOIN
op_clms ON a.UNIV_MBR_ID = op_clms.UNIV_MBR_ID
AND a.YR_QTR = op_clms.YR_QTR
LEFT JOIN
ed_clms ON a.UNIV_MBR_ID = ed_clms.UNIV_MBR_ID
AND a.YR_QTR = ed_clms.YR_QTR
Can somebody please guide me on what I am doing wrong? Each of the individual CTEs check out fine on their own. And I cant find anything else syntactically/logically wrong with my code.
It appears in my original code, I inadvertently omitted a comma after a.YR_QTR. Adding that as shown below, fixes the issue!
a.UNIV_MBR_ID, a.YR_QTR,
(IFNULL(op_clms, 0)) AS op_clms,
(IFNULL(ed_clms,0)) AS ed_clms,
1 as cohort
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Junior here with no one to help me but the void of the internet and my googling skills are mediocre at best.
The following syntax returns the information I need however, it's giving me ALL dates in the table and ignoring the WHERE clause. There are 3 tables I'm trying to Join: 'ProductHistory' is the main one, with one column needed from 'Product' and a date field from the third table 'MainJobDetails'.
SELECT [ProductHistory].[Type]
,[ProductHistory].[Ref]
,[ProductHistory].[JobNo]
,[ProductHistory].[Quantity]
,[ProductHistory].[PurchasePrice]
,[ProductHistory].[UnitPrice]
,[ProductHistory].[UnitDesc]
,[ProductHistory].[ProductID]
,[ProductHistory].[Location]
,[ProductHistory].[UserID]
,[Product].[Description]
,[Product].[StkRef1]
,[MainJobDetails].[DespatchDate]
FROM [ProductHistory]
JOIN [Product] ON [ProductHistory].[ProductID] = [Product].[ID]
JOIN [MainJobDetails] ON [ProductHistory].[JobNo] = [MainJobDetails].[JobNo]
WHERE YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND [ProductHistory].[Type] = 5
OR [ProductHistory].[Type] = 6
ORDER BY [MainJobDetails].[DespatchDate] DESC
I've tried changing the WHERE clause to:
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/10/31' AND '2020/11/30'
But it made no difference.
This is another similar query I've used previously and it works fine:
SELECT [MainJobDetails].[JobNo]
,[MainJobDetails].[EstimateHeaderRef]
,[MainJobDetails].[InvoiceCustomerName]
,[MainJobDetails].[JobDesc]
,[MainJobDetails].[DespatchDate]
,[FinishingInput].[Code]
,[FinishingInput].[Description]
,[FinishingInput].[Runs]
,[FinishingInput].[Timedb]
FROM [MainJobDetails]
LEFT JOIN [FinishingInput]
ON [MainJobDetails].[EstimateHeaderRef]=[FinishingInput].[EstimateHeaderRef]
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/11/01' AND '2020/12/31'
ORDER BY [MainJobDetails].[DespatchDate] DESC
What am I getting wrong in the first statement?
Many thanks in advance for your help.
Put the OR condition within parentheses:
WHERE
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5 OR [ProductHistory].[Type] = 6)
--^-- here and here --^--
Why you need that is because OR has lower priority than AND in logical expressions. So your original code (without the parentheses) is equivalent to:
WHERE
(
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5
)
OR [ProductHistory].[Type] = 6
You can see that this allows any product with Type 6, regardless of other conditions.
I would also suggest further optimizations:
use direct filtering on the date rather than applying date functions no the stored value - this is much more efficient
use IN instead of ORed conditions
So:
WHERE
[MainJobDetails].[DespatchDate] >= '20201101'
AND [MainJobDetails].[DespatchDate] < '20201201'
AND [ProductHistory].[Type] IN (5, 6)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Tried to change my code many times, need help with these errors, please!
Result: ambiguous column name: main.Guides.Guide_ID
SELECT *
FROM Guides
INNER JOIN Guides ON Guides_Countries.Guide_ID = Guides_Countries.Guide_ID
INNER JOIN Countries ON Countries.Country_ID = Guides_Countries.Country_ID
INNER JOIN Guides_Languages ON Guides.Guide_ID = Guides_Languages.Guide_ID
INNER JOIN Languages ON Languages.Language_ID = Guides_Languages.Language_ID
WHERE Countries.Name="Kazakhstan" AND (Languages.Name="German" OR Languages.Name="English") AND Guides.Guide_ID NOT IN
(SELECT Guide_ID
FROM GuidesUnavailableDates
INNER JOIN GuidesUnavailableDates ON GuidesUnavailableDates.UnDate_G_ID=Guides_UnDate.UnDate_G_ID
WHERE (Start_date<="21/06/2020" and End_date>="21/06/2020")
OR (Start_date<="30/06/2020" and End_date>="30/06/2020")
OR (Start_date>="21/06/2020" and End_date<="30/06/2020")
)
;
You have multiple table names appearing multiple times in your FROM clauses. That is causing your problem. I think you want:
SELECT *
FROM Guides g JOIN
Guides_Countries gc
ON gc.Guide_ID = g.Guide_ID JOIN
Countries c
ON c.Country_ID = gc.Country_ID JOIN
Guides_Languages gl
ON g.Guide_ID = gl.Guide_ID JOIN
Languages l
ON l.Language_ID = gl.Language_ID
WHERE c.Name = 'Kazakhstan' AND
l.Name IN ('German', 'English') AND
g.Guide_ID NOT IN (SELECT gud.Guide_ID
FROM GuidesUnavailableDates gud
WHERE gud.Start_date <= '2020-06-30' AND
gud.End_date >= '2020-06-21'
);
Notes:
Guide_Countries is not in your FROM list although Guides is there twice.
IN is much simpler than mutiple ORs.
All columns are qualified so it is clear what table they are coming from.
All tables have simple table aliases which are abbreviations for the table names.
There is no need for a JOIN in the subquery.
Use well formatted dates.
I am guessing that the weird date logic is to find an overlap with the time period mentioned so I simplified the logic. (You haven't explained the logic, so this is a guess and your original code doesn't make sense.)
I strongly recommend using NOT EXISTS with subqueries rather than NOT IN. However, I did not change the code here (mostly because I can't really tell what the subquery is supposed to be doing).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i am using below query to pick User highest qualification but i get repeated values. because user have more than one qualifications.
SELECT hrQualifications.Qualification,hrUserQualifications.HRUserID,MAX(hrQualifications.QualificationLevel) as Qlevel
FROM hrQualifications RIGHT OUTER JOIN
hrUserQualifications ON hrQualifications.QualificationID = hrUserQualifications.QualificationID RIGHT OUTER JOIN
hrUserApplyforPositions ON hrUserQualifications.HRUserID = hrUserApplyforPositions.HRUserID
WHERE (hrUserApplyforPositions.HrPositionID = 1)
group by hrQualifications.Qualification,hrUserQualifications.HRUserID
output which i got
Qualification UserID QualificationLevel
B.Sc.(Hons) 12 16
F.Sc 12 12
B.Sc.(Hons) 18 16
require output. i want highest qualification of user.
Qualification UserID QualificationLevel
B.Sc.(Hons) 12 16
B.Sc.(Hons) 18 16
A good way to do what you want is to use row_number(). This adds a sequential number to rows, starting over again within a partition and ordered by another field.
For your query:
with t as (
SELECT q.Qualification, uq.HRUserID, q.QualificationLevel as Qlevel,
row_number() over (partition by uq.HRUserID
order by q.QualificationLevel desc
) seqnum
FROM hrQualifications q RIGHT OUTER JOIN
hrUserQualifications uq
ON q.QualificationID = uq.QualificationID RIGHT OUTER JOIN
hrUserApplyforPositions uap
ON uq.HRUserID = uap.HRUserID
WHERE uap.HrPositionID = 1
)
select *
from t
where seqnum = 1;
Note that I also added table aliases to make the query more readable.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The following snippet is a join on multiple tables. I need to display all order number, customer name, product name, price and quantity ordered from anyone from Australia. I'm getting table headings but no rows. Is there something wrong with this?
SELECT
"order".orderno AS ord,
customer.cname,
product.prodname,
customer.country_code,
orderdetl.price,
orderdetl.qty,
country.country_code
FROM
prema01.country,
prema01.customer,
prema01."order",
prema01.orderdetl,
prema01.product
WHERE
customer.country_code = 'AUS'
I've changed the code, verified there is data in the tables and it still comes out blank.
I am completely stumped.
SELECT O.ORDERNO, C.CNAME, PN.PRODNAME, ODT.PRICE, ODT.QTY, ODT.QTY * PN.PRODSELL AS TOTAL
FROM prema01.ORDER O, prema01.CUSTOMER C, prema01.ORDERDETL ODT, prema01.PRODUCT PN, prema01.COUNTRY CT
WHERE CT.COUNTRY_NAME = 'Australia'
AND C.COUNTRY_CODE = CT.COUNTRY_CODE
AND C.CUSTNO = O.CUSTNO
AND O.ORDERNO = ODT.ORDERNO
AND PN.PRODNO = ODT.PRODNO AND O.ORDERNO <= 60
ORDER BY TOTAL DESC;
forgot to add the change. the tables have data in them, i've physically verified that.
For starters, you need some join conditions e.g.
Select
r.country_code,
c.cname
From
prema01.country r
inner join
prema01.customer c
on r.country_code = c.country_code
You need to build up the relationships with the other tables in a similar way.
Also, are you sure your tables have any data in them. Does
Select
Count(*)
From
prema01.country
return anything? How about
Select
Count(*)
From
prema01.customer
Where
country_code = 'AUS'
?
Fixed, I was looking for 'Australia' while I have 'AUSTRALIA' in my country table. thanks for the help