on 12-05-2013 6:24 AM
Dear Experts, I have a sample query below:
First part of the output: How can I remove the duplicates in Pls advice. Thank you very much Steve |
Dear All,
Thx for your answer.
Here is another my query:
WITH receipt AS (
SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,
ROW_NUMBER() OVER (partition by oign.docnum, ign1.baseref
ORDER BY oign.docnum, ign1.baseref) as row
FROM ign1 inner join OWOR on owor.DocNum=ign1.BaseRef and ign1.ItemCode=owor.ItemCode
inner join OIGN on oign.DocEntry=ign1.DocEntry
GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum
),
issued AS (
SELECT ige1.ItemCode, OIGe.DocNum, ige1.BaseRef, sum(ige1.linetotal) as total_issue
FROM ige1 inner join OWOR on owor.DocNum= ige1.BaseRef
inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry
group by ige1.ItemCode, ige1.BaseRef, oige.DocNum
)
SELECT owor.ItemCode,
owor.DocNum, issued.DocNum as Issued_No,
SUM(issued.total_issue) AS issuedamount,
receipt.DocNum as Receipt_No,
SUM(receipt.total_receipt) AS receiptamount
FROM wor1 inner join owor on owor.DocEntry = wor1.docentry
left outer join issued
on issued.BaseRef = owor.DocNum
left outer join receipt
on receipt.BaseRef = owor.DocNum
where owor.DocNum=154
GROUP BY owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum
The result using the above query is in the screen capture:
The query can't give the expected result. Please help to improve the query so that it can give the expected result as same as in the print screen. Thank you
Rgds,
Steve
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Steve,
The result that you want is impossible in SQL query. In a SQL Query all columns and all records are shown.
In your query there's no "Duplicate Entry". SQL can remove the "Duplicate Entry" if all columns in a row have the same value and it's not your case.
Only if you use Crystal Report you can remove what you call "Duplicate Entry".
Regards,
Augusto
Hi Augusto,
yes you are correct. I am looking for the solution how to remove this value:
FG10000, 154, 1, 2 and 320 in the above sample.
Then the expected result is as follow:
or
Both are expected but which one is possible, it is better.
Do I have to use crystal report ? but if I use crystal report, do I have to use store procedure query ? if yes, can you give the query ?
If it is possible using query generator rather than crystal report, I think it is more better to us
Thank you
Steve
Hi Steve,
There is so many possibilities are there, for example
For one production , one issue for production 3 Receipt from production
OR
3 Issue for production one receipt from production .
So i think achieving this in query is complicate, better and easy option is use "Crystal report" .
Use this same query in crystal report, there you have one option called "Suppress if duplicate" . Using this option you can achieve your result.
Right Click on the field -> Format Field-> Suppress if duplicate.
I hope this will solve your issue.
Hi Steve,
Try this below query, am getting your expected result.
WITH receipt AS (
SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,
ROW_NUMBER() OVER (partition by oign.docnum, ign1.baseref
ORDER BY oign.docnum, ign1.baseref) as row
FROM ign1 inner join OWOR on owor.DocNum=ign1.BaseRef and ign1.ItemCode=owor.ItemCode
inner join OIGN on oign.DocEntry=ign1.DocEntry
GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum
),
issued AS (
SELECT ige1.ItemCode, OIGe.DocNum, ige1.BaseRef, sum(ige1.linetotal) as total_issue
FROM ige1 inner join OWOR on owor.DocNum= ige1.BaseRef
inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry
group by ige1.ItemCode, ige1.BaseRef, oige.DocNum
)
Select (CASE rnum when 1 then ItemCode end) [FG-Code],
(CASE rnum when 1 then DocNum end) [DocNum],
(CASE rnum when 1 then Issued_No end) [Issued Num],
(CASE rnum when 1 then issuedamount end) [Issued Amt],
Receipt_No,receiptamount from (
SELECT owor.docentry,ROW_NUMBER() OVER (partition by owor.docentry order by owor.docentry) [rnum] ,owor.ItemCode,
owor.DocNum, issued.DocNum as Issued_No,
SUM(issued.total_issue) AS issuedamount,
receipt.DocNum as Receipt_No,
SUM(receipt.total_receipt) AS receiptamount
FROM wor1 inner join owor on owor.DocEntry = wor1.docentry
left outer join issued
on issued.BaseRef = owor.DocNum
left outer join receipt
on receipt.BaseRef = owor.DocNum
where owor.DocNum = 154
GROUP BY owor.docentry,owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum ) TBL1
Hi Steve,
As I told you before, using the query generator is impossible.
In Crystal you can use the same Query that you have and then when you are writing the detail you have to make a condition and if the condition is true the value of the fields will not be printed.
I'm not a great expert in Crystal report.
You have to close this thread and create a new one in the Crystal Reports place. There are much more experts than me in Crystal.
Don't forget to mark the answers as Helpful answer or as Correct answer.
Hope it helps,
Hi Steve,
Try this query, let me know your feedback
WITH receipt AS (
SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,
ROW_NUMBER() OVER (partition by oign.docnum, ign1.baseref
ORDER BY oign.docnum, ign1.baseref) as row
FROM ign1 inner join OWOR on owor.DocNum=ign1.BaseRef and ign1.ItemCode=owor.ItemCode
inner join OIGN on oign.DocEntry=ign1.DocEntry
GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum
),
issued AS (
SELECT ige1.ItemCode, OIGe.DocNum, ige1.BaseRef, sum(ige1.linetotal) as total_issue
FROM ige1 inner join OWOR on owor.DocNum= ige1.BaseRef
inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry
group by ige1.ItemCode, ige1.BaseRef, oige.DocNum
)
Select (CASE rnum when 1 then ItemCode end) [FG-Code],
(CASE rnum when 1 then DocNum end) [DocNum],
(CASE ISrnum when 1 then Issued_No end) [Issued Num],
(CASE ISrnum when 1 then issuedamount end) [Issued Amt],
Receipt_No,receiptamount from (
SELECT owor.docentry,ROW_NUMBER() OVER (partition by owor.docentry order by owor.docentry) [rnum] ,
ROW_NUMBER() OVER (partition by issued.DocNum order by issued.DocNum) [ISrnum],
owor.ItemCode,
owor.DocNum, issued.DocNum as Issued_No,
SUM(issued.total_issue) AS issuedamount,
receipt.DocNum as Receipt_No,
SUM(receipt.total_receipt) AS receiptamount
FROM wor1 inner join owor on owor.DocEntry = wor1.docentry
left outer join issued
on issued.BaseRef = owor.DocNum
left outer join receipt
on receipt.BaseRef = owor.DocNum
where owor.DocNum = 154
GROUP BY owor.docentry,owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum ) TBL1
Hi Steve,
Try this below query,
WITH receipt AS (
SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,
ROW_NUMBER() OVER (partition by oign.docnum, ign1.baseref
ORDER BY oign.docnum, ign1.baseref) as row
FROM ign1 inner join OWOR on owor.DocNum=ign1.BaseRef and ign1.ItemCode=owor.ItemCode
inner join OIGN on oign.DocEntry=ign1.DocEntry
GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum
),
issued AS (
SELECT ige1.ItemCode, OIGe.DocNum, ige1.BaseRef, sum(ige1.linetotal) as total_issue
FROM ige1 inner join OWOR on owor.DocNum= ige1.BaseRef
inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry
group by ige1.ItemCode, ige1.BaseRef, oige.DocNum
)
select * from (
Select (CASE rnum when 1 then ItemCode end) [FG-Code],
(CASE rnum when 1 then DocNum end) [DocNum],
(CASE ISrnum when 1 then Issued_No end) [Issued Num],
(CASE ISrnum when 1 then issuedamount end) [Issued Amt],
(CASE RSrnum when 1 then Receipt_No end) [Receipt Num],
(CASE RSrnum when 1 then receiptamount end) [Receipt Amt]
from (
SELECT owor.docentry,ROW_NUMBER() OVER (partition by owor.docentry order by owor.docentry) [rnum] ,
ROW_NUMBER() OVER (partition by issued.DocNum order by issued.DocNum) [ISrnum],
ROW_NUMBER() OVER (partition by receipt.DocNum order by receipt.DocNum) [RSrnum],
owor.ItemCode, owor.DocNum, issued.DocNum as Issued_No,
max(issued.total_issue) AS issuedamount,
receipt.DocNum as Receipt_No,
max(receipt.total_receipt) AS receiptamount
FROM wor1 inner join owor on owor.DocEntry = wor1.docentry
left outer join issued
on issued.BaseRef = owor.DocNum
left outer join receipt
on receipt.BaseRef = owor.DocNum
where owor.DocNum = 22
GROUP BY owor.docentry,owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum ) TBL1 ) TBL2
group by
TBL2.DocNum,TBL2.[FG-Code],TBL2.[Issued Amt],TBL2.[Issued Num],TBL2.[Receipt Num],TBL2.[Receipt Amt]
having
(Isnull(TBL2.[Issued Amt],0) + Isnull(TBL2.[Receipt Amt],0)) <> 0
order by
TBL2.[FG-Code] desc,TBL2.[Issued Num] desc,TBL2.[Receipt Num] desc
Hi Steve,
Try this, let me know the feedback
WITH receipt AS (
SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,
ROW_NUMBER() OVER (partition by owor.docentry
ORDER BY owor.docentry) as row
FROM ign1 inner join OWOR on owor.DocNum=ign1.BaseRef and ign1.ItemCode=owor.ItemCode
inner join OIGN on oign.DocEntry=ign1.DocEntry
GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum,owor.docentry
),
issued AS (
SELECT
ROW_NUMBER() OVER (partition by owor.docentry
ORDER BY owor.docentry ) as iss_row,
ige1.ItemCode, OIGe.DocNum, ige1.BaseRef, sum(ige1.linetotal) as total_issue
FROM ige1 inner join OWOR on owor.DocNum= ige1.BaseRef
inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry
group by ige1.ItemCode, ige1.BaseRef, oige.DocNum,owor.docentry
)
select * from (
Select (CASE rnum when 1 then ItemCode end) [FG-Code],
(CASE rnum when 1 then DocNum end) [DocNum],
(CASE ISrnum when 1 then Issued_No end) [Issued Num],
(CASE ISrnum when 1 then issuedamount end) [Issued Amt],
(CASE RSrnum when 1 then Receipt_No end) [Receipt Num],
(CASE RSrnum when 1 then receiptamount end) [Receipt Amt]
from (
SELECT owor.docentry,ROW_NUMBER() OVER (partition by owor.docentry order by owor.docentry) [rnum] ,
ROW_NUMBER() OVER (partition by issued.DocNum order by issued.DocNum) [ISrnum],
ROW_NUMBER() OVER (partition by receipt.DocNum order by receipt.DocNum) [RSrnum],
owor.ItemCode, owor.DocNum, issued.DocNum as Issued_No,
max(issued.total_issue) AS issuedamount,
receipt.DocNum as Receipt_No,
max(receipt.total_receipt) AS receiptamount
FROM wor1 inner join owor on owor.DocEntry = wor1.docentry
left outer join issued
on issued.BaseRef = owor.DocNum
left outer join receipt
on receipt.BaseRef = owor.DocNum and receipt.row = issued.iss_row
where owor.DocNum = 154
GROUP BY owor.docentry,owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum ) TBL1 ) TBL2
group by
TBL2.DocNum,TBL2.[FG-Code],TBL2.[Issued Amt],TBL2.[Issued Num],TBL2.[Receipt Num],TBL2.[Receipt Amt]
having
(Isnull(TBL2.[Issued Amt],0) + Isnull(TBL2.[Receipt Amt],0)) <> 0
order by
TBL2.[FG-Code] desc,TBL2.[Issued Num] desc,TBL2.[Receipt Num] desc
Hi Steve,
Previous query will work correct for below example,
if you have more Issue for production than "Receipt from production" then you will get correct answer.
for example you have 4 Issue and 3 Receipt from production than you will get correct answer.
Try the below query ,
This query will give correct answer until More Receipt from production than Issue for production.
But am not able to write a query for both, so based on your scenario you use any one of the query.
WITH receipt AS (
SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,
ROW_NUMBER() OVER (partition by owor.docentry
ORDER BY owor.docentry) as row,owor.docentry
FROM ign1 inner join OWOR on owor.DocEntry= IGN1.BaseEntry and ign1.ItemCode=owor.ItemCode
inner join OIGN on oign.DocEntry=ign1.DocEntry and ign1.BaseType = '202'
GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum,oign.docentry,owor.DocEntry
),
issued AS (
SELECT owor.DocEntry,
ROW_NUMBER() OVER (partition by owor.docentry
ORDER BY owor.docentry ) as iss_row,
OIGe.DocNum, ige1.BaseRef, sum(Isnull(ige1.linetotal,0)) as total_issue
FROM ige1 inner join OWOR on owor.DocEntry= ige1.BaseEntry and IGE1.BaseType = '202'
inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry
group by ige1.BaseRef, oige.DocNum,oige.docentry,owor.DocEntry
)
select
TBL2.DocNum,TBL2.[FG-Code],TBL2.[Issued Num],Isnull(TBL2.[Issued Amt],0) [Issued Amt],TBL2.[Receipt Num],TBL2.[Receipt Amt]
from (
Select (CASE rnum when 1 then ItemCode end) [FG-Code],
(CASE rnum when 1 then Isnull(DocNum,0) end) [DocNum],
(CASE ISrnum when 1 then Isnull(Issued_No,0) end) [Issued Num],
(CASE ISrnum when 1 then Isnull(issuedamount,0) end) [Issued Amt],
(CASE RSrnum when 1 then Isnull(Receipt_No,0) end) [Receipt Num],
(CASE RSrnum when 1 then Isnull(receiptamount,0) end) [Receipt Amt],
(forshort) [forshort]
from (
SELECT owor.docentry,ROW_NUMBER() OVER (partition by owor.docentry order by owor.docentry) [rnum] ,
owor.docentry [forshort],
ROW_NUMBER() OVER (partition by issued.DocNum order by issued.DocNum) [ISrnum],
ROW_NUMBER() OVER (partition by receipt.DocNum order by receipt.DocNum) [RSrnum],
owor.ItemCode, owor.DocNum, issued.DocNum as Issued_No,
max(Isnull(issued.total_issue,0)) AS issuedamount,
receipt.DocNum as Receipt_No,
max(receipt.total_receipt) AS receiptamount
FROM wor1 inner join owor on owor.DocEntry = wor1.docentry
left outer join receipt
on receipt.DocEntry = owor.DocEntry
left outer join issued
on issued.DocEntry = owor.DocEntry and Isnull(issued.iss_row,0) = Isnull(receipt.row,0)
where owor.DocNum = 154
GROUP BY owor.docentry,owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum,receipt.row ,issued.iss_row ) TBL1 ) TBL2
group by
TBL2.DocNum,TBL2.[FG-Code],TBL2.[Issued Amt],TBL2.[Issued Num],TBL2.[Receipt Num],TBL2.[Receipt Amt]
order by
Max(TBL2.forshort) , TBL2.DocNum desc
Hi Prasanna,
Many, many thanks for your answer.
We have uncertainty number of issue for production (IFP) or receipt from production (RFP). Sometimes, both can't be different in numbers.
If it is happened in the future, will you please give the correct query that can accomodate both situation ?
The latest query is now correct but however, if there is a return component, it is not appear in the query.
Another issue using the latest query, if I apply the latest query to one production order that have 15 issue for production documents and 10 receipt from production documents, the result is not correct.
I think it is probably caused by the return components.
Please give us an help again. Thank you.
Rgds,
Steve
Hi Steve,
If you want to display the data in crystal report then try the "Suppress Duplicate Value" option of crystal report. Else in SQL you can try with table variable. Create one table variable (which act as table), insert your result in that table and update that table using loop logic. Hope this will help to resolve your issue.
Regards
Manoj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Steve,
Actual problem is due to multiple receipt from production causes the duplicate entry of issue no and amount.
Same way, if multiple issue for the same production order, the same receipt from production copied two times.
You may try with min or max function to limit query result. If I try, not able paste query here.
Thanks & Regards,
Nagarajan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You may try:
SELECT S.siteName, V.siteIP, MAX(H.date) Date, S.siteName + H.date
FROM sites S
left JOIN history H ON S.siteName = H.siteName
left join value V on V.sitename = S.sitename
Group BY S.siteName, V.siteIP
UNION
SELECT '', '', H.date, S.siteName + H.date
FROM sites S
left JOIN history H ON S.siteName = H.siteName
left join value V on V.sitename = S.sitename
ORDER BY S.siteName + H.date
Even though you can see an additional column, the last column will not be exported because there is no title.
Thanks,
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Steve andre,
select distinct sites.sitename,value.siteip,history.date from site
use distinct to remove duplicates
Hope help full
Thanks and Regards
DEV
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Steve,
When we run a SQL all the content of the rows is shown and there is no way to remove the duplicates.
In your example, if the date was also duplicated it could be possible with the distinct or with a group by.
Regards,
Augusto
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Steve,
Please advice exact table to sort as per above requirement. I think it is not possible to get same row from 1 to 10 if you eliminate duplicate entry.
Thanks & Regards,
Nagarajan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
103 | |
12 | |
11 | |
6 | |
6 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.