473,767 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With INNER JOINS

1 New Member
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.d ate" 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 1284

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

Similar topics

3
6416
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, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
5
1633
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 table called Product, which contains a complete list of products. I have another table called Date, which contains a complete list of dates. I have a table called sales, which contains Product, date and Qty.
0
978
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 if you use a stored procedure or commandtext that contains one or more inner joins. im trying to implement a workaround that goes fine up to a particular step. I have a dataset that I bring to the client. This selectcommand statement contains...
5
1176
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 payment) thses queries calculate total dues and total payments customer made. then i have another query on the base of these 2 queries which is called custGrandBalance and it consist of customerID,amountDue(which is sum of all dues about this...
2
1623
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 = 'enclosure' and link_detail.x_id = 3 and link.idx = link_detail.linkidx and A.strandid = link.x_id_a and B.strandid = link.x_id_z would someone please convert this to a more efficient query using inner
6
9315
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: tblEmployees empId -- EmpName -- EmpRole -- EmpManager -------....------------.... ---------....--------------- 1........ dan yella..........1..........2
12
13191
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
11921
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: http://www.codinghorror.com/blog/archives/000976.html
1
1754
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 Table1.Field1, Table1.Field2 FROM Table1 INNER JOIN Table2 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3 INNER JOIN Table3 ON Table3.Key1 = Table2.Key1 AND Table3.Key3 = Table2.Key3 AND...
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10009
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.