Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want following sql query in ms access

SQL
DECLARE @BreakFast INT =(SELECT  sum( OM.ItemQuantity)  FROM VW_EmpOrderMaster OM WHERE  OM.MealType=1 AND MONTH(OM.OrderTime)
	=MONTH(GETDATE()) 	 and day(OM.OrderTime)=day(GETDATE()) AND YEAR(OM.OrderTime)=YEAR(GETDATE()))
	DECLARE @Lunch INT =(SELECT  sum( OM.ItemQuantity)  FROM VW_EmpOrderMaster OM WHERE  OM.MealType=2 AND MONTH(OM.OrderTime)=MONTH(GETDATE()) 
		 and day(OM.OrderTime)=day(GETDATE()) AND YEAR(OM.OrderTime)=YEAR(GETDATE())) 

DECLARE @BreakFastvisitor INT =(SELECT  sum( OM1.ItemQuantity)  FROM VW_EmpOrderMaster_Visitor OM1 WHERE  OM1.MealType=1 AND MONTH(OM1.OrderTime)
	=MONTH(GETDATE()) 	 and day(OM1.OrderTime)=day(GETDATE()) AND YEAR(OM1.OrderTime)=YEAR(GETDATE()))
	DECLARE @Lunchvisitor INT =(SELECT  sum( OM1.ItemQuantity)  FROM VW_EmpOrderMaster_Visitor OM1 WHERE  OM1.MealType=2
	 AND MONTH(OM1.OrderTime)=MONTH(GETDATE())	 and day(OM1.OrderTime)=day(GETDATE()) 
	AND YEAR(OM1.OrderTime)=YEAR(GETDATE())) 


declare @totalbreakfast int=sum((ISNULL((@BreakFast), 0))+(ISNULL((@BreakFastvisitor), 0))) 
	declare @totalLunch int = sum((ISNULL((@Lunch), 0))+(ISNULL((@Lunchvisitor), 0)))

SELECT  

		 @totalbreakfast AS BreakFast,  
	 @totalLunch AS Lunch,  


What I have tried:

SQL
      ( (SELECT  sum( OM.ItemQuantity) as BreakFast FROM VW_EmpOrderMaster OM WHERE  OM.MealType=1  AND MONTH(OM.OrderTime)
	=MONTH(now()) 	 and day(OM.OrderTime)=day(now()) AND YEAR(OM.OrderTime)=YEAR(now())) union
(SELECT  sum( OM1.ItemQuantity) as BreakFast  FROM VW_EmpOrderMaster_Visitor OM1 WHERE  OM1.MealType=1 AND MONTH(OM1.OrderTime)
	=MONTH(now()) 	 and day(OM1.OrderTime)=day(now()) AND YEAR(OM1.OrderTime)=YEAR(now()))   )  
Posted
Updated 17-May-19 0:05am
v2
Comments
Kornfeld Eliyahu Peter 16-May-19 6:32am    
And? There is any problem here?
DerekT-P 16-May-19 6:33am    
The original query returns a single row of 2 columns; your reworked version returns 2 rows of 1 column each. Which do you actually require?
Niteshbagal 16-May-19 6:52am    
I want two column in result which in in sql query
Dave Kreskowiak 16-May-19 9:11am    
The problem with that original SQL code is that it's a store procedure, which Access doesn't support.

As Dave said above, your original code is a Stored Procedure, which Access doesn't support. So you'll need to write either a single query that returns all your data in one go (no temporary variables, no temporary tables), or write multiple queries and call one from another.

You also need to be aware of differences in functions; so SQL Server uses GetDate(), whereas Access uses NOW(). Rather than testing the year/month/day elements separately, I'd suggest it's more readable (and more efficient and less error prone) to just test the date part of the fields, so your date tests become AND DATEVALUE(OM.OrderTime) = DATEVALUE(NOW())

The Access ISNULL function simply returns a True/False value, so you need to combine this with an inline IIF statement. One downside of this is that any functions called within the ISNULL function have to be repeated if the result is not null, so there's a potentially significant performance hit. But hey, you're using Access, so performance clearly isn't a worry for you.

To get the two column result you're after, and separating out the lunch and breakfast components (i.e. some rows contribute to one column, other rows to another) you'll need to use a CASE statement, testing on the meal type. Access doesn't support SQL Server's CASE statement, but it does have a SWITCH which we can use in a similar way.

Putting this together, we get something like:
SELECT IIF(ISNULL(SUM(SWITCH(OM.MealType=1,OM.ItemQuantity,OM.MealType=2,0))),0,SUM(SWITCH(OM.MealType=1,OM.ItemQuantity,OM.MealType=2,0))) AS Breakfast,
       IIF(ISNULL(SUM(SWITCH(OM.MealType=2,OM.ItemQuantity,OM.MealType=1,0))),0,SUM(SWITCH(OM.MealType=2,OM.ItemQuantity,OM.MealType=1,0))) AS Lunch
FROM VW_EmpOrderMaster OM 
WHERE (OM.MealType=1 OR OM.MealType=2)
AND DATEVALUE(OM.OrderTime)=DATEVALUE(NOW());
This separates out the breakfast and lunch values; filters by date for TODAY only (I guess you will want to parameterise that, so replace the NOW() function with your appropriate query parameter); gives a zero value if there are no lunch/breakfasts found - and does it all in a single query of the table.

Your original request also adds in the number of visitors. Firstly, I'd query if that's really what you mean to do - it feels rather like adding apples and pears to give a number of oranges.

Assuming that's what you want, though, the approach I would probably take is to write a very similar query but against the visitors table; then write a third query that combines the results from the two. That will improve testability and avoid complicating the query still further, so you still have a hope of understanding what's going on! (It took me a while to match the brackets up correctly in the above, without making it all an order of magnitude more complex...) The above is all tested against sample data, btw.

Hopefully you can take it from here.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900