473,411 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Help With INNER JOINS

Happy New Year everyone! I'm new to both T-SQL and this forum. I'm currently doing an internship and my first task was to create a small program which will send an email detailing the sales of the previous day versus monthly targets and sales. Most of the parts were figured out and eveything was done in Visual Studio. The gist of the code was written in one large chunk of SQL code, as below:

Expand|Select|Wrap|Line Numbers
  1. SELECT     derivedtbl_1.family AS 'Family', FLOOR(SUM(derivedtbl_1.sales)) AS 'Previous Day Sales', FLOOR(derivedtbl_2.target) AS 'Sales Target', 
  2.                       FLOOR(derivedtbl_4.totalSales) AS 'Total Sales', (CASE WHEN (FLOOR((derivedtbl_2.target - derivedtbl_4.totalSales) / derivedtbl_5.remDays) > 0) 
  3.                       THEN FLOOR((derivedtbl_2.target - derivedtbl_4.totalSales) / derivedtbl_5.remDays) ELSE 0 END) AS 'Required Sales', 
  4.                       FLOOR(derivedtbl_4.totalSales / derivedtbl_5.totalDays) AS 'Sales Average'
  5. FROM         (SELECT     Brand_Family_NEW.strSuperFamily AS family, 
  6.                                               SMS_RPT_SALES_NAT_DAY.qtySales + SMS_RPT_SALES_NAT_DAY.qtyShopSales AS sales, 
  7.                                               SMS_RPT_SALES_NAT_DAY.dteValue AS date
  8.                        FROM          SMS_RPT_SALES_NAT_DAY INNER JOIN
  9.                                               Brand_Family_NEW ON SMS_RPT_SALES_NAT_DAY.ideBrand = Brand_Family_NEW.ideBrand) AS derivedtbl_1 INNER JOIN
  10.                           (SELECT     TOP (1) dteValue
  11.                             FROM          SMS_RPT_TMP_SP010
  12.                             WHERE      (dteValue < DATEADD(d, - 1, GETDATE()))
  13.                             ORDER BY dteValue DESC) AS derivedtbl_6 ON derivedtbl_6.dteValue = derivedtbl_1.date INNER JOIN
  14.                           (SELECT     SUM(target) AS target, family
  15.                             FROM          (SELECT     SUM(SMS_RPT_TARGET.qtySalesTarget) AS target, SMS_RPT_TARGET.ideBrand, 
  16.                                                                            Brand_Family_NEW.strSuperFamily AS family
  17.                                                     FROM          SMS_RPT_TARGET INNER JOIN
  18.                                                                            Brand_Family_NEW ON SMS_RPT_TARGET.ideBrand = Brand_Family_NEW.ideBrand AND DATEDIFF(month, SMS_RPT_TARGET.dteTarget, GETDATE())= 0
  19.                                                     GROUP BY SMS_RPT_TARGET.ideBrand, Brand_Family_NEW.strSuperFamily) AS derivedtbl_3
  20.                             GROUP BY family) AS derivedtbl_2 ON derivedtbl_1.family = derivedtbl_2.family INNER JOIN
  21.                           (SELECT     SUM(SMS_RPT_SALES_NAT_DAY.qtySales) AS totalSales, Brand_Family_NEW.strSuperFamily AS family
  22.                             FROM          SMS_RPT_SALES_NAT_DAY INNER JOIN
  23.                                                    Brand_Family_NEW ON SMS_RPT_SALES_NAT_DAY.ideBrand = Brand_Family_NEW.ideBrand
  24.                             WHERE      (DATEDIFF(month, SMS_RPT_SALES_NAT_DAY.dteValue, GETDATE()) = 0)
  25.                             GROUP BY Brand_Family_NEW.strSuperFamily) AS derivedtbl_4 ON derivedtbl_4.family = derivedtbl_2.family CROSS JOIN
  26.                           (SELECT     COUNT(DISTINCT a.dteValue) + 1 AS remDays, COUNT(DISTINCT b.dteValue) - 1 AS totalDays
  27.                             FROM          SMS_RPT_TMP_SP010 AS a CROSS JOIN
  28.                                                    SMS_RPT_TMP_SP010 AS b
  29.                             WHERE      (a.dteValue > GETDATE()) AND (b.dteValue < GETDATE())) AS derivedtbl_5
  30. GROUP BY derivedtbl_1.family, derivedtbl_2.target, derivedtbl_4.totalSales, derivedtbl_5.remDays, derivedtbl_5.totalDays
  31. ORDER BY 'Previous Day Sales' DESC
I know this is messy code but I'm not good enough to do optimization (can anyone give me pointers on that?). The problem now is that when I try to change the conditions (lines 18 and 24)
Expand|Select|Wrap|Line Numbers
  1. DATEDIFF(month, SMS_RPT_TARGET.dteTarget, GETDATE())= 0
to
Expand|Select|Wrap|Line Numbers
  1. DATEDIFF(month, SMS_RPT_TARGET.dteTarget, derivedtbl_1.date)= 0
and
Expand|Select|Wrap|Line Numbers
  1. DATEDIFF(month, SMS_RPT_SALES_NAT_DAY.dteValue, GETDATE()) = 0
to
Expand|Select|Wrap|Line Numbers
  1. DATEDIFF(month, SMS_RPT_SALES_NAT_DAY.dteValue, derivedtbl_1.date) = 0
, it gives me a "The multi-part identifier "derivedtbl_1.date" could not be bound" error, twice. I know it's a problem with the nested joins but I don't know how to fix it. Please help me. Many thanks.
Jan 2 '08 #1
0 1267

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
5
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one...
0
by: news.microsoft.com | last post by:
first i apologize for the potential crosspost. this group gets far more traffic. now that i have that out of the way... ;) apparently using AutoGenerated Commands on Command objects does not work...
5
by: khan | last post by:
i have 2 queries totaldues and totalPayments totaldues has 2 fileds(custID (Group By) amountDue(sum of all invoices dues) and totalPayments consist of CustID(Group By) and Amount_Paid(sum of all...
2
by: gkellymail | last post by:
the following query works fine: select link.idx, link.x_table, link.x_id_a, link.x_id_z, a.strandid, b.strandid from link_detail, link, strand A, strand B where link_detail.x_table =...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
12
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
1
by: rando1000 | last post by:
I've got a query from SQL Server that I'm trying to carry over to Access. The Query uses 3 tables with two INNER JOINS, and each of the INNER JOINS has two operators, roughly like this: Select...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.