JOIN unrelated tables by t1.date (between t2.startdate and t2.enddate) - mysql

A SQL question, probably not the most difficult.
I'm making a view from a bunch of related table join on ID's -> easy. Now there is one table that hasn't got a key relationship with all the others. (BatchDates)
`ALTER VIEW [ECSUB].[FCT_Ext_Collection]
AS
SELECT sh.id AS idSubmissionHistory, dh.id, dd.id AS Description, sch.id AS idScoringHistory, sh.CreationDate, sh.UpdateDate, bd.id AS BatchDateID
FROM ECSUB.SubmissionHistory AS sh INNER JOIN EC.DocumentHistory AS dh ON sh.id = dh.idSubmissionHistory
LEFT OUTER JOIN ECSM.ScoringHistory AS sch ON sh.idScoringHistory = sch.id
LEFT OUTER JOIN EC.DocumentDescriptions AS dd ON dd.id = dh.Description
LEFT OUTER JOIN ECSUB.AddressBilling AS ab ON sh.id = ab.id
LEFT OUTER JOIN ECSUB.AddressPremise AS ap ON sh.id = ap.id
CROSS JOIN EC.BatchDates AS bd --ON sh.documentdate between .......
GO`
Well, my main table 'documentHistory' contains a document date, I have to define in which batch this falls.
Each batch has an ID and startdate. A batch is always one month long.
This will make it much more easy to understand, the data from the BatchDates table:
id month startdate
1 2010-12-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-01-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-02-01 00:00:00.000 2010-12-01 00:00:00.000
2 2011-03-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-04-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-05-01 00:00:00.000 2011-03-01 00:00:00.000
3 2011-06-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-07-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-08-01 00:00:00.000 2011-06-01 00:00:00.000
4 2011-09-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-10-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-11-01 00:00:00.000 2011-09-01 00:00:00.000
5 2011-12-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-01-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-02-01 00:00:00.000 2011-12-01 00:00:00.000
6 2012-03-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-04-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-05-01 00:00:00.000 2012-03-01 00:00:00.000
7 2012-06-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-07-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-08-01 00:00:00.000 2012-06-01 00:00:00.000
8 2012-09-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-10-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-11-01 00:00:00.000 2012-09-01 00:00:00.000
9 2012-12-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-01-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-02-01 00:00:00.000 2012-12-01 00:00:00.000
10 2013-03-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-04-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-05-01 00:00:00.000 2013-03-01 00:00:00.000
etc...........
So I need to fetch the batchID based on the documentdate, therefore we use the currentMonth of the column startdate.
Thus: ...JOIN BatchDates where documentDate is in startDate.month (there is no between here)
I don't even know if I need a join, cross join, union, etc...
Thanks in advance!
L

join BatchDates
on datepart(yyyy,[document date]) = datepart(yyyy,[startDate])
and datepart(mm,[document date]) = datepart(mm,[startDate])

Related

selecting duplicate values with a condition from a mysql table

I have the following table in mysql:
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
1 1234567 123 2013-12-31 3 10 2014-02-27 2014-03-10
2 1234567 123 2013-12-31 2 12 2014-03-10 9999-12-31
3 1234579 123 2013-12-31 3 12 2014-05-15 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
6 1224470 123 2015-11-11 2 12 2015-11-11 9999-12-31
7 1224471 123 2015-11-11 3 15 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
If there are records which has the same "FD", I need to get the ones which 's "FA" is "1", if exists.
Basically, I want this output.
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
I have tried the following code, but it gives a weird output:
Code:
SELECT T1.*
FROM findoc T1 LEFT JOIN
findoc T2
ON DATE(T1.`Financial_date`) = DATE(T2.`Financial_date`) AND T2.`Fig_audit` <> 1
WHERE T2.`Fig_audit` IS NULL OR T1.`Fig_audit` = 1
Output:
Key DI CI FD FA NM Valid_from Valid_to
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
It looks a complicated query, and I couldn't manage to do it.
How can I do it?
Thanks.
you can do case based aggregation to find out if there exists row with same date and with atleast one row with column value for FA as 1
select F.* from finddoc F
inner join
(
select fd , sum( case when fa = 1 then 1 else 0 end) as faOneCount
from finddoc
group by fd
) T
on (F.FD = T.FD and T.faOneCount = 1 AND F.FA =1)
or ( F.FD = T.FD and T.faOneCount =0 )
If I understand your question correctly (and I'm not sure I do), you could try something like this:
SELECT T1.*
FROM findoc T1
where T1.NM in (select distinct NM from findoc where FA = 1);
I'm assuming that NM is the common field for which you want to return all results if the FA for any of those NM entries is 1. But that could be a wrong assumption.

Get 30 minutes interval data between start and end time in mysql

I have table structure in mysql,
table_id no_people booking_date bookingend_time bookingstart_time
14 2 2014-10-31 2014-10-31 13:30:00 2014-10-31 11:00:00
5 4 2014-10-31 2014-10-31 16:30:00 2014-10-31 14:30:00
6 2 2014-10-31 2014-10-31 17:00:00 2014-10-31 16:00:00
2 4 2014-11-06 2014-11-06 12:30:00 2014-11-06 10:00:00
2 4 2014-10-31 2014-10-31 16:00:00 2014-10-31 14:00:00
3 4 2014-11-01 2014-11-01 09:00:00 2014-11-01 07:30:00
6 2 2014-11-01 2014-11-01 10:00:00 2014-11-01 07:30:00
2 4 2014-11-03 2014-11-03 10:30:00 2014-11-03 08:30:00
5 4 2014-11-04 2014-11-04 10:30:00 2014-11-04 08:30:00
3 4 2014-11-05 2014-11-05 09:30:00 2014-11-05 07:30:00
14 2 2014-11-05 2014-11-05 09:30:00 2014-11-05 07:30:00
I want to retrieve table_id data with 30 minutes of interval between start and end time.
Ex:
if i give booking start time 10:30 and end time 12:30 i should get 14 as row..
Similarly it should check all rows and return between two times ..
My query so far
SELECT `table_id` FROM `booking` WHERE bookingstart_time>='2014-10-31 10:30:00' AND bookingend_time<='2014-10-31 11:30:00'
Step 1: expand the input time frame by 30 minutes before and 30 minutes after. DATE_ADD() and DATE_SUB() can do that:
DATE_SUB(_input_start_date_here_, INTERVAL 30 MINUTE)
Step 2: rethink your problem in terms of start and end times. Here are the possible cases:
if the booking started during the (expanded) period, then you want this booking in your result
or if the booking started before the period, then you want this booking unless it also ended before the period
on the other hand, if the booking started after the period, then you do not want this booking
The first situation above could be expressed like this:
WHERE bookingstart_time >= DATE_SUB(_input_start_date_here_, INTERVAL 30 MINUTE)
AND bookingstart_time <= DATE_ADD(_input_end_date_here_, INTERVAL 30 MINUTE)
The second condition is left as an exercise. You can also rewrite the above with a more elegant BETWEEN operator.
SELECT restaurant_table FROM rest_restaurantbooking WHERE TIMESTAMPDIFF(SECOND, bookingstart_time, bookingend_time) > 1800.
FOR REFERENCE: HERE

How to get any three rows of particular date in SQL Server

I am stucking in one sql query.
My table format is the below:
Acct# AdmitDate DOS ChargeAmount
12366 2011-12-07 00:00:00.000 2011-12-15 00:00:00.000 25.00
12366 2011-12-07 00:00:00.000 2011-12-16 00:00:00.000 30.00
12366 2011-12-07 00:00:00.000 2011-12-17 00:00:00.000 55.00
12366 2011-12-07 00:00:00.000 2011-12-18 00:00:00.000 48.00
12366 2011-12-07 00:00:00.000 2011-12-19 00:00:00.000 25.00
12366 2012-01-08 00:00:00.000 2012-01-08 00:00:00.000 58.00
12366 2012-01-08 00:00:00.000 2012-01-09 00:00:00.000 46.00
12366 2012-01-08 00:00:00.000 2012-01-10 00:00:00.000 90.00
12366 2012-01-08 00:00:00.000 2012-01-11 00:00:00.000 52.00
12366 2012-01-08 00:00:00.000 2012-01-12 00:00:00.000 95.00
12366 2012-01-08 00:00:00.000 2012-01-13 00:00:00.000 53.20
I want only top 3 DOS by one Admit Date and sum of their ChargeAmount. All the other DOS should be neglact. I have no idea how to do this. I want the below desired output.
Acct# AdmitDate ChargeAmount
12366 2011-12-07 00:00:00.000 128.00
12366 2012-01-08 00:00:00.000 200.00
only 2011-12-17 to 2011-12-19 for Admit Date 2011-12-07 need to be sum and do the same for other Admit date.
If you have any sql query please share with me.
Thanks in advance
Try:
SELECT TOP 3 Acct, AdmitDate, SUM(ChargeAmount)
FROM YourTable
GROUP BY Acct, AdmitDate
Edit
SELECT Acct, AdmitDate, SUM(ChargeAmount)
, RANK() OVER (PARTITION BY AdmitDate ORDER BY AdmitDate DESC)
FROM YourTable
GROUP BY Acct, AdmitDate
Select
Acct#,
AdmitDate,
sum(ChargeAmount) ChargeAmount
from
(Select
Acct#,
AdmitDate,
row_number() over(partition by AdmitDate order by DOS desc) RowNo,
ChargeAmount
from TableName
where Acct# = #AcctNumber
) as t
where (RowNo = 1 or RowNo = 2 or RowNo = 3)
group by Acct#, AdmitDate
I have found the query. This query is working fine for me. Thanks for your efforts.

Double ORDER BY sort with UNION statement

(
SELECT *
FROM (
SELECT d
FROM myTable
WHERE id = "4h"
AND d < "2011-12-08 12:00:00"
ORDER BY d DESC
LIMIT 10
)tmp
ORDER BY d ASC
)
UNION (
SELECT d
FROM myTable
WHERE id = "4h"
AND d >= "2011-12-08 12:00:00"
ORDER BY d ASC
LIMIT 10
)
I'm trying to get the 10 results before and after a particular ID by using two SELECT statements and a UNION. The first SELECT uses ORDER BY DESC to get the 10 preceding and then I attempt to envelope that in a second ORDER BY ASC to get all the results in ASC order but for some reason it does not work.
Here is what I get currently for a result:
d
2011-12-08 08:00:00
2011-12-08 04:00:00
2011-12-08 00:00:00
2011-12-07 20:00:00
2011-12-07 16:00:00
2011-12-07 12:00:00
2011-12-07 08:00:00
2011-12-07 04:00:00
2011-12-07 00:00:00
2011-12-06 20:00:00 <- These top 10 results should ASC!
2011-12-08 12:00:00
2011-12-08 16:00:00
2011-12-08 20:00:00
2011-12-09 00:00:00
2011-12-09 04:00:00
2011-12-09 08:00:00
2011-12-09 12:00:00
2011-12-09 16:00:00
2011-12-09 20:00:00
2011-12-11 20:00:00
And here is what I want:
d
2011-12-06 20:00:00
2011-12-07 00:00:00
2011-12-07 04:00:00
2011-12-07 08:00:00
2011-12-07 12:00:00
2011-12-07 16:00:00
2011-12-07 20:00:00
2011-12-08 00:00:00
2011-12-08 04:00:00
2011-12-08 08:00:00
2011-12-08 12:00:00
2011-12-08 16:00:00
2011-12-08 20:00:00
2011-12-09 00:00:00
2011-12-09 04:00:00
2011-12-09 08:00:00
2011-12-09 12:00:00
2011-12-09 16:00:00
2011-12-09 20:00:00
2011-12-11 20:00:00
(
SELECT d
FROM myTable
WHERE id = '4h' AND d < '2011-12-08 12:00:00'
ORDER BY d DESC
LIMIT 10
) UNION ALL (
SELECT d
FROM myTable
WHERE id = '4h' AND d >= '2011-12-08 12:00:00'
ORDER BY d ASC
LIMIT 10
)
ORDER BY d ASC

MySQL Count Numbers Are Off

I am not sure why my numbers are drastically off from each other.
A query with no max id:
SELECT id, DATE_FORMAT(t_stamp, '%Y-%m-%d %H:00:00') as date, COUNT(*) as count
FROM test_ips
WHERE id > 0
AND viewip != ""
GROUP BY HOUR(t_stamp)
ORDER BY t_stamp ASC;
I get:
1 2012-07-18 19:00:00 1313
106 2012-07-18 20:00:00 1567
107 2012-07-19 09:00:00 847
225 2012-07-19 10:00:00 5095
421 2012-07-19 11:00:00 205
423 2012-07-19 12:00:00 900
461 2012-07-19 13:00:00 619
490 2012-07-20 15:00:00 729
575 2012-07-20 16:00:00 1682
1060 2012-07-20 17:00:00 2063
2260 2012-07-20 18:00:00 1417
5859 2012-07-20 21:00:00 1303
7060 2012-07-20 22:00:00 1340
8280 2012-07-20 23:00:00 1211
9149 2012-07-21 00:00:00 1675
10418 2012-07-21 01:00:00 721
11127 2012-07-21 02:00:00 825
But if I add a max id:
AND id <= 8279
I get:
1 2012-07-18 19:00:00 1313
106 2012-07-18 20:00:00 1201
107 2012-07-19 09:00:00 118
225 2012-07-19 10:00:00 196
421 2012-07-19 11:00:00 2
423 2012-07-19 12:00:00 38
461 2012-07-19 13:00:00 20
490 2012-07-20 15:00:00 85
575 2012-07-20 16:00:00 483
1060 2012-07-20 17:00:00 1200
2260 2012-07-20 18:00:00 1200
5859 2012-07-20 21:00:00 1201
7060 2012-07-20 22:00:00 1220
The numbers are WAY off from each other. Something is goofy.
EDIT: Here is my table structure:
id t_stamp bID viewip unique
1 2012-07-18 19:22:20 5 192.168.1.1 1
2 2012-07-18 19:22:21 1 192.168.1.1 1
3 2012-07-18 19:22:22 5 192.168.1.1 0
4 2012-07-18 19:22:22 3 192.168.1.1 1
You are not grouping by ID and I think you intend to.
Try:
SELECT id, DATE_FORMAT(t_stamp, '%Y-%m-%d %H:00:00') as date, COUNT(*) as count
FROM test_ips
WHERE id > 0
AND viewip != ""
GROUP BY id, DATE_FORMAT(t_stamp, '%Y-%m-%d %H:00:00')
ORDER BY t_stamp;
Your query is not consistent.
In your select statement you are displaying the full date.
But you are grouping your data by the hour. So your count statement is taking the count of all the data for each hour of the day.
As an example take your first result:
1 2012-07-18 19:00:00 1313
The count of 1313 contains the records for all of your dates (7/18, 7/19, 7/20, 7/21, 7/22, etc) that have an hour of 19:00.
But the way you have your query setup, it looks like it should be the count of all records for 2012-07-18 19:00:00.
So when you add AND id <= 8279" The dates of 7/21 and some of 7/20 or no longer being counted so your count values are now lower.
I'm guessing you are meaning to group by the date and hour and not just the hour.