Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T-SQL join same table

Posted on 2008-10-17
6
Medium Priority
?
713 Views
Last Modified: 2012-05-05
Hi, TableA has the fields: Name, Date, Colour, Volume

Ed, 13/12/2008, Blue, 22
Ed, 14/12/2008, Grey, 26
Tom, 13/12/2008, Red, 15
Tom, 14/12/2008, Blue, 18

I want to have a query that does this:

Select Name, Colour, Volume(today) - Volume(today-1)  as 'Current Volume'

So I would get this (joining on Name) for the date 14/12/2008:
Ed, Grey, 4
Tom, Blue, 3

So basically, for the volume field, it is to be the difference between the volume today and volume yesterday.  I will be turning this into a stored procedure, so you can assume variables to hold dates.

0
Comment
Question by:sublimation
[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
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22738560
a self-join, please take this as a starter:
SELECT t.*, p.*
  FROM TableA t
  LEFT JOIN TableA p
    ON p.Name = t.Name
   AND p.[Date] = Dateadd(day, -1, t.[Date])

Open in new window

0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22738758
select p.Name, sum(p.Volume) - sum(v.Volume)
from TableA p inner join TableA t on p.Name=t.Name
where p.[Date]=getDate() and t.[Date]=Dateadd(day,-1,t.[Date])
group by p.Name
0
 
LVL 6

Assisted Solution

by:divyeshhdoshi
divyeshhdoshi earned 600 total points
ID: 22738760
select p.Name, sum(p.Volume) - sum(t.Volume)
from TableA p inner join TableA t on p.Name=t.Name
where p.[Date]=getDate() and t.[Date]=Dateadd(day,-1,t.[Date])
group by p.Name
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 7

Expert Comment

by:Norush
ID: 22738920
Try with:
select p.name, (p.volume - t.volume)
from tblPerson p inner join tblPerson t on p.Name=t.Name
where p.[date] = convert(datetime, convert(varchar(24), GETDATE(), 111))
and t.[date] = convert(datetime, convert(varchar(24), GETDATE()-1, 111))

Open in new window

0
 
LVL 7

Accepted Solution

by:
Norush earned 1400 total points
ID: 22738922
Or:
select p.name, sum(p.volume - t.volume)
from tblPerson p inner join tblPerson t on p.Name=t.Name
where p.[date] = convert(datetime, convert(varchar(24), GETDATE(), 111))
and t.[date] = convert(datetime, convert(varchar(24), GETDATE()-1, 111))
group by p.name

Open in new window

0
 
LVL 7

Expert Comment

by:Norush
ID: 22738948
Please note that i used the getdate() function as the 'variable' and i used a table called tblPerson in my testscenario
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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