Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combining Queries Results On Same Table

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-12 : 06:37:18
Ok so I have for example the following query
SELECT form_data_id, user_id, CONVERT(VARCHAR(50), form_field_value) AS BookingDate
FROM form_data_tbl
WHERE (user_id = 30) AND (form_field_name = 'bookingDate')
UNION
SELECT form_data_id, user_id, CONVERT(VARCHAR(50), form_field_value) AS RoomID
FROM form_data_tbl AS form_data_tbl_1
WHERE (user_id = 30) AND (form_field_name = 'roomId')

And I have 6 or so more "Form_Field_Names" now when I run the following query I get returned

2 30 1
2 30 12/02/2010
3 30 1
3 30 12/02/2010
4 30 1
4 30 12/02/2010

Is it possible that it could return the results for each form_data_id on a single row so that i.e for 2 i get

2 30 1 12/02/2010
3 30 1 12/02/2010
4 30 1 12/02/2010

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-12 : 06:41:26
[code]SELECT form_data_id, user_id,
MAX( CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate,
MAX( CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomID
FROM form_data_tbl
WHERE (user_id = 30)
GROUP BY form_data_id, user_id
[/code]
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-12 : 07:07:51
Thanks that worked somewhat finally I also have a column startTime and StartMinute so I have BookingDate i.e 12/02/2009 Start Time ie 5 and Start Minute 15...

Is it possible also that at the end it sorts by BookingDate, then Start Hours, Then Start Minute

so i.e I would 12/02/2009 3 15
12/02/2009 3 30
12/02/2009 4 15
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-12 : 07:10:48
quote:
Originally posted by velnias2010

Thanks that worked somewhat finally I also have a column startTime and StartMinute so I have BookingDate i.e 12/02/2009 Start Time ie 5 and Start Minute 15...

Is it possible also that at the end it sorts by BookingDate, then Start Hours, Then Start Minute

so i.e I would 12/02/2009 3 15
12/02/2009 3 30
12/02/2009 4 15


sorry didnt get that. do you mean you've time part stored in separate field?
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-12 : 07:17:29
Yes Its Similar to What I had in first query
MAX( CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate,
MAX( CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomID

So that was BookingDate and RoomID

I have 2 other Columns Called Start Time and Start Minute so I would gather them as follows
MAX(CASE WHEN form_field_name = 'startMinute' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startMinute,
MAX(CASE WHEN form_field_name = 'startTime' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startTime

Finally I have BookingDate,StartMinute and StartTime (STart Time being the Hours)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-12 : 07:29:34
ok and you want to order based on them? then just use order by bookingdate,startminute,...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-12 : 07:40:19
I would have used a FULL OUTER JOIN:

SELECT COALESCE(T1.form_data_id, T2.form_data_id) AS form_data_id,
COALESCE(T1.user_id, T2.user_id) AS user_id,
CONVERT(VARCHAR(50), T1.form_field_value) AS BookingDate,
CONVERT(VARCHAR(50), T2.form_field_value) AS RoomID
FROM form_data_tbl AS T1
FULL OUTER JOIN form_data_tbl AS T2
ON T2.user_id = 30 AND T2.form_field_name = 'roomId'
WHERE T1.user_id = 30 AND T1.form_field_name = 'bookingDate'
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-12 : 09:10:48
Ok so basically I have
SELECT form_data_id, user_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS BookingDate,
MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomID,
MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startDate
FROM form_data_tbl
WHERE (user_id = 30)
GROUP BY form_data_id, user_id
ORDER BY startDate DESC

Which works fine (I got rid of Hours and Minutes just working with a single Date Object)

Now Im wondering...how could I set that only future dates should be allowed
i.e BookingDate > NOW()...when i try it says bookingDate is in invalid column and also im guessing columns needs to be DATETIME
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-12 : 09:57:12
Wrap your query in:

SELECT *
FROM
(
... query here ...
) AS X
WHERE bookingDate > GetDate()

but note that in the inner query you have converted bookingDate to VARCHAR, so the conversion and handling will be dubious.

Much better to leave as DATETIME datatype and handle formatting of the date to the application, but failing that covert it as the LAST step - i.e. replace the SELECT * in my example with a list of columns and data type conversion.

SELECT
form_data_id,
user_id,
[BookingDate] = CONVERT(varchar(24), BookingDate, 113), -- Choose your Date Format here
RoomID
FROM
(
SELECT COALESCE(T1.form_data_id, T2.form_data_id) AS form_data_id,
COALESCE(T1.user_id, T2.user_id) AS user_id,
-- NOTE: The date format needs to be recognised here!! 103 = dd/mm/yyyy
CONVERT(DATETIME, T1.form_field_value, 103) AS BookingDate,
CONVERT(INT, T2.form_field_value) AS RoomID -- RoomID is INT, right?
FROM form_data_tbl AS T1
FULL OUTER JOIN form_data_tbl AS T2
ON T2.user_id = 30 AND T2.form_field_name = 'roomId'
WHERE T1.user_id = 30 AND T1.form_field_name = 'bookingDate'
) AS X
WHERE BookingDate > GetDate()
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-13 : 05:25:40
Ah Cool...Another Thing Bothering me...

I would like to pass a 2 paramaters to the above query which are startDate and EndDate and I would like to compare them with all the other Start and EndDates in that table and see if they dont overlap with the ones in there. I.e can I Make a booking for then or is there dates overlapping with it. StartDate and EndDate are 2 other columns like BookingDate above
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 05:43:36
quote:
Originally posted by velnias2010

Ah Cool...Another Thing Bothering me...

I would like to pass a 2 paramaters to the above query which are startDate and EndDate and I would like to compare them with all the other Start and EndDates in that table and see if they dont overlap with the ones in there. I.e can I Make a booking for then or is there dates overlapping with it. StartDate and EndDate are 2 other columns like BookingDate above


just check that in where condition like

where (StartDate < @StartDate
and EndDate >@startdate)
or (StartDate <@Endate
and EndDate >@EndDate)
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-14 : 05:32:27
My Query is as follows :

SELECT user_id, form_data_id, BookingDate, roomId, CONVERT(varchar(24), startDate, 113) AS StartDate, CONVERT(varchar(24), endDate, 113) AS EndDate, RoomName
FROM (SELECT TOP (100) PERCENT form_data_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END)
AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS roomId,
MAX(CASE WHEN form_field_name = 'roomName' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomName,
MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS startDate,
MAX(CASE WHEN form_field_name = 'endDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS endDate, user_id
FROM form_data_tbl
GROUP BY form_data_id, user_id
ORDER BY startDate) AS X
WHERE (startDate > GETDATE()) AND (roomId = 7214)
ORDER BY startDate

Which Works fines

But when I have an entry in my DB as follows
13/12/2010 11:45:00
It Breaks

if i have
12/12/2010 11:45:00
11/12/2010 11:45:00

Its fine

How can I have it so that it will accept dates such as

14/12/2010 as this is how I would like the format to be
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-14 : 05:51:51
This is the Error I get by the way
System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-14 : 07:20:56
I think the issue is CONVERT(varchar(24), startDate, 113) AS StartDate doesnt seem to be creating as a proper date.

When I get rid of startDate > GETDATE(), the error System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value does not appear by orders the date by day value i.e maybe as a string ?

Then when I put it back in , I get the error
System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

And this is when I have startDate with values greater than 12 for day. i.e 19/12/2009.

But i dont think I have US formats somewhere cuz when i inserted GETDATE() it had proper UK Format.

Anyone around to help on this , im really stuck


Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-14 : 08:06:57
"CONVERT(varchar(24), startDate, 113) AS StartDate doesnt seem to be creating as a proper date."

Just to reiterate what I said above "Much better to leave as DATETIME datatype and handle formatting of the date to the application"

Note that in your code:

SELECT ...
FROM ...
(
SELECT ...
MAX(CASE WHEN form_field_name = 'startDate'
THEN CONVERT(VARCHAR(50), form_field_value)
ELSE NULL END
) AS startDate,
...
)
...
WHERE (startDate > GETDATE()) ...

So the StartDate used in the WHERE clause will be the VARCHAR(50) one from the inner select. SQL will attempt to convert the varchar data to a datetime (i.e. implicitly). The only reliable way to do this is if the data is in 'yyyymmdd' format. For anything else you will have to explicitly specify a convert method to indicate what format the date is in - e.g. 'dd/mm/yyyy' or 'mm/dd/yyyy'

Moreover, if you fail to do an explicit conversion, things such as '01/02/10' could be interpreted as 01-Feb-2010, 02-Jan-2010, or even 10-Feb-2001 !! and if your data doesn't happen to have a Day Number higher than 12 where SQL is anticipating a month you won't get any error message.

Store your dates in a datetime datatype columns; if you must store them as text (e.g. variant datatype) use yyyymmdd format. Anything else will be trouble, I'm afraid.
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-14 : 08:23:48
The Data type must remain NTEXT im afraid

I have this now

SELECT user_id, endDate, startDate, CONVERT(DATETIME, startDate, 113) AS BookingDate
FROM (SELECT TOP (100) PERCENT form_data_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END)
AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS roomId,
MAX(CASE WHEN form_field_name = 'roomName' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomName,
MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 103), 103) ELSE NULL END)
AS startDate, MAX(CASE WHEN form_field_name = 'endDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 101), 103) ELSE NULL
END) AS endDate, user_id
FROM form_data_tbl
GROUP BY form_data_id, user_id
ORDER BY startDate) AS X
WHERE (startDate > GETDATE())
ORDER BY startDate

which sorts the dates correctly...what I cannot yet do is compare the date i.e start Date with a manual date

i.e AND (startDate > '13/12/2009') Then i get that out of range error message again
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-01-14 : 08:30:49
But ye If I do AND (startDate > '20101201') its fine..

Anyway to get the (startDate > '13/12/2009') working with what I have
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-14 : 08:49:54
I think this

CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 103), 103)

should be

CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value), 103)

i.e. you only want to apply the "103" to the conversion to DateTime. It will probably be ignored when converting to Varchar though, so only important for "tidyness".

Beware that
CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value), 103)
will raise error if ANY of your form_field_value are not in valid 'dd/mmm/yyyy' format . There is an IsDate() function, but that has other problems associated with it (ask me if you do have potentially invalid dates in your form_field_value column)

"Anyway to get the (startDate > '13/12/2009') working with what I have"

Same issue here too, I'm afraid:

(startDate > CONVERT(datetime, '13/12/2009', 103))

obviously if you can input the string-date in yyyymmdd format then no conversion needed.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-14 : 08:50:48
P.S. You could also use string manipulation to convert '13/12/2009' to '20091213'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 09:46:32
quote:
Originally posted by velnias2010

The Data type must remain NTEXT im afraid

I have this now

SELECT user_id, endDate, startDate, CONVERT(DATETIME, startDate, 113) AS BookingDate
FROM (SELECT TOP (100) PERCENT form_data_id, MAX(CASE WHEN form_field_name = 'bookingDate' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END)
AS BookingDate, MAX(CASE WHEN form_field_name = 'roomId' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS roomId,
MAX(CASE WHEN form_field_name = 'roomName' THEN CONVERT(VARCHAR(50), form_field_value) ELSE NULL END) AS RoomName,
MAX(CASE WHEN form_field_name = 'startDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 103), 103) ELSE NULL END)
AS startDate, MAX(CASE WHEN form_field_name = 'endDate' THEN CONVERT(DATETIME, CONVERT(VARCHAR(50), form_field_value, 101), 103) ELSE NULL
END) AS endDate, user_id
FROM form_data_tbl
GROUP BY form_data_id, user_id
ORDER BY startDate) AS X
WHERE (startDate > GETDATE())
ORDER BY startDate

which sorts the dates correctly...what I cannot yet do is compare the date i.e start Date with a manual date

i.e AND (startDate > '13/12/2009') Then i get that out of range error message again


can i ask why? by making it ntext arent you making date manipulations difficult?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-14 : 09:54:10
I think form_field_value a "store anything" column, based on what what is in form_field_name (roomName, bookingDate, startDate, endDate)

Clearly not designed to be used as a relational database, and thus the problems that velnias2010 is encountering trying to make useful reports from it
Go to Top of Page
    Next Page

- Advertisement -