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.
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 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 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 2 years ago.
Improve this question
i am trying to join these two tables, but I can only do so if I change values of columns while joining like "substr(to_char(p.personnummer),3)" and "substr(k.ip_id,1,10)" . Can anyone tell me what is the order I am supposed to do it?
select x.ip_id, x.organisationsnummer
from (select
substr(to_char(p.personnummer),3) as ip_id_jnr,
substr(k.ip_id,1,10) as ipo_id
from customer k
inner join customer_VIEW p on ipo_id = ip_id_jnr) x
;
Your query doesn't show which table contains organisationsnummer; I used the k alias (as if it belongs to customer); fix it if necessary.
So, inline view:
select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10);
Now use it:
select x.ip_id,
x.organisationsnummer
from (select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10)
) x;
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.
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
Hello i need to know when (date) the 10 (tenth) item as been sold, the ID item is passed per parameter,
this is the sales tables:
Thks in advance
Please try the following (I believe* that I used the correct columns):
// This example finds the date that the 10th occurrence of Part #1001 was sold.
// #idArtigo is the placeholder for your incoming parameter
DECLARE #idArtigo int;
SET #idArtigo = 1001;
WITH Artigos AS (
SELECT
Row_Number() OVER(ORDER BY v.[DataHora] ASC, lv.[IdLinhaVenda] ASC) AS RowNumber,
v.[DataHora],
v.[IdVenda],
lv.[IdLinhaVenda]
FROM
[Vendas] AS v
INNER JOIN [LinhasVenda] AS lv
ON ( v.[IdVenda] = lv.[IdVenda] )
WHERE
lv.[IdArtigo] = #idArtigo
)
SELECT
[DataHora],
[IdVenda],
[IdLinhaVenda]
FROM
Artigos
WHERE
RowNumber = 10;
*NOTE: I do not know Portuguese (other than via Google Translate), so I made some educated guesses (& assumptions) as to which columns to use.
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 8 years ago.
Improve this question
I have this simple query, I want to retrieve all the data that select statement is selecting now (rightly), plus I want to get total rows number using the same query how can I do this. How do I add total rows query with below query?
SELECT
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
Last item of this result has a row number that equals the number of records.
SELECT
ROW_NUMBER() over(tblTaxingSchemeDetails.TaxSchemeDetailsId)as SeqNo,
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
SELECT * COUNT(*) OVER() AS [Total_Rows]
FROM table 1 innerjoin table 2
You can get the desired result through this Query... Also would remind this query would be take more resource while compare to the normal query...
SELECT
COUNT(tblTaxingSchemeDetails.TaxSchemeDetailsId) over(PARTITION BY tblTaxingScheme.TaxSchemeId) as h,
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId