Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


"Total Revenue Per Day" SQL Query Format


"Total Revenue Per Day" SQL Query Format

Author
Message
DKN2101
DKN2101
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 8
I have a table called DollarTransactions in a database, which records purchases. The table has the following fields, which you can see by clicking the link to a screenshot:



I need to write 3 queries using these fields:

1) Total Cents for each day in the month of March 2013. Revenue is measured with the "cents" field.

2) Total Cents per hour for each day in March 2013

3) User ID of the user who spent the most Cents in December 2012?

I was working on a query for #1 that went like this:

SELECT trunc(createdDate, 'HH') "Hour", sum(cents)
FROM DollarTransactions
WHERE createdDate BETWEEN '2013-03-01' AND '2013-03-31'
GROUP BY trunc(createdDate, 'HH')




But I don't know if that's right, and I'm lost on the other queries. Any help would be really appreciated!
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16525 Visits: 16992
DKN2101 (4/24/2013)
I have a table called DollarTransactions in a database, which records purchases. The table has the following fields, which you can see by clicking the link to a screenshot:



I need to write 3 queries using these fields:

1) Total Cents for each day in the month of March 2013. Revenue is measured with the "cents" field.

2) Total Cents per hour for each day in March 2013

3) User ID of the user who spent the most Cents in December 2012?

I was working on a query for #1 that went like this:

SELECT trunc(createdDate, 'HH') "Hour", sum(cents)
FROM DollarTransactions
WHERE createdDate BETWEEN '2013-03-01' AND '2013-03-31'
GROUP BY trunc(createdDate, 'HH')




But I don't know if that's right, and I'm lost on the other queries. Any help would be really appreciated!


This looks suspiciously like homework. It also is Oracle syntax, not sql server.

We will help guide you with homework but not give you the answers. What have you tried so far?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
DKN2101
DKN2101
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 8
This isn't homework. It's a SQL workstream for a company I just joined (already told them during the interview process I wasn't that strong on SQL). I wanted to try and solve myself before asking co-workers...

The syntax I am using is what little I know of SQL. I think the syntax is oracle but should in fact be SQL Server. This is another problem I'm running into because I don't know which syntax to use where..

My programming background is weak and is limited to C++, Java, and SIMPLE SQL queries. I understand the what needs to be done and can write out pseudocode for each query but I can't make the correct syntax to get return values.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16525 Visits: 16992
OK. #1 and #2 and almost the same thing. Let's start with #1 (Total Cents for each day in the month of March 2013. Revenue is measured with the "cents" field.)

Do you want to show every day even if there are no rows for that day? Or do you only want to show days when there are rows? The answer to this will change the landscape of these two queries.

#3 is pretty straight forward (User ID of the user who spent the most Cents in December 2012)

I would use a cte (common table expression) to first get the total spent for each UserID. Then select the one with the largest total spent.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24157 Visits: 37929
Just want to verify that you are using SQL Server 2008 or SQL Server 2008 R2 (or maybe actually SQL Server 2012).

This info can change things some as well.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
DKN2101
DKN2101
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 8
- I want to show every day, even if there isn't a row for it.

- Don't have much experience with CTE ... the syntax should be something like

WITH <TABLE NAME> (COL1, COL2, ...., COLN)
AS
(<SELECT_QUERY>Wink

From my understanding of CTE and your explanation the idea would be to create a temporary table in which the revenue spend for each userID would display, and then from that temporary table select the user who spent the most.

WITH UserSpend_CTE (userID, Revenue_Spend)
AS
(SELECT UserID, sum(cents) FROM DollarTransactions)
SELECT [????]



The ???? represents that I don't know the syntax for selecting the userID with highest cents spent.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16525 Visits: 16992
DKN2101 (4/24/2013)
- I want to show every day, even if there isn't a row for it.

- Don't have much experience with CTE ... the syntax should be something like

WITH <TABLE NAME> (COL1, COL2, ...., COLN)
AS
(<SELECT_QUERY>Wink

From my understanding of CTE and your explanation the idea would be to create a temporary table in which the revenue spend for each userID would display, and then from that temporary table select the user who spent the most.

WITH UserSpend_CTE (userID, Revenue_Spend)
AS
(SELECT UserID, sum(cents) FROM DollarTransactions)
SELECT [????]



The ???? represents that I don't know the syntax for selecting the userID with highest cents spent.


Your cte would be something like this:


with MaxCents (UserID, Revenue_Spend) as
(
   select UserID, SUM(Cents)
   from DollarTransactions
   where createdDate >= '2012-12-01'
      and createdDate < '2013-01-01'
   group by UserID
)



So now how would you get which UserID spent the most?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
DKN2101
DKN2101
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 8
Hmm .. maybe something like :


SELECT userID as "Highest Spending User", MAX(Revenue_Spend) as "Amount Spent"
FROM MaxCents



...?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16525 Visits: 16992
DKN2101 (4/24/2013)
Hmm .. maybe something like :


SELECT userID as "Highest Spending User", MAX(Revenue_Spend) as "Amount Spent"
FROM MaxCents



...?


What happened when you tried that? Syntax error?

Keep in mind that your cte now contains the Revenue_Spend for ALL UserIDs. Which one do you want?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
DKN2101
DKN2101
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 8
Syntax error.

If i do:


SELECT MAX(Revenue_Spend) AS "Largest Amount Spent" FROM MaxCents



I get the max amount spent, but I can't figure out how to get it to display the userID as well.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search