473,406 Members | 2,259 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,406 software developers and data experts.

Need SQL Function Brainwave

I have this function in sql server 2005:
Expand|Select|Wrap|Line Numbers
  1. ALTER FUNCTION dbo.fnGuestID()
  2. RETURNS TABLE 
  3. AS
  4. RETURN SELECT g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.Date,s.Quantity,s.SalePrice
  5.        From Guest AS g, Sales As s 
  6.        WHERE g.GuestID = s.GuestID AND s.SalesStatus = 'ToGuestInvoice' 
  7.        GROUP BY g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.Date,s.Quantity,s.SalePrice
And I get the different values on their respected cells....What I would like to know or get some ideas on, is how to make the same GuestID, that comes up more than once on the table, be on the same cells together one below the other instead of two separate cells...For example: Let's say there are more than one GuestID's that are the same, so it has same GuestFirstName, LastName and personal info the same, but the Sales part of it changes because it is a different Sale. Once again, I need some ideas on how to make that specific Guest(Same GuestID) have the different sales included for the same person...any enlightenment will be greatly appreciated. Let me know if this isn't clear.
Jan 16 '08 #1
6 1147
ck9663
2,878 Expert 2GB
I have this function in sql server 2005:
Expand|Select|Wrap|Line Numbers
  1. ALTER FUNCTION dbo.fnGuestID()
  2. RETURNS TABLE 
  3. AS
  4. RETURN SELECT g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.Date,s.Quantity,s.SalePrice
  5.        From Guest AS g, Sales As s 
  6.        WHERE g.GuestID = s.GuestID AND s.SalesStatus = 'ToGuestInvoice' 
  7.        GROUP BY g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.Date,s.Quantity,s.SalePrice
And I get the different values on their respected cells....What I would like to know or get some ideas on, is how to make the same GuestID, that comes up more than once on the table, be on the same cells together one below the other instead of two separate cells...For example: Let's say there are more than one GuestID's that are the same, so it has same GuestFirstName, LastName and personal info the same, but the Sales part of it changes because it is a different Sale. Once again, I need some ideas on how to make that specific Guest(Same GuestID) have the different sales included for the same person...any enlightenment will be greatly appreciated. Let me know if this isn't clear.

try:

Expand|Select|Wrap|Line Numbers
  1.  SELECT g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,
  2.   sum(s.SaleTotal) as TotalSaleOfThisGuestID, count(*) as NumberOfSaleOfThisGuestID
  3.        From Guest AS g, Sales As s 
  4.        WHERE g.GuestID = s.GuestID AND s.SalesStatus = 'ToGuestInvoice' 
  5.        GROUP BY g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail
  6.  

-- CK
Jan 16 '08 #2
try:

Expand|Select|Wrap|Line Numbers
  1.  SELECT g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,
  2.   sum(s.SaleTotal) as TotalSaleOfThisGuestID, count(*) as NumberOfSaleOfThisGuestID
  3.        From Guest AS g, Sales As s 
  4.        WHERE g.GuestID = s.GuestID AND s.SalesStatus = 'ToGuestInvoice' 
  5.        GROUP BY g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail
  6.  
That works, the only issue is that I would like to print this info, and when I do that I would like to have the quantity for each ID, like the different items, quantities, sale prices and the dates of each sale....I hope you understand what I mean, thanks for the suggestion tho I can use that for one aspect of it.
Jan 16 '08 #3
ck9663
2,878 Expert 2GB
That works, the only issue is that I would like to print this info, and when I do that I would like to have the quantity for each ID, like the different items, quantities, sale prices and the dates of each sale....I hope you understand what I mean, thanks for the suggestion tho I can use that for one aspect of it.
if that's the case you will again break the rows into two or more. sum and count per guestid are aggregates...quantities, sale, item are all detail...if you include them, you will break the aggregate again...

-- CK
Jan 17 '08 #4
if that's the case you will again break the rows into two or more. sum and count per guestid are aggregates...quantities, sale, item are all detail...if you include them, you will break the aggregate again...

-- CK
Ok, thanks for the help
Jan 17 '08 #5
if that's the case you will again break the rows into two or more. sum and count per guestid are aggregates...quantities, sale, item are all detail...if you include them, you will break the aggregate again...

-- CK
One more question...is it possible to include an If statement or case in a sql function? (Not stored procedure) Because I was thinking about something like calling both dates separately...to somehow get all the dates, I wouldn't mind anything else about the sale not to come out, but the date would be great...
Jan 17 '08 #6
ck9663
2,878 Expert 2GB
One more question...is it possible to include an If statement or case in a sql function? (Not stored procedure) Because I was thinking about something like calling both dates separately...to somehow get all the dates, I wouldn't mind anything else about the sale not to come out, but the date would be great...

yes. it's called CASE...WHEN


-- CK
Jan 18 '08 #7

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

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
6
by: J Mox | last post by:
I have a function that changes which radio button is selected. I need to pass the form name to the function but not the field name and am doing so like: changeRadio(this.form); The function: ...
6
by: Chad A. Beckner | last post by:
Does anyone know how to make common database connections "common" in all aspx files? In other words, instead of creating a SQLConnection, then a DataAdapter in every ASPX file for my application,...
3
by: Buddy Robbins | last post by:
Hey folks, I'm trying to use the PathCleanupSpec function from the shell library. The function prototype is: int PathCleanupSpec( LPCWSTR pszDir, LPWSTR pszSpec) In the old days of VB6, I...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
7
by: fox | last post by:
Hi, Lacking javascript knowledge, I just realized why my project has a bug. I am using ASP to loop through a set of records while it creates URLs with a querystring that has a single value pair....
0
by: Avinash Vora | last post by:
On Aug 5, 2008, at 8:37 PM, Michael Mabin wrote: You aren't differentiating too far from TurboGears at this point. I don't quite follow this, but... Uh oh.
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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...

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.