SQL Query for range of dates - sql

Lets frame the question again---
table1{date, bID, sName, fID}
{11/05,B1,A1,P1}
{12/05,B2,A2,P2}
{13/05,B1,A3,P1}
{15/05,B3,A4,P1}
{16/05,B1,A5,P2}
{19/05,B1,A6,P2}
This is the table and the data stored in the table is also specified...
Now the query that i want is that:-
Depending of fId (lets say, P1 is selected) it should display the data from table say from 11/05-17/05 (no date should be missed). The data retrieved is as follows:-
11/05,B1,A1
12/05,--,--
13/05,B1,A3
14/05,--,--
15/05,B3,A4
16/05,--,--
17/05,--,--
The data retrieved for a particular fID(say P1) is displayed.. Explaning the result...
1) it displayed all data from 11/05-17/05 where fId is P1, if there is no date in the database, then also it should display null value for that date (i.e.14/05 date was not there in database, but still it displayed with a null value)..
2) if fId for that particular date is not P1, then also it store a null value in result set..
Atlast the data is retrieved in result set,, and processed further..
So i want to write the query for this problemm,, is it possible..

No code here, just my thoughts.
You need to create a temporary table with dates ranging from your begin date to an end date, inclusive. And then left join table1 with that temporary table on date column plus add where fID = ?.

As the other answer here mentions, a table with all the dates in it, and a LEFT JOIN is what you need.
Say you have this table:
CREATE TABLE table1
{
date DATETIME
bID VARCHAR(10),
sName VARCHAR(10),
fID VARCHAR(10)
}
and then this date-table:
CREATE TABLE dates
(
dt DATETIME
)
and in this table you need to have all the dates for the range you want to display. Usually you populate it with a couple of years in both directions, but that's up to you.
Note: For simplicity, I did not bother with primary keys in either table. You should of course make sure you have a primary key, and in the case of the dates table, it could be the dt column.
Then to display the results you want:
SELECT
dt,
bID,
sName
FROM
dates
LEFT JOIN table1 ON dt = date AND fld = 'P1'
ORDER BY
dt
Note that the selection of only P1 rows is done in the JOIN criteria. If you add a WHERE clause to do the same, you'll loose all dates that have no data.

Related

Dynamically Updating Columns with new Data

I am handling an SQL table with over 10K+ Values, essentially it controls updating the status of a production station over the day. Currently the SQL server will report a new message at the current time stamp - ergo a new entry can be generated for the same part hundreds of times a day whilst only having the column "Production_Status" and "TimeStamp" changed. I want to create a new table that selects unique part names then have two other columns that control bringing up the LATEST entry for THAT part.
I have currently selected the data - reordered it so the latest timestamp is first on the list. I am currently trying to do this dynamic table but I am new to sql.
select dateTimeStamp,partNumber,lineStatus
from tblPLCData
where lineStatus like '_ Zone %' or lineStatus = 'Production'
order by dateTimeStamp desc;
The Expected results should be a NewTable with the row count being based off how many parts are in our total production facility - this column will be static - then two other columns that will check Originaltable for the latest status and timestamp and update the two other columns in the newTable.
I don't need help with the table creation but more the logic that surrounds the updating of rows based off of another table.
Much Appreciated.
It looks like you could take advantage of a sub join that finds the MAX statusDate for each partNumber, then joins back to itself so that you can get the corresponding lineStatus value that corresponds to the record with the max date. I just have you inserting/updating a temp table but this can be the general approach you could take.
-- New table that might already exist in your db, I am creating one here
declare #NewTable(
partNumber int,
lineStatus varchar(max),
last_update datetime
)
-- To initially set up your table or to update your table later with new part numbers that were not added before
insert into #NewTable
select tpd.partNumber, tpd.lineStatus, tpd.lineStatusdate
from tblPLCData tpd
join (
select partNumber, MAX(lineStatusdate) lineStatusDateMax
from tblPLCData
group by partNumber
) maxStatusDate on tpd.partNumber = maxStatusDate.partNumber
and tpd.lineStatusdate = maxStatusDate.lineStatusDateMax
left join #NewTable nt on tbd.partNumber = nt.partNumber
where tpd.lineStatus like '_ Zone %' or tpd.lineStatus = 'Production' and nt.partNumber is null
-- To update your table whenever you deem it necessary to refresh it. I try to avoid triggers in my dbs
update nt set nt.lineStatus = tpd.lineStatus, nt.lineStatusdate = tpd.lineStatusDate
from tblPLCData tpd
join (
select partNumber, MAX(lineStatusdate) lineStatusDateMax
from tblPLCData
group by partNumber
) maxStatusDate on tpd.partNumber = maxStatusDate.partNumber
and tpd.lineStatusdate = maxStatusDate.lineStatusDateMax
join #NewTable nt on tbd.partNumber = nt.partNumber
where tpd.lineStatus like '_ Zone %' or tpd.lineStatus = 'Production'

Using case to select field based on value of another field

i have a database three fields: booking type, booking num and booking date. Some values are
Bookingtype Bookingnum date
main 0001 10-11-15
main 0002 09-11-15
sub 0002a 12-12-15
i need to write a select statement for the date for each record. But when the booking type is sub it should get the date from the main booking date. in this case booking 2a should have date 09-11-5..I'm using postgres 9.3 and have simplified the fields to make it easier for others to understand what i need.
Assuming your main numbers are always the first 4 characters of Bookingnum (a clear definition is missing):
SELECT b.Bookingtype, b.Bookingnum
, COALESCE(main.date, b.date) AS date
FROM bookings b
LEFT JOIN bookings main ON b.Bookingtype = 'sub'
AND main.Bookingnum = left(b.Bookingnum, 4);
I would consider a foreign key column (self-reference) to the main row.

I want to make SQL tables that are updated daily yet retain every single day's contents for later lookup. What is the best practice for this?

Basically I'm trying to create a database schema based around multiple unrelated tables that will not need to reference each other AFAIK.
Each table will be a different "category" that will have the same columns in each table - name, date, two int values and then a small string value.
My issue is that each one will need to be "updated" daily, but I want to keep a record of the items for every single day.
What's the best way to go about doing this? Would it be to make the composite key the combination of the date and the name? Or use something called a "trigger"?
Sorry I'm somewhat new to database design, I can be more specific if I need to be.
Yes, you have to create a trigger for each category table
I'm assuming name is PK for each table? If isnt the case, you will need create a PK.
Lets say you have
table categoryA
name, date, int1, int2, string
table categoryB
name, date, int1, int2, string
You will create another table to store changes log.
table category_history
category_table, name, date, int1, int2, string, changeDate
You create two trigger, one for each category table
Where you save what table gerate the update and what time was made.
create trigger before update for categoryA
INSERT INTO category_history VALUES
('categoryA', OLD.name, OLD.date, OLD.int1, Old.int2, OLD.string, NOW());
This is pseudo code, you need write trigger using your rdbms syntaxis, and check how get system date now().
As has already been pointed out, it is poor design to have different identical tables for each category. Better would be a Categories table with one entry for each category and then a Dailies table with the daily information.
create table Categories(
ID smallint not null auto_generated,
Name varchar( 20 ) not null,
..., -- other information about each category
constraint UQ_Category_Name unique( Name ),
constraint PK_Categories( ID )
);
create table Dailies(
CatID smallint not null,
UpdDate date not null,
..., -- Daily values
constraint PK_Dailies( CatID, UpdDate ),
constraint FK_Dailies_Category foreign key( CatID )
references Categories( ID )
);
This way, adding a new category involves inserting a row into the Categories table rather than creating an entirely new table.
If the database has a Date type distinct from a DateTime -- no time data -- then fine. Otherwise, the time part must be removed such as by Oracle's trunc function. This allows only one entry for each category per day.
Retrieving all the values for all the posted dates is easy:
select C.Name as Category, d.UpdDate, d.<daily values>
from Categories C
join Dailies D
on D.CatID = C.ID;
This can be made into a view, DailyHistory. To see the complete history for Category Cat1:
select *
from DailyHistory
where Name = 'Cat1';
To see all the category information as it was updated on a specific date:
select *
from DailyHistory
where UpdDate = date '2014-05-06';
Most queries will probably be interested in the current values -- that is, the last update made (assuming some categories are not updated every day). This is a little more complicated but still very fast if you are worried about performance.
select C.Name as Category, d.UpdDate as "Date", d.<daily values>
from Categories C
join Dailies D
on D.CatID = C.ID
and D.UpdDate =(
select Max( UpdDate )
from Dailies
where CatID = D.CatID );
Of course, if every category is updated every day, the query is simplified:
select C.Name as Category, d.UpdDate as "Date", d.<daily values>
from Categories C
join Dailies D
on D.CatID = C.ID
and D.UpdDate = <today's date>;
This can also be made into a view. To see today's (or the latest) updates for Category Cat1:
select *
from DailyCurrent
where Name = 'Cat1';
Suppose now that updates are not necessarily made every day. The history view would show all the updates that were actually made. So the query shown for all categories as they were on a particular day would actually show only those categories that were actually updated on that day. What if you wanted to show the data that was "current" as of a particular date, even if the actual update was several days before?
That can be provided with a small change to the "current" query (just the last line added):
select C.Name as Category, d.UpdDate as "Date", d.<daily values>
from Categories C
join Dailies D
on D.CatID = C.ID
and D.UpdDate =(
select Max( UpdDate )
from Dailies
where CatID = D.CatID
and UpdDate <= date '2014-05-06' );
Now this shows all categories with the data updated on that date if it exists otherwise the latest update made previous to that date.
As you can see, this is a very flexible design which allows access the data just about any way desired.

How to create a query on 2 existing tables and build a table(view) with data from both tables and a condition?

sorry the title might be a little bit confusing.
I will try to explain in full here and add some tables as examples.
Ok, what I have is a MS-SQL database that I use to store data/info coming from equipments that are mounted in some vehicles.
For the moment there are 2 tables in the database: "Communication" - a big table Used to store every information from the equipments when they connect to the TCP-server. Records are added one after another (only INSERTS here).
The table looks like this:
The second table "EquipmentStatus" - it has EquipmentID as a primary key, so it is a smaller table (only UPDATES here), and it looks like this:
Well, what I would need is a new table with Vehicles that DID communicate between Date_1 and Date_2, i.e. a SQL query that can provide this:
where Date_1 and Date_2 have been set to 20 respectively 22 of June).
Also, the next step would be to get also a table(view) for Vehicles that DID NOT communicate between Date_1 and Date_2, i.e. the one you see below:
Thanks for your time and patience!
I took a stab at these without having the data on my end, but this should get you on the right path.
A:
SELECT DISTINCT s.Vehicle_Number, s.Vehicle_Status, s.DateLastCommunication
FROM EquipmentStatus s
INNER JOIN Communications c
ON s.Equipment_ID = c.Equipment_ID
WHERE c.DateTimeCommunication BETWEEN '2015-06-20' AND '2015-06-22'
B:
SELECT DISTINCT s.Vehicle_Number, s.Vehicle_Status, s.DateLastCommunication
FROM EquipmentStatus s
INNER JOIN Communications c
ON s.Equipment_ID = c.Equipment_ID
WHERE s.Equipment_ID NOT IN
(SELECT Equipment_ID FROM Communications WHERE DateTimeCommunication
BETWEEN '2015-06-20' AND '2015-06-22')
is this what you are looking for?
declare #startDate datetime, #enddate datetime
--vehicles communicated
select A.Vehicle_Number, b.Vehicle_Status, A.DataLastCommunication
from (
Select Vehicle_Number, MAX(DataLastCommunication) as DataLastCommunication
from dbo.Communications
where DataLastCommunication between #startDate and #enddate
group by Vehicle_Number
) as A
inner join dbo.EquipmentStatus b
on a.Vehicle_Number=b.Vehicle_Number
and a.DataLastCommunication = b.DataLastCommunication
--vehicles did not communicate
select a.Vehicle_Number, a.Vehicle_Status, a.DataLastCommunication
from dbo.EquipmentStatus a
where not exists(
select 1
from dbo.Communications
where Vehicle_Number=a.Vehicle_Number
and DataLastCommunication between #startDate and #enddate
)
The last query should use a Vehicles table instead of the communications table because it could be possible that there is a new vehicle which has no entries yet in any of these 2 tables (which will return the vehicle number but the status and date will be null)....
In sql-server, you can use functions to return the two different set of data, just like two new tables:
create function f_has_comm(#date1 datetime, #date2 datetime)
returns table
as
return (select Vehicle_Number, Vehical_status, DateLastCommunication
from EquipmentStatus where EquipmentID in (select EquipmentID from Communication
where DatetimeCommunication>=#date1 and DatetimeCommunication<=#date2)
)
go
create function f_no_comm(#date1 datetime, #date2 datetime)
returns table
as
return (select Vehicle_Number, Vehical_status, DateLastCommunication
from EquipmentStatus where EquipmentID NOT IN (select EquipmentID from Communication
where DatetimeCommunication>=#date1 and DatetimeCommunication<=#date2)
)
go
and now you can select from the two functions like:
select * from f_has_comm('2015-6-1','2015-6-20');
select * from f_no_comm('2015-6-1','2015-6-20');

Assign unique ID's to three tables in SELECT query, ID's should not overlap

I am working on SQL Sever and I want to assign unique Id's to rows being pulled from those three tables, but the id's should not overlap.
Let's say, Table one contains cars data, table two contains house data, table three contains city data. I want to pull all this data into a single table with a unique id to each of them say cars from 1-100, house from 101 - 200 and city from 300- 400.
How can I achieve this using only select queries. I can't use insert statements.
To be more precise,
I have one table with computer systems/servers host information which has id from 500-700.
I have another tables, storage devices (id's from 200-600) and routers (ids from 700-900). I have already collected systems data. Now I want to pull storage systems and routers data in such a way that the consolidated data at my end should has a unique id for all records. This needs to be done only by using SELECT queries.
I was using SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS UniqueID and storing it in temp tables (separate for storage and routers). But I believe that this may lead to some overlapping. Please suggest any other way to do this.
An extension to this question:
Creating consistent integer from a string:
All I have is various strings like this
String1
String2Hello123
String3HelloHowAreYou
I Need to convert them in to positive integers say some thing like
String1 = 12
String2Hello123 = 25
String3HelloHowAreYou = 4567
Note that I am not expecting the numbers in any order.Only requirement is number generated for one string should not conflict with other
Now later after the reboot If I do not have 2nd string instead there is a new string
String1 = 12
String3HelloHowAreYou = 4567
String2Hello123HowAreyou = 28
Not that the number 25 generated for 2nd string earlier can not be sued for the new string.
Using extra storage (temp tables) is not allowed
if you dont care where the data comes from:
with dat as (
select 't1' src, id from table1
union all
select 't2' src, id from table2
union all
select 't3' src, id from table3
)
select *
, id2 = row_number() over( order by _some_column_ )
from dat