473,804 Members | 3,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with SubQueries?

Guys,

Got this query...

---------------------------
SELECT TOP 5 Tbl_admin_hotel s.HotelName,
(SELECT COUNT(Tbl_marke ting_history.Ho telID)
FROM Tbl_marketing_h istory
WHERE Tbl_marketing_h istory.HotelID = Tbl_admin_hotel s.HotelID)
AS CountTotal,
(SELECT MAX(DateSent)
FROM Tbl_marketing_h istory
WHERE Tbl_marketing_h istory.HotelID = Tbl_admin_hotel s.HotelID)
AS LastSent
FROM Tbl_admin_hotel s
WHERE NOT Tbl_admin_hotel s.HotelID = 99
ORDER BY CountTotal DESC
---------------------------

Within the table Tbl_marketing_h istory there is also a 'Subject' column that
I require, the row to grab this Subject column from should relate to the row
I'm selecting in the SubQuery to grab 'DateSent'.

I've tried and tried to grab multiple columns but failed miserably? Can
anyone help pleaseeee!

Cheers, Ash
Jul 20 '05
12 1719
Hi

If you ran the query I posted i.e.

SELECT HotelId, DateSent, COUNT(*)
FROM Tbl_marketing_h istory
GROUP BY HotelId, DateSent
HAVING COUNT(*) > 1

You would know for certain if there are duplicates. Without DDL this is made
harder. If DateSent is a SQLServer Datatime data type then there is less
likely to be duplicated, but yours may be a simple character representation.
Your comments tend to imply that your database design is not quite right. I
would expect HotelId and DateSent to be the primary key in a history table
(although Datesent could be an attribute of a mailshot). You do not seem to
have a difference between campaigns and mailshots. I would expect a mailshot
to be attributable to multiple campaigns and vice versa.

With the current SQL you may want to try:
SELECT DISTINCT TOP 5
H.HotelName,
M.subject,
C.countTotal,
C.lastSent
FROM
Tbl_admin_hotel s H
JOIN
(SELECT
HotelID,
COUNT(HotelID) as countTotal,
MAX( dateSent ) as lastSent
FROM Tbl_marketing_h istory
GROUP BY HotelID ) as mktHistory C
ON C.HotelID = H.HotelID
JOIN Tbl_marketing_h istory M
ON M.hotelID = C.HotelID AND
M.dateSent = C.dateSent
WHERE H.HotelID != 99
ORDER BY CountTotal DESC

John

"J. Hall" <re************ *@a-hall.com> wrote in message
news:tY******** ************@ec lipse.net.uk...
Thanks for your reply Paul,

The tbl_Marketing_H istory has 'CampaignID' as its primary key, the hotelid
column within that table is merely to relate each mailing to a particular
hotel. There may on occasion be two mailings sent the same date, but rarely.
There's definitely no duplication of a hotel in the Tbl_Admin_Hotel s, so I'm a bit unsure as to why we're getting one hotel twice in the query result?

Cheers, Ash
"-P-" <en**********@h otmail.DOTcom> wrote in message
news:Rq******** ************@ad elphia.com...
"J. Hall" <re************ *@a-hall.com> wrote in message news:qs******** ************@ec lipse.net.uk...
Found a problem with this query, its not grabbing distinct hotels, so
in my top 5 listing, I have two of the same hotel?

Cheers, Ash

Could you have duplicate values for (hotelID, dateSent) in

tbl_Marketing_H istory? I assumed that this was the table's
primary key.

Paul Horan
Sr. Architect VCI
Springfield, Mass
www.vcisolutions.com


Jul 20 '05 #11
Hi John,

Appreciate your help, I'll try to summarise the layout - the
tbl_admin_hotel s table stores the unique id and hotel name of each hotel,
the tbl_marketing_h istory stores each mailing subject, body text and
datelastsent, the only unique column in that table is CampaignID - there is
no duplication unless a mailing is accidently sent twice (impatient people
clicking submit twice ;o))

There's definitely no duplication in the tbl_admin_hotel s table - so any
ideas as to why I am getting two of one hotel?
"John Bell" <jb************ @hotmail.com> wrote in message
news:a1******** *************@n ews-text.cableinet. net...
Hi

If you ran the query I posted i.e.

SELECT HotelId, DateSent, COUNT(*)
FROM Tbl_marketing_h istory
GROUP BY HotelId, DateSent
HAVING COUNT(*) > 1

You would know for certain if there are duplicates. Without DDL this is made harder. If DateSent is a SQLServer Datatime data type then there is less
likely to be duplicated, but yours may be a simple character representation. Your comments tend to imply that your database design is not quite right. I would expect HotelId and DateSent to be the primary key in a history table
(although Datesent could be an attribute of a mailshot). You do not seem to have a difference between campaigns and mailshots. I would expect a mailshot to be attributable to multiple campaigns and vice versa.

With the current SQL you may want to try:
SELECT DISTINCT TOP 5
H.HotelName,
M.subject,
C.countTotal,
C.lastSent
FROM
Tbl_admin_hotel s H
JOIN
(SELECT
HotelID,
COUNT(HotelID) as countTotal,
MAX( dateSent ) as lastSent
FROM Tbl_marketing_h istory
GROUP BY HotelID ) as mktHistory C
ON C.HotelID = H.HotelID
JOIN Tbl_marketing_h istory M
ON M.hotelID = C.HotelID AND
M.dateSent = C.dateSent
WHERE H.HotelID != 99
ORDER BY CountTotal DESC

John

"J. Hall" <re************ *@a-hall.com> wrote in message
news:tY******** ************@ec lipse.net.uk...
Thanks for your reply Paul,

The tbl_Marketing_H istory has 'CampaignID' as its primary key, the hotelid column within that table is merely to relate each mailing to a particular hotel. There may on occasion be two mailings sent the same date, but rarely.

There's definitely no duplication of a hotel in the Tbl_Admin_Hotel s, so

I'm
a bit unsure as to why we're getting one hotel twice in the query result?
Cheers, Ash
"-P-" <en**********@h otmail.DOTcom> wrote in message
news:Rq******** ************@ad elphia.com...
"J. Hall" <re************ *@a-hall.com> wrote in message

news:qs******** ************@ec lipse.net.uk...
> Found a problem with this query, its not grabbing distinct hotels,

so in
my
> top 5 listing, I have two of the same hotel?
>
> Cheers, Ash
>
Could you have duplicate values for (hotelID, dateSent) in

tbl_Marketing_H istory? I assumed that this was the table's
primary key.

Paul Horan
Sr. Architect VCI
Springfield, Mass
www.vcisolutions.com



Jul 20 '05 #12
Hi

You are going going to have to post DDL (Create table statements etc
including PKs, FKs and indexes) and example data (as insert statements) so
that you can we can recreate this error.

John

"J. Hall" <re************ *@a-hall.com> wrote in message
news:yp******** ************@ec lipse.net.uk...
Hi John,

Appreciate your help, I'll try to summarise the layout - the
tbl_admin_hotel s table stores the unique id and hotel name of each hotel,
the tbl_marketing_h istory stores each mailing subject, body text and
datelastsent, the only unique column in that table is CampaignID - there is no duplication unless a mailing is accidently sent twice (impatient people
clicking submit twice ;o))

There's definitely no duplication in the tbl_admin_hotel s table - so any
ideas as to why I am getting two of one hotel?
"John Bell" <jb************ @hotmail.com> wrote in message
news:a1******** *************@n ews-text.cableinet. net...
Hi

If you ran the query I posted i.e.

SELECT HotelId, DateSent, COUNT(*)
FROM Tbl_marketing_h istory
GROUP BY HotelId, DateSent
HAVING COUNT(*) > 1

You would know for certain if there are duplicates. Without DDL this is made
harder. If DateSent is a SQLServer Datatime data type then there is less
likely to be duplicated, but yours may be a simple character

representation.
Your comments tend to imply that your database design is not quite right. I
would expect HotelId and DateSent to be the primary key in a history table (although Datesent could be an attribute of a mailshot). You do not seem

to
have a difference between campaigns and mailshots. I would expect a

mailshot
to be attributable to multiple campaigns and vice versa.

With the current SQL you may want to try:
SELECT DISTINCT TOP 5
H.HotelName,
M.subject,
C.countTotal,
C.lastSent
FROM
Tbl_admin_hotel s H
JOIN
(SELECT
HotelID,
COUNT(HotelID) as countTotal,
MAX( dateSent ) as lastSent
FROM Tbl_marketing_h istory
GROUP BY HotelID ) as mktHistory C
ON C.HotelID = H.HotelID
JOIN Tbl_marketing_h istory M
ON M.hotelID = C.HotelID AND
M.dateSent = C.dateSent
WHERE H.HotelID != 99
ORDER BY CountTotal DESC

John

"J. Hall" <re************ *@a-hall.com> wrote in message
news:tY******** ************@ec lipse.net.uk...
Thanks for your reply Paul,

The tbl_Marketing_H istory has 'CampaignID' as its primary key, the hotelid column within that table is merely to relate each mailing to a particular hotel. There may on occasion be two mailings sent the same date, but

rarely.

There's definitely no duplication of a hotel in the Tbl_Admin_Hotel s,
so I'm
a bit unsure as to why we're getting one hotel twice in the query

result?
Cheers, Ash
"-P-" <en**********@h otmail.DOTcom> wrote in message
news:Rq******** ************@ad elphia.com...
> "J. Hall" <re************ *@a-hall.com> wrote in message
news:qs******** ************@ec lipse.net.uk...
> > Found a problem with this query, its not grabbing distinct hotels,

so
in
my
> > top 5 listing, I have two of the same hotel?
> >
> > Cheers, Ash
> >
>
>
> Could you have duplicate values for (hotelID, dateSent) in
tbl_Marketing_H istory? I assumed that this was the table's
> primary key.
>
> Paul Horan
> Sr. Architect VCI
> Springfield, Mass
> www.vcisolutions.com
>
>



Jul 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
57667
by: Don | last post by:
Hi, I am moving from Sybase to Oracle and I used to be able to do update statement like this in Sybase: UPDATE TABLE1 SET T1.field1 = T2.field2 FROM TABLE1 T1, TABLE2 T2 WHERE T1.field2 = T2.field2 AND ....
1
1817
by: rsarath | last post by:
Hello, I have the following setup and I would appreciate any help in improving the performance of the query. BigTable: Column1 (indexed) Column2 (indexed) Column3 (no index) Column4 (no index)
8
1998
by: Neeper | last post by:
I'm trying to pull the last 10 records from a transactions from a table using this query: SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10 But I want to display the rows in ascending order by timestamp. I can't get the subquery below to work and not sure why: SELECT *
15
1917
by: MLH | last post by:
Mr Leigh Purvis gave me a very clever piece of SQL to accomplish what is probably an uncommon objective. In it, he uses the EXISTS operator. I can find no documentation on it in A97 HELP. I would like to read more about this useful SQL operator. Suggestions? SELECT CustID, OutType, EXISTS (SELECT CustID FROM tblCorrespondence AS tblC WHERE tblC.CustID = tblCorrespondence.CustID AND tblC.OutType = "01")
8
3080
by: starman7 | last post by:
i have a table with objects in categories and their positions. there will be several rows with category 400, and they will have various positions, i want to delete only the row with the lowest position. i can select the row i want to delete, but don't know how to delete just this row. here's my select:
2
2672
by: psuaudi | last post by:
I have a main query that I would like to call two different subqueries. In MS Access, I usually just save the two subqueries as separate queries which are then called by a third separate and main query. However, I'd like to put them all into one SQL command. Is this possible? Here are the queries: -This query calls the other two queries below- SELECT ., ., Format(((.Date)-(.Date)),"Fixed") AS , .Type FROM INNER JOIN ON (. = .) AND...
0
1057
by: DukeSnyder | last post by:
I am trying to divide 2 subqueries. there is a comment in the query pointing out what I have unsuccessfuly tried. Can anyone lead me in the right direction please. SELECT TOP 100 PERCENT dbo.tCourseDescriptions.CourseName, dbo.nrcAssignments.AssignmentTitle, dbo.nrcAssignments.AssignmentDueDate, dbo.nrcStandard.StandardAbbreviation, dbo.nrcStandard.StandardDescription, dbo.nrcStandard.StateStandardID,...
4
2958
by: muzu1232004 | last post by:
Can anyone explain me when we use correlated subqueries rather than nested subqueries. Do all the correlated subqueries can be written in nested subqueries form as well ? What are the major conditions that apply whenever we write a correlated subquery and why we go for it ? Please let me know about this as i am not clear which to use when.
0
5710
debasisdas
by: debasisdas | last post by:
Using Subqueries ================== The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement. The sub query executes once before the main query. The result of the sub query is used by the main query (outer Query). You can place the sub query in a number of SQL clauses. WHERE clause
1
2884
by: lizandra | last post by:
Greetings, I am a newbie, I have been working to extract data from a basic sales db and trying to decide when I should use joins and when I should use subqueries. Much of what I read online says use subqueries only as a last resort, especially correlated ones as they do a record by record data evaluation and are very resource intensive. Are joins and WHERE and HAVING clauses the preferred method for extracting related data? But in SQL...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.