merge two rows into one row in sql? - sql

I have a table as :
Serveur
Total
Date
#01
200
2022-01-01 14:11:50.000
#01
100
2022-01-01 22:19:33.000
#02
150
2022-01-01 14:55:35.000
#02
300
2022-01-01 21:45:28.000
#03
500
2022-01-01 21:43:21.000
But I want the output as follows(I want to use this output to inner join with some other table) :
Serveur
Total_1
Total_2
#01
200
100
#02
150
300
#03
500
0

I guess you are using Microsoft SQL
Please follow the link below for syntax and examples on how to pivot the data within tables
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver16
Or you can use sun queries to pivot which is time consuming and dirty way of doing it.

Related

How to find the right budget value into the activities in Tableau (Relationship)

I have related two custom sql query in Tableau (via making relationship)
The outcome of the queries looks like :
Q1 : (It shows the starting time of the valid budget.If a user has multiple rows in this table, it means his/her budget has been updated with new amount)
id_user
budgete_start_date
budget_amount
1234
06-11-2021
120
1234
06-07-2022
200
56789
06-01-2022
1200
56789
06-07-2022
2000
643
05-05-2022
30
Q2 :(It shows the budget usage)
id_user
budgete_usage_date
amount_usage
1234
01-12-2021
50
1234
05-08-2022
100
56789
10-02-2022
60
56789
07-08-2022
500
643
05-07-2022
17
I need to find a way to create the following view to know what was the valid budget at
budgete_usage_date.
id_user
budgete_usage_date
amount_usage
valid budget
1234
01-12-2021
50
120
1234
05-08-2022
100
200
56789
10-02-2022
60
1200
56789
07-08-2022
500
2000
643
05-07-2022
17
30
How can I do that with calculated field in Tableau (with db made by relationship)?
If that's not possible, how can I do that directly in query? (changing the relationship to a single query)

aggregation at different levels, I need to get sum by type level in sql server

ID TYPE AMT PERIOD
-------------------------------------------
1001 1 4500 2008-09-30
1001 2 2333 2008-09-30
1001 2 -2333 2008-09-30
1001 1 -200 2008-09-30
1001 2 300 2008-09-30
Desired output:
ID TYPE AMT PERIOD
------------------------------------
1001 1 4300 2008-09-30
1001 2 300 2008-09-30
I need to get sum of the AMT but still needs to include other columns, please see the desired output as shown above.
I tried using grouping sets but can't get it right.
SELECT ID,TYPE,SUM(AMT) as Total_Amt,PERIOD
FROM table
GROUP BY ID,TYPE,PERIOD

SQL query Design

Table 1 : Employee
EmpId CreatedAt
100 2015-11-09 07:21:02
200 2017-01-24 18:24:01
300 2016-08-20 06:55:35
Table 2 : Account
AccId EmpID Currency CreatedAt
9000 100 USD 2017-04-20 19:40:55
9001 200 USD 2017-04-20 19:40:55
9002 100 EUR 2017-05-20 19:40:55
9003 200 USD 2017-04-20 19:40:55
9004 100 USD 2017-04-20 19:40:55
Table 3 : Transaction
TrnsId AccId Amount CreatedAt
10 9000 3000 2017-04-25 19:40:55
11 9001 500 2017-05-25 19:40:55
12 9000 -200 2017-05-30 19:40:55
13 9000 -500 2017-06-11 19:40:55
Create a table that provides the day end balance (at midnight) for each account since it was first created, i.e. there should be a single entry in the table for each day an account exists, and its balance at the end of that day.
Can anybody help me in writing query to above scenario?
Thanks.
Since you haven't posted any attempt to solve this yourself, I will assume you need an initial nudge in the right direction. Hopefully this will help.
Outer-Join your Account table to a table (create it if you don't have one) that has one row for every day in the calendar (this is often referred to as a "tally table"). Filter out the days in the calendar that were before the date the account was created.
That will produce a result of one row for every Account-Date combination, which is all the rows you want in your result.
From there it's just a matter of adding the column with the End-Of-Day balance for that Account on that Date. There are lots of ways to do this. Google "SQL Running Total" and pick a method you like.

Access the previous row in select

I have a scenario as below
--source data
departuredttm flight_source flight_destination available_seats
13-07-2016 04:00:00 A B 200
13-07-2016 08:00:00 A B 320
13-07-2016 08:20:00 A B 20
I have a lookup table which tell how many total passengers are there for this source and destinatin whose flights are delayed and needs to adjusted in available seats in source data.lookup table is like this.
--lookup table for passenger_from_delayed_flights
flight_source flight_destination passengers
A B 500
now I have to adjust these 500 passengers in available seats as in source data
---output
DepartureDttm flight_source flight_destination AVAILABLE_SEATS PASSENGERS_TO_ADJUST PASSENGER_LEFT
13-07-2016 04:00:00 A B 200 500 300
13-07-2016 08:00:00 A B 320 300 20
13-07-2016 08:20:00 A B 20 20 0
initially passenger to adjust is 500 where we have 200 seats , next 320 seats are available and we have to adjust 300 (500-200) passengers.
Please help
Thanks
Your expected result is probably wrong, the 2nd flight already has enough seats, so PASSENGER_LEFT should be -20 (or 0).
This is a calculation based on a running total:
passengers - SUM(available_seats)
OVER (ORDER BY departuredttm
ROWS UNBOUNDED PRECEDING) AS PASSENGER_LEFT
available_seats + PASSENGER_LEFT AS PASSENGERS_TO_ADJUST

How to Suppress Duplicate reocords in Total value for crystal report

This is Following details Called
For exam
Date type id acc amount
12/01/2 1 01 100 1000
12/01/2 2 02 200 3000
12/01/2 2 02 300 5000
Crystal report Total -----------------------
18000
But Actual Total for 9000 amount , How to solve issue
I tried in grouping also.
Pleas Help
Prakash
use the option Select Distinct Records in Database tab