Is there something wrong with my SQL?

G

GD

I'm trying to run a make table bringing the ID, InvNo & PONo fields from
T09_DupInvs1 table, and I want to rank the PONo field (preferably in order by
ID [autonumber]). The result of the SQL below is the ID, PONo and Rank
fields, but no InvNo. And the rank is 1 all the way down its field. What am
I doing wrong? I'm very new to SQL. THANKS!!!!

SELECT Q.ID, Q.InvNo, Q.PONo (SELECT COUNT (*) FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID]) +1 AS Rank INTO T10_DupInvs2
FROM T09_DupInvs1 AS Q
ORDER BY Q.ID;
 
J

John Spencer

SELECT Q.ID
, Q.InvNo
, Q.PONo
, <<< You are missing this comma in the posted SQL >>>
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID]) +1 AS Rank INTO T10_DupInvs2
FROM T09_DupInvs1 AS Q
ORDER BY Q.ID;

Do you want to start over at 1 for each InvNo and POno combination? That is
what you seem to be doing with the above query.

If you want one sequence of numbers for all the records, then
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[ID] < Q.[ID])

If you want the sequence to restart with each PONo
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID])

If you want the sequnce to restart with each InvNo
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[ID] < Q.[ID])


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
G

GD

Thanks, John!! That solved the ranking problem. But why wouldn't it be
including the InvNo field as a result?

--
GD


John Spencer said:
SELECT Q.ID
, Q.InvNo
, Q.PONo
, <<< You are missing this comma in the posted SQL >>>
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID]) +1 AS Rank INTO T10_DupInvs2
FROM T09_DupInvs1 AS Q
ORDER BY Q.ID;

Do you want to start over at 1 for each InvNo and POno combination? That is
what you seem to be doing with the above query.

If you want one sequence of numbers for all the records, then
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[ID] < Q.[ID])

If you want the sequence to restart with each PONo
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID])

If you want the sequnce to restart with each InvNo
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[ID] < Q.[ID])


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
I'm trying to run a make table bringing the ID, InvNo & PONo fields from
T09_DupInvs1 table, and I want to rank the PONo field (preferably in order by
ID [autonumber]). The result of the SQL below is the ID, PONo and Rank
fields, but no InvNo. And the rank is 1 all the way down its field. What am
I doing wrong? I'm very new to SQL. THANKS!!!!

SELECT Q.ID, Q.InvNo, Q.PONo (SELECT COUNT (*) FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID]) +1 AS Rank INTO T10_DupInvs2
FROM T09_DupInvs1 AS Q
ORDER BY Q.ID;
 
G

GD

Never mind...I forgot about your missing comma catch.
--
GD


GD said:
Thanks, John!! That solved the ranking problem. But why wouldn't it be
including the InvNo field as a result?

--
GD


John Spencer said:
SELECT Q.ID
, Q.InvNo
, Q.PONo
, <<< You are missing this comma in the posted SQL >>>
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID]) +1 AS Rank INTO T10_DupInvs2
FROM T09_DupInvs1 AS Q
ORDER BY Q.ID;

Do you want to start over at 1 for each InvNo and POno combination? That is
what you seem to be doing with the above query.

If you want one sequence of numbers for all the records, then
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[ID] < Q.[ID])

If you want the sequence to restart with each PONo
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID])

If you want the sequnce to restart with each InvNo
(SELECT COUNT (*)
FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[ID] < Q.[ID])


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
I'm trying to run a make table bringing the ID, InvNo & PONo fields from
T09_DupInvs1 table, and I want to rank the PONo field (preferably in order by
ID [autonumber]). The result of the SQL below is the ID, PONo and Rank
fields, but no InvNo. And the rank is 1 all the way down its field. What am
I doing wrong? I'm very new to SQL. THANKS!!!!

SELECT Q.ID, Q.InvNo, Q.PONo (SELECT COUNT (*) FROM [T09_DupInvs1] Q1
WHERE Q1.[InvNo] = Q.[InvNo]
AND Q1.[PONo] = Q.[PONo]
AND Q1.[ID] < Q.[ID]) +1 AS Rank INTO T10_DupInvs2
FROM T09_DupInvs1 AS Q
ORDER BY Q.ID;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top