Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple subquery "select as" in SQL2005

Posted on 2011-03-02
1
Medium Priority
?
434 Views
Last Modified: 2012-05-11
Is there a more efficiant way to code the following query? (sorry I'm very new to SQL) I'm trying to build an asp page for inventory management. Here is a sample of the stored procedure to return the data. I have 50 products and have to keep joining tables becuase I'm exceeding the 256 subquery select limit in SQL2005.
tbl_inventoryreconcile has one entry per month, others have one entry per day.

Thanks
ALTER proc [dbo].[usp_GetInventoryMTDSelectedDate]
(
	@enddate datetime
)
as
select
	/*** product1 VALUES ***/
	(select product1_TOT from tbl_inventoryreconcile where datediff(m,@enddate,[datetime]) = -1) as 'product1_PREV_MONTH',
	(select product1_PROD from uv_InventoryData where datediff(d,[datetime],@enddate) = 0) as 'product1_PROD',
	(select sum(product1_PROD) from uv_InventoryData where datediff(m,[datetime],@enddate) = 0 and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate) and datepart(d,[datetime]) <= datepart(d,@enddate)) as 'product1_PROD_MTD',
	(select product1_CONS from uv_InventoryData where datediff(d,[datetime],@enddate) = 0) as 'product1_CONS',
	(select sum(product1_CONS) from uv_InventoryData where datediff(m,[datetime],@enddate) = 0 and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate) and datepart(d,[datetime]) <= datepart(d,@enddate)) as 'product1_CONS_MTD',
	(select product1_TOT from tbl_inventoryreconcile where datediff(m,@enddate,[datetime]) = -1) + sum(product1_PROD) - sum(product1_CONS) as 'product1_CALC',
	case
		when (select product1_TOT from tbl_inventoryreconcile where datepart(mm,[datetime]) = datepart(mm,@enddate) and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate)) is null then -9999
		else (select product1_TOT from tbl_inventoryreconcile where datepart(mm,[datetime]) = datepart(mm,@enddate) and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate))
	end as 'product1_TOT',
	case
		when (select product1_TOT from tbl_inventoryreconcile where datepart(mm,[datetime]) = datepart(mm,@enddate) and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate)) - (select product1_TOT from tbl_inventoryreconcile where datediff(m,@enddate,[datetime]) = -1) + sum(product1_PROD) - sum(product1_CONS) is null then 0
		else (select product1_TOT from tbl_inventoryreconcile where datepart(mm,[datetime]) = datepart(mm,@enddate) and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate)) - (select product1_TOT from tbl_inventoryreconcile where datediff(m,@enddate,[datetime]) = -1) + sum(product1_PROD) - sum(product1_CONS)
	end as 'product1_ADJ'
	
into #table1
from
	dbo.uv_InventoryData
	where
	datepart(dd,[datetime])= datepart(dd,@enddate)
	and datepart(mm,[datetime]) = datepart(mm,@enddate)
	and datepart(yyyy,[datetime]) = datepart(yyyy,@enddate)
group by
	cast(cast(datepart(mm,[DATETIME]) as varchar) + '/1/' + cast(datepart(yyyy,[DATETIME]) as varchar) as datetime)
order by
	cast(cast(datepart(mm,[DATETIME]) as varchar) + '/1/' + cast(datepart(yyyy,[DATETIME]) as varchar) as datetime)

Open in new window

0
Comment
Question by:AlamoAutomation
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 40

Accepted Solution

by:
lcohan earned 1000 total points
ID: 35020198
1. In the stored procedure you don't need to SELECT.....INTO #table1 if you dont' need to use that table as it's a waste.

2. This is what I use to "drop time" from a date time function and still make use of an existing index on the date column - just replace dateentered with getdate() to see the results:

select CONVERT(DATETIME,(CAST(MONTH(dateentered) AS VARCHAR(2))+'/'+CAST(DAY(dateentered) AS VARCHAR(2))+'/'+CAST(YEAR(dateentered) AS VARCHAR(4))),101)

3. I would add a WITH (NOLOCK) after each table in the FROM or JOIN to avoid locking/blocking

4. Make sure you have indexes on the WHERE and JOINed tables/columns
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question