473,387 Members | 1,619 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.

Need help in writing sql query.

85 64KB
Dear all.
Let me explain a simple scenario.
suppose i have two tables tbl_Leads and tbl_Leads_Correspondence.
and i have following data in both table

tbl_Leads
LeadID lName lDate
1 first 2014-11-24 22:22:52.523
2 second 2014-11-25 22:22:52.523
3 third 2014-11-26 22:22:52.523
4 fourth 2014-11-27 22:22:52.523
5 fifth 2014-11-28 22:22:52.523
6 sixth 2014-11-29 22:22:52.523



tbl_Leads_Correspondence
ID LeadID byName Date
1 1 waqas 2014-11-24 22:22:52.523
2 3 waqas 2014-11-26 22:22:52.523
3 4 wilfred 2014-11-27 22:22:52.523
4 5 wilfred 2014-11-28 22:22:52.523
5 6 wilfred 2014-11-29 22:22:52.523
6 6 waqas 2014-11-29 22:22:52.523
7 6 waqas 2014-11-29 22:22:52.523
8 6 waqas 2014-11-29 22:22:52.523
9 6 waqas 2014-11-29 22:22:52.523
10 6 waqas 2014-11-29 22:22:52.523
11 6 waqas 2014-11-29 22:22:52.523
12 1 wilfred 2014-11-29 22:22:52.523

it can be seen that the second table tbl_Leads_Correspondence has leadid as foreign key from the first table.

now my problem is that
i want only those leadID and total count of leadIDs from the table tbl_Leads_Correspondence where byName= wilfred
which are initiated by wilfred.
for example,
LeadID 6 in tbl_Leads_Correspondence has multiple records but i want the leadid 6 only when it was first entered by wilfred. Like the leadID will be returned only in case of wilfred as wilfred was the first person who entered the record in database.
similarly the leadID from tbl_Leads_Correspondence will only be returned when the byName 'waqas' will be passed to the query as parameter.
i hope i make my self clear..
any help or sample query will be greatly appreciated..

Regards:
Dec 5 '14 #1

✓ answered by Rabbit

You need a query that returns the minimum date per lead. You then join that back to the table to get the rest of the data and filter for Wilfred.

5 1437
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Leads_Correspondence.*, tbl_Leads.*
  2. FROM tbl_Leads INNER JOIN tbl_Leads_Correspondence ON tbl_Leads.LeadID = tbl_Leads_Correspondence.LeadID
  3. WHERE (((tbl_Leads_Correspondence.byName)="wilfred"));
Dec 5 '14 #2
Rabbit
12,516 Expert Mod 8TB
I don't understand what results you are looking for from that sample data. Please provide the sample results you expect rather than trying to describe it.
Dec 5 '14 #3
Mudassir
85 64KB
Dear rabbit
the results i want from tbl_leads_cerrespondence are

Name leadid
wilfred 4
Wilfred 5
Wilfred 6

The record against wilfred also exists with lead id 1. But that was initiated by waqas. And the leadIds 4,5,6 were started by wilfred in the correpondence table.
i hope i make myself clear now..
Dec 7 '14 #4
Rabbit
12,516 Expert Mod 8TB
You need a query that returns the minimum date per lead. You then join that back to the table to get the rest of the data and filter for Wilfred.
Dec 7 '14 #5
Mudassir
85 64KB
Thanx dear i got the idea. Will try it and hope it will work as per my requirement.
Dec 7 '14 #6

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

Similar topics

4
by: George Stout | last post by:
First off I do not know alot about writing queries to an Access Database from an ASP page. This is why I need help. I have an Events database for 6 colleges in our metro area. On the homepage I...
3
by: acdevteam | last post by:
Hello Everyone, We are a dev team very new to Access, and so far we have gotten excellent support from this group. We have a question about writing a query. Here is the situation: We have a...
0
by: vinod | last post by:
Dear experts i need a query like this i have a table like this. accno name amount running_total 1 a 50 50 2 ...
8
by: annecarterfredi | last post by:
query the syscat.columns table and prepare SELECT statement (SELECT all_columns FROM table_name). For example, let's say that TAB_1 table has total of three columns, and the statement should be: ...
5
by: Karthik D V | last post by:
Hello All, I have a table like this ID CHARACTER ----------- --------- 1 A 2 A 3 B 4 B
28
by: Mark Braithwaite | last post by:
Hi We are attempting to create a dynamic spreadsheet which will extract information from CRM / SQL. We have very limited expierence in creating SQL queries and we would really appreciate any...
4
by: muhammadrashidmughal | last post by:
columns f1 f2 f2 f4 f5 f6 f7 f8 ...... 1 2 3 4 ...
1
by: vapanchamukhi | last post by:
hi.... i want to write the query select * from user where name = "richard"; in perl the string richard i have taken that in the variable $name. i have written the query as below $sth =...
6
by: Adelestrat | last post by:
I'm trying to have a query return "due by 9/8/14" if the entry in a field is blank or has a date before 9/8/14, or return "valid through (date)" if the date in the field is after 9/8/14. Here's how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.