I have to get data from 2 tables using join.1st table Name is sliprecord
+-------+-----------+-----------------------+---------+
| cid | cname | date | totalAmount |
+-------+-----------+-----------------------+---------+
| 8 | Umer | 2015-12-15 | 1000 |
| 9 | Shakir | 2015-12-20 | 2000 |
+-------+-----------+-----------------------+---------+
Another table Name is expense
+-------+-----------+-----------------------+---------+
| idExpense | title | date | amount |
+-------+-----------+-----------------------+---------+
| 1 | BreakFast | 2015-12-15 | 300 |
| 2 | Lunch | 2015-12-15 | 500 |
| 3 | Dinner | 2015-12-17 | 700 |
+-------+-----------+-----------------------+---------+
I want to create balance sheet, If sliprecord Table don't have a date in expense Table then it should also give me sliprecord date and sliprecord totalAmount .
And If expense don't have a date in sliprecord Table then it should also give me expense title,expense date,and expense amount .
Desired Out put should be like this:
+-------+-----------+-----------------------+---------+
| title | EXP_date | amount | Slip_date | totalAmount
+-------+-----------+-----------------------+---------+
| BreakFast | 2015-12-15 | 300 |2015-12-15 | 1000
| Lunch | 2015-12-15 | 500 | |
| Dinner | 2015-12-17 | 700 |
| | | |2015-12-20 | 2000
+-------+-----------+-----------------------+---------+
Related
I'm a bit of a beginner in sql and I need your help.
I'm sorry that I don't know if the question is correct.
now the code gives this;
+---------+----------+-------------+------------+
| id | CompanyId| DealId | price |
+---------+----------+-------------+------------+
| 1 | 1 | 1 | 100 |
| 2 | 1 | 2 | 50 |
| 3 | 1 | 3 | 25 |
| 4 | 2 | 1 | 1000 |
| 5 | 2 | 2 | 2000 |
| 6 | 2 | 3 | 2500 |
+---------+----------+-------------+------------+
but this is what i want;
+---------+----------+-------------+------------+------------+--+
| id | companyId| DealName1 | DealName2 | DealName3 | |
+---------+----------+-------------+------------+------------+--+
| 1 | 1 | 100 | 50 | 25 | |
| 2 | 2 | 1000 | 2000 | 2500 | |
| 3 | 3 | value | value | value | |
| 4 | 4 | value | value | value | |
+---------+----------+-------------+------------+------------+--+
select CompanyId
,[1] as DealName1
,[2] as DealName2
,[3] as DealName3
from (select CompanyId, DealId, price from t) t
pivot (sum(price) for DealId in([1],[2],[3])) p
CompanyId
DealName1
DealName2
DealName3
1
100
50
25
2
1000
2000
2500
Fiddle
I have a problem i needed to solve using sql server 2008. Below are the tables:
CommuterTrip_Details Table:
CREATE TABLE [dbo].[CommuterTrip_Details](
[TripID] [int] NOT NULL,
[TripStartStation] [varchar](20) NULL,
[TripStartTime] [time](7) NULL,
[TripEndStation] [varchar](20) NULL,
[TripEndTime] [time](7) NULL)
BusTrip_Times Table:
CREATE TABLE [dbo].[BusTrip_Times](
[BusID] [int] NOT NULL,
[StationID] [varchar](50) NULL,
[DepartTripTime] [time](7) NULL)
Below is the content of CommuterTrip_Details Table:
+------+----------------+------------------+---------------+------------------+
|TripID|TripStartStation| TripStartTime | TripEndStation| TripEndTime |
+------+----------------+------------------+---------------+------------------+
| 1 | Station_01 | 07:00:00.0000000 | Station_03 | 07:15:00.0000000 |
| 2 | Station_01 | 07:01:00.0000000 | Station_05 | 07:17:00.0000000 |
| 4 | Station_02 | 07:12:00.0000000 | Station_08 | 07:23:00.0000000 |
| 5 | Station_02 | 07:15:00.0000000 | Station_10 | 07:30:00.0000000 |
| 7 | Station_03 | 07:20:00.0000000 | Station_10 | 07:48:00.0000000 |
| 8 | Station_04 | 07:22:00.0000000 | Station_06 | 07:51:00.0000000 |
| 9 | Station_05 | 07:31:00.0000000 | Station_09 | 07:53:00.0000000 |
| 10 | Station_06 | 07:35:00.0000000 | Station_10 | 08:02:00.0000000 |
+------+----------------+------------------+---------------+------------------+
Below is the content of BusTrip_Times Table:
+-------+------------+------------------+
| BusID | StationID | DepartTripTime |
+-------+------------+------------------+
| 1 | Station_01 | 07:00:00.0000000 |
| 2 | Station_01 | 07:05:00.0000000 |
| 3 | Station_01 | 07:10:00.0000000 |
| 8 | Station_02 | 07:15:00.0000000 |
| 14 | Station_03 | 07:25:00.0000000 |
| 17 | Station_04 | 07:25:00.0000000 |
| 18 | Station_05 | 07:30:00.0000000 |
| 19 | Station_05 | 07:35:00.0000000 |
| 21 | Station_06 | 07:40:00.0000000 |
+-------+------------+------------------+
Desired Result:
enter image description here
Explanation:
BusID = 1, boarded 1 commuter in Station_01 then commuter alighted in Station_03. Same is true with BusID = 2.
BusID = 8, boarded 2 commuters in station_02, up till station_08, where 1 commuter alighted, and 1 remained till station_10
Rest of the bus has the same concept.
To count the boarded commuters, TripStartStation = StationID and TripStartTime <= DepartTripTime.
The TripStartTime must get the closest DepartTripTime. I manage to get this using ROW_NUMBER() OVER PARTITION.
I have got two tables. Table A contains the main data in every change state, Table B has the detail data.
Table A
| ID | Name |
| 1 | House |
| 2 | Tree |
| 3 | Car |
Table B
| ID | FK | DateTime | Color | Type |
| 1 | 1 | 2017-26-01T13:00:00 | Red | Bungalow |
| 2 | 2 | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | 1 | 2017-26-01T13:10:00 | Green | Bungalow |
| 4 | 3 | 2017-26-01T13:15:00 | Yellow | Smart |
| 5 | 1 | 2017-26-01T13:20:00 | White | Bungalow |
| 6 | 3 | 2017-26-01T13:25:00 | Black | Smart |
Result to watch
| ID | Name | DateTime | Color | Type |
| 1 | House | 2017-26-01T13:20:00 | White | Bungalow |
| 2 | Tree | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | Car | 2017-26-01T13:25:00 | Black | Smart |
The current state of an entity in Table A is described by the record with the youngest timestamp in Table B.
Now, I want to be nofitied, if an entity gets a new state (new record in Table B) or a new entity is created (new records in Table A and B), i.e. it could look like.
New result to watch
| ID | Name | DateTime | Color | Type |
| 1 | House | 2017-26-01T13:20:00 | White | Bungalow |
| 2 | Tree | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | Car | 2017-26-01T19:25:00 | Silver | Smart |
| 4 | Dog | 2017-26-01T20:25:00 | White / Black | Poodle |
With SqlDependency it is not possible to be notified for a statement which contains GROUP BY with MAX aggregate, window functions or TOP clause. So I have no idea how I can get the last detail data for an entity.
Is there any possibility to create a statement for this requirement or are there any other ways to be notified after changes for this result?
You can create and use SQL Triggers usin CLR
Here - SQL Trigger CLR
[DONE] for those who need this.. I will edit this and put the answer below..
How to make LName and FName all CAPS using subquery?
I already make all LName and FName CAPS but not in subquery using this code
Select UPPER(LName) AS "Lastname", UPPER(FName) AS "Firstname" FROM Trainers;
OUTPUT:
+-----------+-----------+
| Lastname | Firstname |
+-----------+-----------+
| MONTANO | GERALD |
| ALEJANDRO | LEONARD |
| MONTEZ | GARY |
| GUILLERMO | JACKIE |
| SANCHEZ | VERNA |
| GABORNI | MICHELLE |
+-----------+-----------+
Table Trainers
+-----------+-----------+----------+-------+
| TrainerNo | LName | FName | Rate |
+-----------+-----------+----------+-------+
| 00804871 | Montano | Gerald | 200 |
| 00807461 | Alejandro | Leonard | 175 |
| 00807462 | Montez | Gary | 150 |
| 00823253 | Guillermo | Jackie | 200 |
| 00827320 | Sanchez | Verna | 125 |
| 00845907 | Gaborni | Michelle | 150 |
+-----------+-----------+----------+-------+
ANSWER:
Select UPPER(Lname) AS "Lastname", UPPER(FName) AS "Firstname" from Trainers Where Rate <= (Select Max(Rate) from Trainers);
IF you want to return the first name concatenated with the second name in upper case in a sub-query. Then you can do this.
Select * from Table Name, (Select UPPER(LName) + UPPER(FName) FROM Trainers) as Name
Table Columns
or_cv_no
here is where 'cv_no' and 'or_no' stored
type
different types are 'CV' and 'OR'
amount
here is where 'cv_amt' and 'or_amt' stored
date
data that is being fetched has same date
my query is this
SELECT IIF(type = 'CV', or_cv_no) AS cv_no, IIF(type = 'CV', amount) AS cv_amt, IIF(type = 'OR', or_cv_no) AS or_no, IIF(type = 'OR', amount) AS or_amt FROM transacAmt
and the output is
how can i output it to look like this sample:
| cv_no | cv_amt | or_no | or_amt |
|---------------------------------|
| 1234 | 1,500 | 4321 | 1,200 |
|-------+--------+-------+--------|
| 7777 | 1,700 | 2222 | 1,760 |
|-------+--------+-------+--------|
| 1111 | 1,900 | 3333 | 1,530 |
|-------+--------+-------+--------|
| | | 5355 | 1,420 |
|-------+--------+-------+--------|
| | | 1355 | 4,566 |
EDIT: The output must be like this: marked 'x' must be removed.. or be filled by data