How to join 2 tables with some of transpose row to columns - sql

for example i have 2 tables
info_table
id | Title | description
1 | title1 | dec1
2 | title2 | dec2
3 | title3 | dec3
Instance_Table
e_id | name | string
1 | date | 2015/01/19
2 | time | 10:00
3 | value | 10
1 | date | 2015/01/20
2 | time | 11:00
3 | value | 12
1 | date | 2015/01/21
2 | time | 12:00
3 | value | 13
What result expected:
id | Title | date | Time | value | Description
1 | title1 | 2015/01/19 | 10:00 | 10 | Des1
2 | title2 | 2015/01/20 | 11:00 | 11 | Des2
3 | title3 | 2015/01/21 | 12:00 | 13 | Des3

You should integrate a Foreign Key on the Instance_Table, and your e_id should be a Primary Key.
info_table
id | Title | description
1 | title1 | dec1
2 | title2 | dec2
3 | title3 | dec3
Instance_Table
e_id | name | string | FK_InfoTable
1 | date | 2015/01/19 | 1
2 | time | 10:00 | 1
3 | value | 10 | 1
4 | date | 2015/01/20 | 2
5 | time | 11:00 | 2
6 | value | 12 | 2
7 | date | 2015/01/21 | 3
8 | time | 12:00 | 3
9 | value | 13 |3
And with that kind of SQL Statement you should get what you want.
SELECT * FROM info_table INNER JOIN Instance_Table ON info_table.id = Instance_Table.FK_InfoTable
You can read on relationnal database here
Relationnal database WIKI

Related

MS Access SQL to get the average of two records

Currently, I am using MS Excel to do this but I would like to know if it is possible in MS Access.
I would like to get the average of a value from the previous hour and the current hour, and put that average value in the current hour record. One restriction is not to query the first record since it has no previous hour.
How should go about implementing the pseudo-sql code below?
SELECT Date
,Hour
,Node
,Average (Value,"Value from previous hour, e.g (Hour-1) with the same date and node")
FROM tblInput
WHERE Hour = 2,3,4
tblInput:
+----------+------+------+------+
| Date | Hour | Node |Value |
+----------+------+------+------+
| ... | ... | ... | ... |
| 1/1/2-18 | 1 | AAA | 5 |
| 1/1/2-18 | 2 | AAA | 10 |
| 1/1/2-18 | 3 | AAA | 15 |
| 1/1/2-18 | 4 | AAA | 20 |
| 1/1/2-18 | 1 | BBB | 4 |
| 1/1/2-18 | 2 | BBB | 8 |
| 1/1/2-18 | 3 | BBB | 12 |
| 1/1/2-18 | 4 | BBB | 16 |
| ... | ... | ... | ... |
+----------+------+------+------+
Output:
+----------+------+------+------+
| Date | Hour | Node | Ave |
+----------+------+------+------+
| 1/1/2-18 | 2 | AAA | 7.5 |
| 1/1/2-18 | 3 | AAA | 12.5 |
| 1/1/2-18 | 4 | AAA | 17.5 |
| 1/1/2-18 | 2 | BBB | 6 |
| 1/1/2-18 | 3 | BBB | 10 |
| 1/1/2-18 | 4 | BBB | 14 |
+----------+------+------+------+
It is:
SELECT
[Date],
Hour,
Node,
(Value +
(Select Value
From tblInput As T
Where
T.Node = tblInput.Node And
T.Date = tblInput.Date And
T.Hour = tblInput.Hour - 1)) / 2
FROM
tblInput
WHERE
Hour In (2,3,4)

Update columns based on record count and count total

I need some help writing a script to update a table.
The table has the following:
| StudentID | Name | Record | Label |
| 1 | Ed | 1 | 1 |
| 1 | Ed | 1 | 1 |
| 1 | Ed | 1 | 1 |
| 1 | Ed | 1 | 1 |
| 2 | Bob | 1 | 1 |
| 2 | Bob | 1 | 1 |
| 2 | Bob | 1 | 1 |
| 2 | Bob | 1 | 1 |
I would like to update the Record and Label columns, so that the query would increment the Record column from 1 to n for the same StudentId. The Label column would also need to be updated to display the record # of total number of records for that StudentId.
The result for Ed should be:
| StudentID | Name | Record | Label |
| 1 | Ed | 1 | 1 of 4 |
| 1 | Ed | 2 | 2 of 4 |
| 1 | Ed | 3 | 3 of 4 |
| 1 | Ed | 4 | 4 of 4 |
Hoping someone can help me with this.

writing SQL query to show result in specific order

I have this table
+----+--------+------------+-----------+
| Id | day_id | subject_id | period_Id |
+----+--------+------------+-----------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 8 | 2 | 6 | 1 |
| 9 | 2 | 7 | 2 |
| 15 | 3 | 3 | 1 |
| 16 | 3 | 4 | 2 |
| 22 | 4 | 5 | 1 |
| 23 | 4 | 5 | 2 |
| 24 | 4 | 6 | 3 |
| 29 | 5 | 8 | 1 |
| 30 | 5 | 1 | 2 |
to something like this
| Id | day_id | subject_id | period_Id |
| 1 | 1 | 1 | 1 |
| 8 | 2 | 6 | 1 |
| 15 | 3 | 3 | 1 |
| 22 | 4 | 5 | 1 |
| 29 | 5 | 8 | 1 |
| 2 | 1 | 2 | 2 |
| 2 | 1 | 2 | 2 |
| 16 | 3 | 4 | 2 |
| 23 | 4 | 5 | 2 |
| 30 | 5 | 1 | 2 |
+----+--------+------------+-----------+
SO, I want to choose one period with a different subject each day and doing this for number of weeks. so first subject dose not come until all subject have been chosen.
You can ORDER BY period_id first and then by day_id:
SELECT *
FROM your_table
ORDER BY period_Id, day_Id
LiveDemo

how to write a query to get multilevel data

I have four tables as below:
tblAccount
Id i sprimary key
+----+-----------------+
| Id | AccName |
+----+-----------------+
| 1 | AccountA |
| 2 | AccountB |
+----+-----------------+
tblLocation
Id is primary key.
+----+---------------+
| Id | LocName |
+----+---------------+
| 1 | LocationA |
| 2 | LocationB |
| 3 | LocationC |
+----+---------------+
tblAccountwiseLocation
Id i sprimary key.LocId and AccId are foreign key.
+----+---------------+---------------+
| Id | LocId | AccId |
+----+---------------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 1 | 2 |
| 5 | 2 | 2 |
| 6 | 3 | 2 |
+----+---------------+---------------+
tblRSCMaster
Id i sprimary key.LocId and AccId are foreign key.
+----+---------------+---------------+----------------+------------------+
| Id | LocId | AccId | RSCNo | DateOfAddition |
+----+---------------+---------------+----------------+------------------+
| 1 | 1 | 1 | Acc1_Loc1_1_14 | 15/01/2014 |
| 2 | 2 | 1 | Acc1_Loc2_1_14 | 15/01/2014 |
| 3 | 3 | 1 | Acc1_Loc2_1_14 | 15/01/2014 |
| 4 | 1 | 2 | Acc2_Loc1_1_14 | 15/01/2014 |
| 5 | 2 | 2 | Acc2_Loc2_1_14 | 15/01/2014 |
| 6 | 3 | 2 | Acc2_Loc3_1_14 | 15/01/2014 |
| 7 | 1 | 1 | Acc1_Loc1_2_14 | 15/02/2014 |
| 8 | 2 | 1 | Acc1_Loc2_2_14 | 15/02/2014 |
| 9 | 3 | 1 | Acc1_Loc3_2_14 | 15/02/2014 |
| 10 | 1 | 2 | Acc2_Loc1_2_14 | 15/02/2014 |
| 11 | 2 | 2 | Acc2_Loc2_2_14 | 15/02/2014 |
| 12 | 3 | 2 | Acc2_Loc3_2_14 | 15/02/2014 |
| 13 | 1 | 1 | Acc1_Loc1_3_14 | 15/03/2014 |
| 14 | 2 | 1 | Acc1_Loc2_3_14 | 15/03/2014 |
| 15 | 3 | 1 | Acc1_Loc3_3_14 | 15/03/2014 |
| 16 | 1 | 2 | Acc2_Loc1_3_14 | 15/03/2014 |
| 17 | 2 | 2 | Acc2_Loc2_3_14 | 15/03/2014 |
| 18 | 3 | 2 | Acc2_Loc3_3_14 | 15/03/2014 |
| 19 | 1 | 1 | Acc1_Loc1_4_14 | 15/04/2014 |
| 20 | 2 | 1 | Acc1_Loc2_4_14 | 15/04/2014 |
| 21 | 3 | 1 | Acc1_Loc3_4_14 | 15/04/2014 |
| 22 | 1 | 2 | Acc2_Loc1_4_14 | 15/04/2014 |
| 23 | 2 | 2 | Acc2_Loc2_4_14 | 15/04/2014 |
| 24 | 3 | 2 | Acc2_Loc3_4_14 | 15/04/2014 |
| 25 | 1 | 1 | Acc1_Loc1_5_14 | 15/05/2014 |
| 26 | 2 | 1 | Acc1_Loc2_5_14 | 15/05/2014 |
| 27 | 3 | 1 | Acc1_Loc3_5_14 | 15/05/2014 |
| 28 | 1 | 2 | Acc2_Loc1_5_14 | 15/05/2014 |
| 29 | 2 | 2 | Acc2_Loc2_5_14 | 15/05/2014 |
| 30 | 3 | 2 | Acc2_Loc3_5_14 | 15/05/2014 |
+----+---------------+---------------+----------------+------------------+
Acc1_Loc1_1_14 resembles RSC for LocationA of AccountA for Jan 2014.
I need to get a output as below from tblRSCMaster.
+---------------+---------------+----------------+------------------+
| LocId | AccId | RSCNo | DateOfAddition |
+---------------+---------------+----------------+------------------+
| 1 | 1 | Acc1_Loc1_3_14 | 15/03/2014 |
| 1 | 1 | Acc1_Loc1_4_14 | 15/04/2014 |
| 1 | 1 | Acc1_Loc1_5_14 | 15/05/2014 |
| 2 | 1 | Acc1_Loc2_3_14 | 15/03/2014 |
| 2 | 1 | Acc1_Loc2_4_14 | 15/04/2014 |
| 2 | 1 | Acc1_Loc2_5_14 | 15/05/2014 |
| 3 | 1 | Acc1_Loc3_3_14 | 15/03/2014 |
| 3 | 1 | Acc1_Loc3_4_14 | 15/04/2014 |
| 3 | 1 | Acc1_Loc3_5_14 | 15/05/2014 |
+---------------+---------------+----------------+------------------+
Each account has multiple locations and each location has multiple RSCs.
I need to get last three RSCs for each location for AccountA.
I have tried the below query:
SELECT tblAccountwiseLocation.LocId,tblAccountwiseLocation.AccId,tblRSCMaster.RSCNo,tblRSCMaster.DateOfAddition FROM tblAccountwiseLocation
INNER JOIN tblRSCMaster ON tblAccountwiseLocation.LocId= tblRSCMaster.LocId
where tblRSCMaster.AccId=1
But not getting the proper output.
Please help me out.
Thank you all in advance.
You can wrap the existing query inside a common table expression, and use ROW_NUMBER() to get only the last 3 (by tblRSCMaster.DateOfAddition) entries per tblAccountwiseLocation.LocId.
WITH cte AS (
SELECT tblAccountwiseLocation.LocId,
tblAccountwiseLocation.AccId,
tblRSCMaster.RSCNo,
tblRSCMaster.DateOfAddition,
ROW_NUMBER() OVER (PARTITION BY tblAccountwiseLocation.LocId
ORDER BY tblRSCMaster.DateOfAddition DESC) rn
FROM tblAccountwiseLocation
INNER JOIN tblRSCMaster
ON tblAccountwiseLocation.LocId = tblRSCMaster.LocId
AND tblAccountwiseLocation.AccId = tblRSCMaster.AccId
WHERE tblRSCMaster.AccId=1
)
SELECT LocId, AccId, RSCNo, DateOfAddition
FROM cte
WHERE rn <= 3
ORDER BY LocId, AccId, DateOfAddition
An SQLfiddle to test with.
Is this what you need?
select m.*
from (select m.*, row_number() over (partition by accID
order by DateOfAddition desc) as seqnum
from tblRSCMaster
where m.locid = 1
) m
where seqnum <= 3
order by AccId, DateOfAddition;
I think you need to filter on the locid rather than on the AccId to get what you want.

Ask about query in sql server

i have table like this:
| ID | id_number | a | b |
| 1 | 1 | 0 | 215 |
| 2 | 2 | 28 | 8952 |
| 3 | 3 | 10 | 2000 |
| 4 | 1 | 0 | 215 |
| 5 | 1 | 0 |10000 |
| 6 | 3 | 10 | 5000 |
| 7 | 2 | 3 |90933 |
I want to sum a*b where id_number is same, what the query to get all value for every id_number? for example the result is like this :
| ID | id_number | result |
| 1 | 1 | 0 |
| 2 | 2 | 523455 |
| 3 | 3 | 70000 |
This is a simple aggregation query:
select id_number, sum(a*b)
from t
group by id_number
I'm not sure what the first column is for.