473,434 Members | 1,638 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,434 software developers and data experts.

Multiple Row Into Columns

8
Hi,

What I need to do next is basically, if you take the person below as an example, from the data below, there are three records for the 19th, so ignore 10:32a and combine 7:05p with 7:33p on the same row and do a duration calculation on another column. Also if the data is two row on the same date, like the 21st, combine the two data and do a calculation for the duration.

Name |EE # |State |First OutPunch |Second In Punch |Duration
John Smith |123456789 |GA |4/19/2010 10:32 |NULL |NULL
John Smith |123456789 |GA |4/19/2010 19:05 |NULL |NULL
John Smith |123456789 |GA |NULL |4/19/2010 19:33 |NULL
John Smith |123456789 |GA |4/21/2010 13:06 |NULL |NULL
John Smith |123456789 |GA |NULL |4/21/2010 13:38 |NULL

Would like the data to look like this:
Name |EE # |State |First OutPunch |Second In Punch |Duration
John Smith |123456789 |GA |4/19/2010 7:05 PM |4/19/2010 7:33 PM |28
John Smith |123456789 |GA |4/21/2010 1:06 PM |4/21/2010 1:38 PM |32

Thanks,

Ej
May 7 '10 #1
8 1750
Delerna
1,134 Expert 1GB
Something Like this
Expand|Select|Wrap|Line Numbers
  1. SELECT Name,[EE #],State,convert(datetime,left(max([First OutPunch]),11)  
  2.     max([First OutPunch]) as [First OutPunch], 
  3.     max([Second In Punch]) as [Second In Punch], 
  4.     datediff(s,max([First OutPunch]),max([Second In Punch])
  5. FROM
  6. (
  7.    SELECT Name,[EE #],State,
  8.       convert(datetime,left(isNull([First OutPunch],[Second In Punch]),11) as Dte,
  9.       [First OutPunch],
  10.       [Second In Punch]
  11.    FROM theTable
  12. )a
  13. GROUP BY Name,[EE #],State,Dte
  14.  
  15.  
  16.  
it's not tested
May 9 '10 #2
ejbatu
8
This worked great! Thank you
May 10 '10 #3
ejbatu
8
The script provided works great for the original scenario I provided. I just run into a scenario like below:

Name |EE # |State |First OutPunch |Second In Punch |Duration
John Smith |123456789 |GA |4/19/2010 19:05 |NULL |NULL
John Smith |123456789 |GA |NULL |4/19/2010 19:33 |NULL
John Smith |123456789 |GA |NULL |4/19/2010 20:32 |NULL
John Smith |123456789 |GA |4/21/2010 13:06 |NULL |NULL
John Smith |123456789 |GA |NULL |4/21/2010 13:38 |NULL

So I would like the data to look like this, where the 20:32 is ignored:
John Smith |123456789 |GA |4/19/2010 7:05 PM |4/19/2010 7:33 PM |28
John Smith |123456789 |GA |4/21/2010 1:06 PM |4/21/2010 1:38 PM |32

What is the best way to edit the script provided to accommodate the scenario above?
May 11 '10 #4
Delerna
1,134 Expert 1GB
Unless I am miss-understanding the question

In the first sample when 2 records existed the query needed to return the record with the latest date

In this sample the query needs to return the earliest date.

So the query needs to
sometimes return the latest
and
othertimes the earliest date


.....based on what ???


There must be some fact in the data that a query can use to make that decision. If such a fact does not exist in the data then it is not possible to do that.

How will the query know which one to choose?
May 11 '10 #5
ejbatu
8
Okay, sorry for not giving you the complete detail… Here a sample of data (separated by pipe |) that I’m working with (TableA and TableB). Basically, the second scenario I’m trying capture is where Jenny Gram on 5/1 punched out at 2:49PM and punched in at 3:25PM, so since there is a break, need duration calculated for that. On other hand, Bob Davis on 4/27 punched out at 6:19pm and punched in at 7:12pm, since it is a new reason, no need to capture it. Hope this makes sense

Expand|Select|Wrap|Line Numbers
  1. TableA
  2. Name|EE#|Event DT|In Punch|OutPunch|State|Reason
  3. John Smith|123456789|4/19/2010|4/19/2010 8:00 AM|4/19/2010 10:32 AM|TN|new
  4. John Smith|123456789|4/19/2010|4/19/2010 1:59 PM|4/19/2010 7:05 PM|TN|new
  5. John Smith|123456789|4/19/2010|4/19/2010 7:33 PM|4/19/2010 9:11 PM|TN|break A
  6. John Smith|123456789|4/21/2010|4/21/2010 9:54 AM|4/21/2010 1:06 PM|TN|new
  7. John Smith|123456789|4/21/2010|4/21/2010 1:38 PM|4/21/2010 6:03 PM|TN|break A
  8.  
  9. Bob Davis|234526854|4/27/2010|4/27/2010 9:59 AM|4/27/2010 2:44 PM|FL|new
  10. Bob Davis|234526854|4/27/2010|4/27/2010 3:23 PM|4/27/2010 6:19 PM|FL|break A
  11. Bob Davis|234526854|4/27/2010|4/27/2010 7:12 PM|4/27/2010 11:15 PM|FL|new
  12. Bob Davis|234526854|4/28/2010|4/28/2010 8:59 AM|4/28/2010 2:32 PM|FL|new
  13.  
  14. Jenny Gram|345685252|5/1/2010|5/1/2010 10:01 AM|5/1/2010 2:44 PM|CA|new
  15. Jenny Gram|345685252|5/1/2010|5/1/2010 2:45 PM|5/1/2010 2:49 PM|CA|break B
  16. Jenny Gram|345685252|5/1/2010|5/1/2010 3:25 PM|5/1/2010 5:23 PM|CA|break B
  17. Jenny Gram|345685252|5/2/2010|5/2/2010 2:30 PM|5/2/2010 5:38 PM|CA|new
  18. Jenny Gram|345685252|5/2/2010|5/2/2010 6:11 PM|5/2/2010 10:48 PM|CA|break B
  19.  
  20.  
  21. TableB
  22. Name|EE#|Event DT|In Punch|OutPunch|InClient|InUser|InFuncCode|OutClient|OutUser|OutFuncCode
  23. John Smith|123456789|4/19/2010|4/19/2010 8:00 AM|4/19/2010 10:32 AM|Download:Device 100052|PunchDevice|P|Download:Device 100052|PunchDevice|P
  24. John Smith|123456789|4/19/2010|4/19/2010 1:59 PM|4/19/2010 7:05 PM|sdfsdfd office|sdfsdfd|E|erterter office|erterter|E
  25. John Smith|123456789|4/19/2010|4/19/2010 7:33 PM|4/19/2010 9:11 PM|Download:Device 100052|PunchDevice|P|Download:Device 100052|PunchDevice|P
  26. John Smith|123456789|4/21/2010|4/21/2010 9:54 AM|4/21/2010 1:06 PM|Download:Device 100052|PunchDevice|P|Download:Device 100052|PunchDevice|P
  27. John Smith|123456789|4/21/2010|4/21/2010 1:38 PM|4/21/2010 6:03 PM|erterter office|erterter|E|Download:Device 100052|PunchDevice|P
  28.  
  29. Bob Davis|234526854|4/27/2010|4/27/2010 9:59 AM|4/27/2010 2:44 PM|Download:Device 100009|PunchDevice|P|erghjtt office|erghjtt|E
  30. Bob Davis|234526854|4/27/2010|4/27/2010 3:23 PM|4/27/2010 6:19 PM|erghjtt office|erghjtt|E|Download:Device 100009|PunchDevice|P
  31. Bob Davis|234526854|4/27/2010|4/27/2010 7:12 PM|4/27/2010 11:15 PM|sdfsdfd office|sdfsdfd|E|Download:Device 100052|PunchDevice|P
  32. Bob Davis|234526854|4/28/2010|4/28/2010 8:59 AM|4/28/2010 2:32 PM|Download:Device 100009|PunchDevice|P|Download:Device 100009|PunchDevice|P
  33.  
  34. Jenny Gram|345685252|5/1/2010|5/1/2010 10:01 AM|5/1/2010 2:44 PM|sdfsdfd office|sdfsdfd|E|Download:Device 100009|PunchDevice|P
  35. Jenny Gram|345685252|5/1/2010|5/1/2010 2:45 PM|5/1/2010 2:49 PM|Download:Device 100052|PunchDevice|P|erghjtt office|erghjtt|E
  36. Jenny Gram|345685252|5/1/2010|5/1/2010 3:25 PM|5/1/2010 5:23 PM|Download:Device 100052|PunchDevice|P|Download:Device 100009|PunchDevice|P
  37. Jenny Gram|345685252|5/2/2010|5/2/2010 2:30 PM|5/2/2010 5:38 PM|Download:Device 100052|PunchDevice|P|sdfsdfd office|sdfsdfd|E
  38. Jenny Gram|345685252|5/2/2010|5/2/2010 6:11 PM|5/2/2010 10:48 PM|Download:Device 100052|PunchDevice|P|Download:Device 100052|PunchDevice|P
  39.  
  40.  
  41. Output result:
  42. Name|EE#|State|1stOutPunchIdent|2ndInPunchIdent|firstOutPunch|secondInPunch|Duration
  43. John Smith|123456789|TN|erterter|Download:Device 100052|4/19/2010 7:05 PM|4/19/2010 7:33 PM|28
  44. John Smith|123456789|TN|Download:Device 100052|erterter|4/21/2010 1:06 PM|4/21/2010 1:38 PM|32
  45.  
  46. Bob Davis|234526854|FL|erghjtt|erghjtt|4/27/2010 2:44 PM|4/27/2010 3:23 PM|39
  47.  
  48. Jenny Gram|345685252|CA|Download:Device 100009|Download:Device 100052|5/1/2010 2:44 PM|5/1/2010 2:45 PM|1
  49. Jenny Gram|345685252|CA|erghjtt|Download:Device 100052|5/1/2010 2:49 PM|5/1/2010 3:25 PM|36
  50. Jenny Gram|345685252|CA|sdfsdfd|Download:Device 100052|5/2/2010 5:38 PM|5/2/2010 6:11 PM|33
  51.  
  52.  
May 12 '10 #6
ejbatu
8
Any word on this?... thanks!
May 14 '10 #7
Delerna
1,134 Expert 1GB
That data is a whole different scenario.
I took a look at the data and have been thinking upon your
problem when I have time.

It's not as simple as the scenario you painted in the orriginal post and I don't come here and I don't have time, to fill orders for free queries.
Rather I come here to assist people to overcome difficulties they may be experiencing writing their own solutions.

I don't know whether you have control over the design of the database but the tables, the way they are structured do not lend themselves to reliably query what you need.
For example,
Why are there 2 tables with the data in
Name|EE#|Event DT|In Punch|OutPunch
duplicated in both?
From what I can see there should only be 1 table
with these fields
Expand|Select|Wrap|Line Numbers
  1.    Name
  2.    EE#
  3.    Event DT
  4.    PunchTime
  5.    PunchType    IN or OUT
  6.    State
  7.    Reason
  8.    Client
  9.    User
  10.    FuncCode
  11.  
I don't suggest that this is absolutely how the table should look, I cannot see your database as a whole. This is merely a very simple example of how those two tables could be normalised and simplified.

Your query would then have been a matter of selecting
the punchtime where punchtype=out and reason= breakA or breakB and then matching that to the very next following record where punchtype=in


If you must keep the tables as they are then you need a query to massage that data into a form that looks somthing like the table I suggested. (It's a messy solution to improper normalization (see normalization-table-structures) and if you take this route you will probably find yourself taking this solution again and again further down the track)

I tend to write queries in stages progressively reaching the result I need.
Do that and if you have a difficulty getting some part of the query to work then post the query along with a clear description of the problem and we can assist you (not do it for you).
May 16 '10 #8
ejbatu
8
Okay, that is a good challenge. I'll work on it and get back to you... Thanks.
May 18 '10 #9

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

Similar topics

6
by: Dennis | last post by:
In CSS3 it looks like we'll have multiple column flowing of text (newspaper style) in which the number of columns can be determined automatically given the available horizontal space....
7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
0
by: David Londeck | last post by:
I am using Visual Basic 2003 and I have written a notepad like application using the RichTextBox control. I am having trouble trying to emulate Microsoft Words text block copy/paste feature. In...
3
by: Jane | last post by:
Hi, If anyone could help me with this, I greatly appreciate it. I would like to know how I can display data from a db in multiple columns rather than have it displayed in 1 column per recordset....
4
by: Cezar | last post by:
Hi, I need to display the content of an ArrayList on multiple columns, like data1 data2 data3 data4 data5 data6 data7 data8 ---- or data1 data3 data5 data7
2
by: Simon Harris | last post by:
Hi All, I have an app which requires a list of coutries to be displayed, so far I have a datagrid which contains displays the countries Ok, along with flag images, these are also links to my...
3
by: simchajoy2000 | last post by:
Hi, I am trying to use a VB.NET listview object to display information from a datatable. I need to have two columns of information but I don't want the user to be able to select each column...
2
by: scratchadere | last post by:
I am tryin to partition a listbox into multiple columns.Here is the real problem.I am getting data from MS ACCESS database.I want to display it juz like how an email account displays unread messages...
2
by: ray well | last post by:
i need to display 2 columns of data in a list box. how would i set this up IN CODE. say my table is tblNames, and i have 2 fields, FirstName, LastName, and want the data to show up in 2...
3
by: Will | last post by:
Can someone help with code to delete multiple columns from an excel spreadsheet? I know which columns I need to delete. The code below will delete a single column but I'm not sure how to delete...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
1
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
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...
0
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...
0
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...

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.