Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

"Total Revenue Per Day" SQL Query Format Expand / Collapse
Author
Message
Posted Wednesday, April 24, 2013 12:01 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 26, 2013 10:00 AM
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!
Post #1446156
Posted Wednesday, April 24, 2013 12:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1446179
Posted Wednesday, April 24, 2013 1:12 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 26, 2013 10:00 AM
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.
Post #1446197
Posted Wednesday, April 24, 2013 1:37 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1446209
Posted Wednesday, April 24, 2013 1:42 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:02 PM
Points: 20,862, Visits: 32,893
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.



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)
Post #1446211
Posted Wednesday, April 24, 2013 2:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 26, 2013 10:00 AM
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>)

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.
Post #1446223
Posted Wednesday, April 24, 2013 2:40 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
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>)

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)
Post #1446234
Posted Wednesday, April 24, 2013 2:48 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 26, 2013 10:00 AM
Points: 5, Visits: 8
Hmm .. maybe something like :

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

...?
Post #1446236
Posted Wednesday, April 24, 2013 3:00 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1446239
Posted Wednesday, April 24, 2013 3:16 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 26, 2013 10:00 AM
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.
Post #1446244
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse