SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Coffee sales for just 7am to 8am but for the entire year?


Coffee sales for just 7am to 8am but for the entire year?

Author
Message
chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
select * from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot') and dt_when between '2017-01-01 07:00:00.000' and '2017-01-01 08:00:00.000'


Actually I need it for each Quarter of the year. Is this where GetTime() comes into play? Firsuring out how to write the dateTime part is my only blocker.

Thanks.

ScottPletcher
ScottPletcher
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46311 Visits: 8033
Not 100% sure what you need, but, for example, this gives sales for the latest quarter for >=7AM and <8AM:


select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7



SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
ScottPletcher - Tuesday, December 19, 2017 11:19 AM
Not 100% sure what you need, but, for example, this gives sales for the latest quarter for >=7AM and <8AM:


select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7


And for all of 2017? Thanks for the response, btw. Just change Quarter to Year?

Looking for sales totals for those menu items for each Q of the 2017 calendar year but only between 7am to 8am, everyday.

chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
ScottPletcher - Tuesday, December 19, 2017 11:19 AM
Not 100% sure what you need, but, for example, this gives sales for the latest quarter for >=7AM and <8AM:


select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7

Msg 195, Level 15, State 10, Line 15
'hour' is not a recognized built-in function name.

Ideas?

chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
CREATE TABLE mytable(
i_ticket_id INT NOT NULL PRIMARY KEY
,s_item VARCHAR(50) NOT NULL
,dt_when DATETIME NOT NULL
,c_price MONEY NOT NULL
,c_discount_amountMONEY NOT NULL
);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164230,'Latte','2017-03-01 07:23:42.310',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164241,'Cappucino','2017-03-01 08:11:05.800',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164239,'Espresso','2017-03-01 08:23:45.903',6.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164242,'Espresso','2017-03-01 09:25:27.910',6.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164245,'Latte','2017-03-01 09:28:01.330',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164248,'Latte','2017-03-01 09:31:26.720',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164248,'Latte','2017-03-01 09:31:26.803',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164242,'Espresso','2017-03-01 09:40:26.087',6.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164242,'Espresso','2017-03-01 10:02:26.780',6.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164253,'Cappucino','2017-03-01 10:21:47.083',7.00,1.75);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164263,'Latte','2017-03-01 12:21:43.513',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164266,'Double Espresso','2017-03-01 13:51:13.003',8.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164275,'Latte','2017-03-01 14:04:02.710',7.00,1.75);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164378,'Sm Coffee Pot','2017-03-02 05:55:34.193',8.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164379,'Sm Coffee Pot','2017-03-02 06:04:46.210',8.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164381,'Lg Coffee Pot','2017-03-02 06:26:39.187',12.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164392,'Latte','2017-03-02 08:05:02.310',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164395,'Latte','2017-03-02 08:13:02.680',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164397,'Sm Coffee Pot','2017-03-02 08:20:33.860',8.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164401,'Cappucino','2017-03-02 08:36:30.353',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164402,'Latte','2017-03-02 08:38:36.657',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164402,'Latte','2017-03-02 08:38:36.660',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164404,'Cappucino','2017-03-02 08:41:42.840',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164395,'Latte','2017-03-02 09:02:01.063',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164413,'Latte','2017-03-02 10:19:28.853',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164416,'Espresso','2017-03-02 10:45:29.847',6.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164418,'Lg Coffee Pot','2017-03-02 11:33:20.280',12.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164420,'Espresso','2017-03-02 12:26:14.910',6.00,1.50);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164420,'Espresso','2017-03-02 12:26:14.947',6.00,1.50);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164424,'Cappucino','2017-03-02 13:35:07.143',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164424,'Cappucino','2017-03-02 13:35:07.160',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164472,'Iced Coffee','2017-03-02 16:34:57.690',5.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164485,'Iced Coffee','2017-03-02 20:29:01.117',5.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164547,'Sm Coffee Pot','2017-03-03 07:33:24.243',8.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164550,'Iced Coffee','2017-03-03 07:50:02.023',5.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164565,'Latte','2017-03-03 09:12:33.853',7.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164570,'Sm Coffee Pot','2017-03-03 09:38:59.373',8.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164573,'Lg Coffee Pot','2017-03-03 10:34:59.517',12.00,0.00);
INSERT INTO mytable(i_ticket_id,s_item,dt_when,c_price,c_discount_amount) VALUES (164657,'Cappucino','2017-03-03 15:50:17.227',7.00,0.00);

chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(Quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7


This query chokes on the 'hour' giving:
Msg 195, Level 15, State 10, Line 15
'hour' is not a recognized built-in function name.

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223132 Visits: 40398
chef423 - Tuesday, December 19, 2017 12:01 PM
select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(Quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7


This query chokes on the 'hour' giving:
Msg 195, Level 15, State 10, Line 15
'hour' is not a recognized built-in function name.


Because it isn't, use datepart, for example: DATEPART(HOUR,GETDATE())


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)
chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
Lynn Pettis - Tuesday, December 19, 2017 12:11 PM
chef423 - Tuesday, December 19, 2017 12:01 PM
select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(Quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7


This query chokes on the 'hour' giving:
Msg 195, Level 15, State 10, Line 15
'hour' is not a recognized built-in function name.


Because it isn't, use datepart, for example: DATEPART(HOUR,GETDATE())

Thanks

select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(Quarter, datediff(quarter, 0, getdate()), 0)
and DATEPART(hour,dt_when) = 7

chef423
chef423
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2740 Visits: 410
Lynn Pettis - Tuesday, December 19, 2017 12:11 PM
chef423 - Tuesday, December 19, 2017 12:01 PM
select *
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(Quarter, datediff(quarter, 0, getdate()), 0)
and hour(dt_when) = 7


This query chokes on the 'hour' giving:
Msg 195, Level 15, State 10, Line 15
'hour' is not a recognized built-in function name.


Because it isn't, use datepart, for example: DATEPART(HOUR,GETDATE())

If I want to choose Q1 / Q2 / Q3 from the same year? -1 -2 -3?

ScottPletcher
ScottPletcher
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46311 Visits: 8033
Sorry about the HOUR(), that's the one function I always forget doesn't exist. Per quarter for the current year, something like this:


select
DATEPART(quarter, dt_when) as quarter,
SUM(sales_amount) AS sales
from TicketItem
where s_item IN('Coffee', 'Espresso', 'Double Espresso', 'Decaf Espresso', 'Cappucino', 'Latte', 'Iced Coffee', 'sm coffee pot', 'lg coffee pot')
and dt_when >= dateadd(year, datediff(year, 0, getdate()), 0)
and datepart(hour, dt_when) = 7
group by DATEPART(quarter, dt_when)



SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
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