April 24, 2013 at 12:01 pm
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!
April 24, 2013 at 12:33 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 24, 2013 at 1:12 pm
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.
April 24, 2013 at 1:37 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 24, 2013 at 1:42 pm
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.
April 24, 2013 at 2:27 pm
- 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.
April 24, 2013 at 2:40 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 24, 2013 at 2:48 pm
Hmm .. maybe something like :
SELECT userID as "Highest Spending User", MAX(Revenue_Spend) as "Amount Spent"
FROM MaxCents
...?
April 24, 2013 at 3:00 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 24, 2013 at 3:16 pm
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.
April 24, 2013 at 3:22 pm
DKN2101 (4/24/2013)
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.
There is no need to aggregate the data. We already did that with the CTE. Which row do you want from the CTE?
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 25, 2013 at 2:09 pm
Do you still need help with this?
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 26, 2013 at 12:47 am
April 26, 2013 at 7:17 am
DKN2101 (4/24/2013)
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.
How about this?
with MaxCents (UserID, Revenue_Spend) as
(
select
UserID,
SUM(Cents) TotalCents
from
DollarTransactions
where
createdDate >= '2012-12-01'
and createdDate < '2013-01-01'
group by
UserID
)
select top (1)
UserID,
TotalCents
from
MaxCents
order by
TotalCents desc;
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply