473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trying to get data by distinct field values

7
I have a table that has job data, Account_Num, Work_Order_Num, Date, Short_Desc, TechID, QCID, QCPassFail.

In this table there may be multiple records with the same Account_Num and Work_Order_Num, however when it comes time to check the QCPassFail, I need to only count 1 for each unique Account_Num/Work_Order_Num pair.

My problem comes in that I need more than just those 2 fields for reporting purposes so a SELECT DISTINCT on those two fields only won't work.

I've tried using a subquery with WHERE x IN, and I've had no luck trying to use an inner join using the same table.
Apr 9 '08 #1
6 1561
Delerna
1,134 Expert 1GB
So for the accounts that have more than 1 record, what are the rules that you want to apply in order to work out which one of them you want the query to return.
Or are you after something like this
Expand|Select|Wrap|Line Numbers
  1. SELECT a.Account_Num, Ctn_Account,
  2.        a.Work_Order_Num, Cnt_Order,
  3.        Date, Short_Desc, TechID, QCID, QCPassFail
  4. FROM YourTable a
  5. left join
  6. (   SELECT Account_Num, Work_Order_Num,
  7.            count(Account_Num) as Ctn_Account, 
  8.            count(Work_Order_Num) as Cnt_Order
  9.     FROM YourTable
  10. ) b on a.Account_Num=b.Account_Num 
  11.    and a.Work_Order_Num=b.Work_Order_Num
  12.  
Apr 9 '08 #2
gadall
7
Expand|Select|Wrap|Line Numbers
  1. Account_Num  Work_Order_Num   Schedule_Date    Short_Desc   TechID  QCID QCPassFail
  2. 20661406     25853100           2007-10-12       VIDEO DISC  73002   73011  1
  3. 20661406     25853100           2007-10-12       CHSI DISC     73002   73011  1
For the purposes of this report, I only need 1 of these records, which one, is irrelevant.
Apr 9 '08 #3
ck9663
2,878 Expert 2GB
What kind of report are you generating? If just a count or summary, you don't need the entire record, which could be easier to be implemented in a query.

-- CK
Apr 9 '08 #4
gadall
7
What kind of report are you generating? If just a count or summary, you don't need the entire record, which could be easier to be implemented in a query.

-- CK
I need all the information shown in the records (the actual records have a lot more information) This information is pulled into an excel spreadsheet for a detailed report (the count is done by formula in excel).
Apr 9 '08 #5
Delerna
1,134 Expert 1GB
Not sure if this suits all of your data but it works for the data you provide

Expand|Select|Wrap|Line Numbers
  1. SELECT Account_Num,
  2.       Work_Order_Num,
  3.       count(Account_Num) as Account_Num,
  4.       count(Work_Order_Num as Work_Order_Num,
  5.       Schedule_Date,
  6.       first(Short_Desc)  as Short_Desc,
  7.       TechID,
  8.       QCID,
  9.       QCPassFail
  10. FROM YourTable
  11. GROUP BY  Account_Num,Work_Order_Num,Schedule_Date,TechID,QCID,QCPassFail
  12.  
Or this may suit better
Expand|Select|Wrap|Line Numbers
  1. SELECT Account_Num,
  2.       Work_Order_Num,
  3.       count(Account_Num) as Account_Num,
  4.       count(Work_Order_Num as Work_Order_Num,
  5.       first(Schedule_Date) as Schedule_Date,
  6.       first(Short_Desc)  as Short_Desc,
  7.       first(TechID) as TechID,
  8.       first(QCID) as QCID,
  9.       first(QCPassFail) as QCPassFail
  10. FROM YourTable
  11. GROUP BY  Account_Num,Work_Order_Num
  12.  
Apr 9 '08 #6
gadall
7
Working solution:

Expand|Select|Wrap|Line Numbers
  1.  SELECT DISTINCT Account_Num,
  2.  
  3.             Work_Order_Num,
  4.  
  5.             Schedule_Date,
  6.  
  7.             Assigned_Installer,
  8.  
  9.             MIN(Short_Description) as Short_Desc,
  10.  
  11.             QCID,
  12.  
  13.             QCPassFail
  14.  
  15.       FROM QCData 
  16.  
  17.       WHERE QCPassFail IS NOT NULL AND Schedule_Date >= '3/1/2008' AND Schedule_Date <= '3/31/2008'
  18.  
  19.       GROUP BY Schedule_Date, Account_Num, Work_Order_Num, Assigned_Installer, QCID, QCPassFail
Apr 10 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Santosh | last post by:
Since I just started my new work, I have inherited a MS Access database which has nearly 13000 records in a single table. Now, my mandate is to clean the database and maybe split the table into...
7
by: mark | last post by:
Please excuse this blatant attempt at advertising. I hope you find this software both interesting and useful. http://www.caddproductivity.com/dataswap.htm Data Swap The Data Swap program...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
5
by: sdowney717 | last post by:
from this, circdate being a datetime field: SQLQuery = "select distinct circdate from circdata order by circdate" I need the distinct date portion excluding the time part. this has come about...
5
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character...
6
by: syvman | last post by:
Hi all... I have a quick question, if anyone knows the answer. I've built a query which looks at a set of records in a table (tblFilename) and I've set it up so that it displays the following...
1
by: Rajasuds | last post by:
I have a question in MS Access I have two tables MASTER with Fields 1, Field 2, Field 3 TRANS with Field1, Field 2, Field,10 I need to set up a look up condition where, I need !Field 2 to...
2
by: Techhead | last post by:
I need to run a SELECT DISTINCT query across multiple fields, but I need to add another field that is NON-DISTINCT to my record set. Here is my query: SELECT DISTINCT lastname, firstname,...
5
by: DeanL | last post by:
Hi all, I'm trying to set up a query that runs from a command button on a form (simple enough so far), what I want the query to do is take values from the fields on the form (seven fields in...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.