writing an sql query to display specific charges - sql

I am writing an sql query to display names of guests who have incurred both athletic and dinner charges on their itinerary. Can someone point me in the right direction as to setting up the query?
this is what i have but gives no result..
SELECT GUEST.FIRSTNAME, CHARGETYPE.DESCRIPTION
FROM GUEST, CHARGES, CHARGETYPE
WHERE CHARGETYPE.CHARGEID = CHARGES.CHARGEID
AND CHARGES.CHARGEID = '07'
AND CHARGES.CHARGEID = '03'
the chargeid 07 and 03 reflect athletic and dinner charges respectfully. im sorry for the lack of info, first time really using stackoverflow.
Here is the CHARGETYPE table
CHARGEID and DESCRIPTION are the column names
01 ROOM DEPOSIT
02 LUNCH SERVICE
03 DINNER SERVICE
04 BREAKFAST SERVICE
05 ROOM CHARGE
06 GIFTSHOP PURCHASE
07 ATHLETIC CHARGE
CHARGEID, ITINID, CHARGETIME, AMOUNT, GUESTID are the columns for the CHARGES table
01 001 11:00 -1850001
02 001 14:00 30 0001
03 001 18:00 55 0001
04 001 11:00 20 0001
05 001 20:00 20 0001
08 001 12:00 20 0001
01 002 16:00 -185 0002
02 002 16:30 40 0002
04 002 10:00 20 0002
05 002 14:00 25 0002
09 002 15:00 25 0002
01 003 10:00 -185 0003
02 003 12:00 30 0003
03 003 18:00 50 0003
04 003 11:00 18 0003
05 003 12:30 27 0003
06 003 14:45 30 0003
01 004 11:00 -185 0004
02 004 13:00 40 0004
03 004 19:00 60 0004
04 004 11:00 15 0004
05 004 20:00 34 0004
01 005 10:00 -185 0005
02 005 12:00 30 0005
03 005 17:00 30 0005
04 005 10:00 10 0005
05 005 16:00 25 0005
01 006 11:00 -185 0001
02 006 12:00 22 0001
03 006 18:00 40 0001
04 006 08:00 15 0001
05 006 20:00 60 0001
07 006 14:00 50 0001
01 007 11:00 -185 0003
02 007 12:00 20 0003
03 007 17:00 55 0003
04 007 10:00 15 0003
05 007 16:00 45 0003
01 008 11:00 -185 0005
02 008 13:00 20 0005
03 008 18:00 60 0005
04 008 09:00 20 0005
05 008 22:00 65 0005
07 008 12:00 50 0005
.
GUESTID LASTNAME FIRSTNAME are the columns for the GUEST table
0001 SMITH JOHN
0002 SMITH LISA
0003 ADAMS PETER
0004 ADAMS JANE
0005 JOHNSON DEAN

SELECT G.FIRSTNAME,G.LASTNAME, CT.DESCRIPTION
FROM GUEST G
INNER JOIN CHARGETYPE CT
ON G.GUESTID = C.GUESTID
INNER JOIN CHARGES
ON CT.CHARGEID = C.CHARGEID
WHERE C.CHARGEID IN ('07','03' );

Related

SQL statement for trial balance to display from table with one column of debit minus credit amount

I need trial balance from following tables:
Table: Journal
ID
TransactionDate
AccountCodeLevel1VarChar
AccountCodeLevel2VarChar
AccountCodeLevel3VarChar
AccountCodeLevel4VarChar
AccountCodeLevel5VarChar
AccountCodeLevel6VarChar
DebitAmountDecimal
CreditAmountDecimal
DescriptionVarchar
1
2022-1-1
1
01
01
01
001
001
1
Received cash from issuing common stocks
2
2022-1-1
3
01
01
01
001
001
1
Received cash from issuing common stocks
3
2022-1-1
1
01
01
01
001
001
2
Received cash borrowed from bank
4
2022-1-1
2
01
01
01
001
001
2
Received cash borrowed from bank
5
2022-1-1
1
01
01
01
001
001
3
Received cash borrowed from bank
6
2022-1-1
2
01
01
01
001
001
3
Received cash borrowed from bank
7
2022-1-1
1
01
01
01
001
001
4
Received cash from selling products
8
2022-1-1
4
01
01
01
001
001
4
Received cash from selling products
9
2022-1-1
1
01
01
01
001
001
5
Collected cash from services rendered
10
2022-1-1
4
02
01
01
001
001
5
Collected cash from services rendered
11
2022-1-1
5
01
01
01
001
001
6
Paid employee salaries
12
2022-1-1
1
01
01
01
001
001
6
Paid employee salaries
Table: AccountCode
AccountCodeLevel1VarChar
AccountCodeLevel2VarChar
AccountCodeLevel3VarChar
AccountCodeLevel4VarChar
AccountCodeLevel5VarChar
AccountCodeLevel6VarChar
AccountNameVarChar
1
01
01
01
001
001
Cash
2
01
01
01
001
001
Loan from banks
3
01
01
01
001
001
Paid-up common shares
4
01
01
01
001
001
Service revenue
4
02
01
01
001
001
Sales revenue
5
01
01
01
001
001
Salary
How can I get the SQL to render the summary result as shown hereunder with DebitAmountDecimal minus CreditAmountDecimal?
AccountCodeLevel1VarChar
AccountCodeLevel2VarChar
AccountCodeLevel3VarChar
AccountCodeLevel4VarChar
AccountCodeLevel5VarChar
AccountCodeLevel6VarChar
AccountNameVarChar
TrialBalanceAmount
1
01
01
01
001
001
Cash
9
2
01
01
01
001
001
Loan from banks
-5
3
01
01
01
001
001
Paid-up common shares
-1
4
01
01
01
001
001
Service revenue
-4
4
02
01
01
001
001
Sales revenue
-5
5
01
01
01
001
001
Salary
6
I tried to do with this SQL statement but the result is wrong:
SELECT
"ACC"."AccountCodeLevel1VarChar",
"ACC"."AccountCodeLevel2VarChar",
"ACC"."AccountCodeLevel3VarChar",
"ACC"."AccountCodeLevel4VarChar",
"ACC"."AccountCodeLevel5VarChar",
"ACC"."AccountCodeLevel6VarChar",
"ACC"."AccountNameVarChar",
SUM( "JNL"."DebitAmountDecimal" - "JNL"."CreditAmountDecimal" ) "TrialBalanceAmount"
FROM
"AccountCode" "ACC", "Journal" "JNL"
WHERE
"ACC"."AccountCodeLevel1VarChar" = "JNL"."AccountCodeLevel1VarChar"
AND
"ACC"."AccountCodeLevel2VarChar" = "JNL"."AccountCodeLevel2VarChar"
AND
"ACC"."AccountCodeLevel3VarChar" = "JNL"."AccountCodeLevel3VarChar"
AND
"ACC"."AccountCodeLevel4VarChar" = "JNL"."AccountCodeLevel4VarChar"
AND
"ACC"."AccountCodeLevel5VarChar" = "JNL"."AccountCodeLevel5VarChar"
AND
"ACC"."AccountCodeLevel6VarChar" = "JNL"."AccountCodeLevel6VarChar"
GROUP BY
"ACC"."AccountCodeLevel1VarChar",
"ACC"."AccountCodeLevel2VarChar",
"ACC"."AccountCodeLevel3VarChar",
"ACC"."AccountCodeLevel4VarChar",
"ACC"."AccountCodeLevel5VarChar",
"ACC"."AccountCodeLevel6VarChar",
"ACC"."AccountNameVarChar"
ORDER BY
"ACC"."AccountCodeLevel1VarChar" ASC,
"ACC"."AccountCodeLevel2VarChar" ASC,
"ACC"."AccountCodeLevel3VarChar" ASC,
"ACC"."AccountCodeLevel4VarChar" ASC,
"ACC"."AccountCodeLevel5VarChar" ASC,
"ACC"."AccountCodeLevel6VarChar" ASC
Thank you so much #Mark Rotteveel for the comment that I was summing NULL.
SUM( COALESCE ( "JNL"."DebitAmountDecimal", 0 ) ) - SUM( COALESCE ( "JNL"."CreditAmountDecimal", 0 ) ) "TrialBalanceAmount"
Now the result is complete.

Right join multiple key where mutiple key is null

I need help from captain obvious I suppose. I'm trying to Insert data from a table into a temptable. Ok this is easy
I need to insert the data we got today and the data we got 10 days ago. The where clause may aford it, th's okay
What for me is hard is to insert the data of today only if it does not appear in the data 10 days ago
An exemple of the table I use ([datatable]) :
Date Purchase Line_Purchase
---------------------------------------------------------------------------
2017-04-29 0000002 01
2017-04-29 0000002 02
2017-04-29 0000003 01
2017-04-29 0000003 02
2017-04-29 0000003 03
2017-04-29 0000004 01
2017-04-29 0000005 01
2017-04-19 0000001 01
2017-04-19 0000001 02
2017-04-19 0000001 03
2017-04-19 0000002 01
2017-04-19 0000002 02
My desired table temptable:
Input_date Purchase Line_Purchase
-------------------------------------------------------------------------
2017-04-19 0000001 01
2017-04-19 0000001 02
2017-04-19 0000001 03
2017-04-19 0000002 01
2017-04-19 0000002 02
2017-04-29 0000003 01
2017-04-29 0000003 02
2017-04-29 0000003 03
2017-04-29 0000004 01
2017-04-29 0000005 01
Is there any request possible in SQL that can change that ?
I tried this way
INSERT INTO #TEMPTABLE
(Input_date ,Purchase ,Line_Purchase)
SELECT
table.Date
,table.Purchase
,table.Line_Purchase
FROM
datatable table
WHERE
convert(date, table.Date) = convert(date, GETDATE() - 10)
INSERT INTO #TEMPTABLE
(Input_date ,Purchase ,Line_Purchase)
SELECT
table.Date
,table.Purchase
,table.Line_Purchase
FROM
datatable table
RIGHT JOIN #TEMPTABLE temp
on table.Purchase = temp.Purchase and table.Line_Purchase = temp.Line_Purchase
WHERE
convert(date, table.Date) = convert(date, GETDATE())
AND (temp.Purchase is null AND temp.Line_Purchase is null)
Thanks in advance
You can do this with not exists():
select date as Input_date, Purchase, Line_Purchase
into #temptable
from t
where date = '2017-04-19' --convert(date, getdate() - 10);
insert into #temptable (Input_date, Purchase, Line_Purchase)
select *
from t
where date = '2017-04-29'
and not exists (
select 1
from t as i
where i.purchase=t.purchase
and i.line_purchase=t.line_purchase
and i.date = '2017-04-19' --convert(date, getdate() - 10)
);
select *
from #temptable;
rextester demo: http://rextester.com/SAQSG21367
returns:
+------------+----------+---------------+
| Input_Date | Purchase | Line_Purchase |
+------------+----------+---------------+
| 2017-04-19 | 0000001 | 01 |
| 2017-04-19 | 0000001 | 02 |
| 2017-04-19 | 0000001 | 03 |
| 2017-04-19 | 0000002 | 01 |
| 2017-04-19 | 0000002 | 02 |
| 2017-04-29 | 0000003 | 01 |
| 2017-04-29 | 0000003 | 02 |
| 2017-04-29 | 0000003 | 03 |
| 2017-04-29 | 0000004 | 01 |
| 2017-04-29 | 0000005 | 01 |
+------------+----------+---------------+
Optionally, if you are doing both of these operations at the same time you can do it in the same query using a derived table/subquery or common table expression with row_number()
;
;with cte as (
select date, Purchase, Line_Purchase
, rn = row_number() over (partition by Purchase,Line_Purchase order by date)
from t
--where date in ('2017-09-26','2017-09-16')
where date in (convert(date, getdate()), convert(date, getdate()-10))
)
select date as Input_date, Purchase, Line_Purchase
into #temptable
from cte
where rn = 1
select *
from #temptable;
rextester demo: http://rextester.com/QMF5992
returns:
+------------+----------+---------------+
| Input_date | Purchase | Line_Purchase |
+------------+----------+---------------+
| 2017-09-16 | 0000001 | 01 |
| 2017-09-16 | 0000001 | 02 |
| 2017-09-16 | 0000001 | 03 |
| 2017-09-16 | 0000002 | 01 |
| 2017-09-16 | 0000002 | 02 |
| 2017-09-26 | 0000003 | 01 |
| 2017-09-26 | 0000003 | 02 |
| 2017-09-26 | 0000003 | 03 |
| 2017-09-26 | 0000004 | 01 |
| 2017-09-26 | 0000005 | 01 |
+------------+----------+---------------+

How to find sum of count, grouped by the id?

I would like to get the output for the following problem.
I have the following datatype:
id start end count Time Train
001 Paris London 01 05:00 Yes
001 Paris London 01 05:00 Yes
002 Prague Vienna 15 15:00 No
003 Frankfurt London 01 17:00 Yes
015 Paris London 08 21:00 No
019 Barcelona Vienna 15 15:00 No
003 Frankfurt London 01 07:00 Yes
002 Prague Vienna 15 05:00 No
I would like to find the sum of count, grouped by the id. Also ignore the rows that has the same id, start and end . Also I have a data of 4 gb and I would like to find the start and end city of top 5 count. Thank you.
I could like to get output that gives data something similar to this,
Prague -> Vienna Count : 15
Barcelona -> Vienna count : 15
Paris --> london Count : 09
Frankfurt -> London Count: 02
.....
You can use drop_duplicates + groupby with aggregating sum:
df['count'] = df['count'].astype(int)
df = df.drop_duplicates(['id','start','end'])
print (df)
id start end count Time Train
0 001 Paris London 1 05:00 Yes
2 002 Prague Vienna 15 15:00 No
3 003 Frankfurt London 1 07:00 Yes
4 015 Paris London 8 21:00 No
5 019 Barcelona Vienna 15 15:00 No
df1 = df.groupby('id', as_index=False)['count'].sum()
print (df1)
id count
0 001 1
1 002 15
2 003 1
3 015 8
4 019 15
df11 = df.groupby(['id', 'start', 'end'], as_index=False)['count'].sum()
print (df11)
id start end count
0 001 Paris London 1
1 002 Prague Vienna 15
2 003 Frankfurt London 1
3 015 Paris London 8
4 019 Barcelona Vienna 15
df12 = df.groupby(['start', 'end'], as_index=False)['count'].sum()
print (df12)
start end count
0 Barcelona Vienna 15
1 Frankfurt London 1
2 Paris London 9
3 Prague Vienna 15
For top values use nlargest:
df2 = df.nlargest(5, 'count')[['start','end']]
print (df2)
start end
2 Prague Vienna
5 Barcelona Vienna
4 Paris London
0 Paris London
3 Frankfurt London
SELECT T.* FROM
(
SELECT *,COUNT(id) AS count FROM TABLE1 GROUP BY id,start,end
) T
GROUP BY id ORDER BY count DESC LIMIT 0,5

Replicate a string names x times and ascribe replication number

I would like to replicate key names x number of times and have a separate column to indicate the replication number, e.g. let's say I have three key names as follows:
101
102
103
So, I would like each of the above numbers (names) replicated 3 times and to have a separate identifier number equal to 4 characters. It would therefore look like this:
101 0001
101 0002
101 0003
102 0001
102 0002
102 0003
103 0001
103 0002
103 0003
I guess this could be genered with a relatively straight forward awk script? *Edit: I would like to not specify the names to replicate in the script - it should be "replicate all names in this text file", as there are a lot of them (~400) and all with variable name types.
Thank you in advance!
In bash
echo {101,102,103}" "{01,02,03}
101 01 101 02 101 03 102 01 102 02 102 03 103 01 103 02 103 03
Following Fedorqui's advice for newlines
printf "%s\n" {101,102,103}" "{01,02,03}
101 01
101 02
101 03
102 01
102 02
102 03
103 01
103 02
103 03
Using the awk -v flag to pass a variable number which is the number of repeats for each line, and sprint to format the number into 2 decimal places with zero padding:
awk -v number_repeats=3 '{
for(i=0; i<number_repeats; i++) {
print $0, sprintf("%02d", i+1)
}
}'
If you don't mind GNU Parallel
parallel -k 'printf "%02d %02d\n"' ::: {6..12} ::: 1 2 3
06 01
06 02
06 03
07 01
07 02
07 03
08 01
08 02
08 03
09 01
09 02
09 03
10 01
10 02
10 03
11 01
11 02
11 03
12 01
12 02
12 03
Or, if your keys are in a file, called keys like this
32
45
78
You can read the file
parallel -k 'printf "%02d %02d\n" {1} {2}' :::: keys ::: 1 2 3
32 01
32 02
32 03
45 01
45 02
45 03
78 01
78 02
78 03

MS SQL query to return next start time and pervious end time

I have a table with columns, Machine, job_name, Start_time. Machine and job name are text and Start_time is Date/time.
I would like a query that will return the next chronological start time for a machine as the end time.
I have tried to use a MIN(start_time) > Start time. But this returns a row of Multiple start times for every possible end time. I am not sure how to identify the Start_time per row as the comparision start_time.
Table example.
MAchine, Job_name, Start_time
scanner, A1A, 1/24/2014 8:00am
scanner, ABA, 1/24/2014 12:00pm
scanner, A1B, 1/24/2014 10:00pm
scanner, AC3, 1/25/2014 4:00am
scanner, AG2, 1/25/2014 11:00am
scanner, BK4, 1/25/2014 3:00pm
scanner1, AA, 1/24/2014 9:00am
scanner1, AA, 1/24/2014 1:00pm
scanner1, AB, 1/24/2014 8:00pm
scanner1, A3, 1/25/2014 2:00am
scanner1, A2, 1/25/2014 7:00am
scanner1, B4, 1/25/2014 2:00pm
scanner2, A1, 1/24/2014 11:00am
scanner2, AB, 1/24/2014 12:00pm
scanner2, A1, 1/24/2014 5:00pm
scanner2, AC, 1/25/2014 1:00am
scanner2, A2, 1/25/2014 5:00am
scanner2, K4, 1/25/2014 2:00pm
I would like results below.
MAchine, Job_name, Start_time
scanner, A1A, 1/24/2014 8:00am, 1/24/2014 12:00pm
scanner, ABA, 1/24/2014 12:00pm, 1/24/2014 10:00pm
scanner, A1B, 1/24/2014 10:00pm, 1/25/2014 4:00am
scanner, AC3, 1/25/2014 4:00am, 1/25/2014 11:00am
scanner, AG2, 1/25/2014 11:00am, 1/25/201 3:00pm
scanner, BK4, 1/25/2014 3:00pm,
scanner1, AA, 1/24/2014 9:00am, 1/24/2014 1:00pm
scanner1, AA, 1/24/2014 1:00pm, 1/24/2014 8:00pm
scanner1, AB, 1/24/2014 8:00pm, 1/25/2014 2:00am
scanner1, A3, 1/25/2014 2:00am, 1/25/2014 7:00am
scanner1, A2, 1/25/2014 7:00am, 1/25/2014 2:00pm
scanner1, B4, 1/25/2014 2:00pm,
scanner2, A1, 1/24/2014 11:00am, 1/24/2014 12:00pm
scanner2, AB, 1/24/2014 12:00pm, 1/24/2014 5:00pm
scanner2, A1, 1/24/2014 5:00pm, 1/25/2014 1:00am
scanner2, AC, 1/25/2014 1:00am, 1/25/2014 5:00am
scanner2, A2, 1/25/2014 5:00am, 1/25/2014 2:00pm
scanner2, K4, 1/25/2014 2:00pm,
You will want to consider looking at the LAG and LEAD functions available in SQL Server 2012 and above.
This approach will work with most databases:
SQL Fiddle
MS SQL Server 2008 Schema Setup:
CREATE TABLE Table1
([MAchine] varchar(8), [Job_name] varchar(3), [Start_time] datetime)
;
INSERT INTO Table1
([MAchine], [Job_name], [Start_time])
VALUES
('scanner', 'A1A', '1/24/2014 8:00am'),
('scanner', 'ABA', '1/24/2014 12:00pm'),
('scanner', 'A1B', '1/24/2014 10:00pm'),
('scanner', 'AC3', '1/25/2014 4:00am'),
('scanner', 'AG2', '1/25/2014 11:00am'),
('scanner', 'BK4', '1/25/2014 3:00pm'),
('scanner1', 'AA', '1/24/2014 9:00am'),
('scanner1', 'AA', '1/24/2014 1:00pm'),
('scanner1', 'AB', '1/24/2014 8:00pm'),
('scanner1', 'A3', '1/25/2014 2:00am'),
('scanner1', 'A2', '1/25/2014 7:00am'),
('scanner1', 'B4', '1/25/2014 2:00pm'),
('scanner2', 'A1', '1/24/2014 11:00am'),
('scanner2', 'AB', '1/24/2014 12:00pm'),
('scanner2', 'A1', '1/24/2014 5:00pm'),
('scanner2', 'AC', '1/25/2014 1:00am'),
('scanner2', 'A2', '1/25/2014 5:00am'),
('scanner2', 'K4', '1/25/2014 2:00pm')
;
Query 1:
select t3.MAchine, t3.Job_name, t3.Start_time, tm.MinNextTime
from Table1 t3
left outer join (
select t1.MAchine, t1.Start_time, min(t2.Start_time) as MinNextTime
from Table1 t1
inner join Table1 t2 on t1.MAchine = t2.MAchine and t2.Start_time > t1.Start_time
group by t1.MAchine, t1.Start_time
) tm on t3.MAchine = tm.MAchine and t3.Start_time = tm.Start_time
order by t3.MAchine, t3.Start_time
Results:
| MACHINE | JOB_NAME | START_TIME | MINNEXTTIME |
|----------|----------|--------------------------------|--------------------------------|
| scanner | A1A | January, 24 2014 08:00:00+0000 | January, 24 2014 12:00:00+0000 |
| scanner | ABA | January, 24 2014 12:00:00+0000 | January, 24 2014 22:00:00+0000 |
| scanner | A1B | January, 24 2014 22:00:00+0000 | January, 25 2014 04:00:00+0000 |
| scanner | AC3 | January, 25 2014 04:00:00+0000 | January, 25 2014 11:00:00+0000 |
| scanner | AG2 | January, 25 2014 11:00:00+0000 | January, 25 2014 15:00:00+0000 |
| scanner | BK4 | January, 25 2014 15:00:00+0000 | (null) |
| scanner1 | AA | January, 24 2014 09:00:00+0000 | January, 24 2014 13:00:00+0000 |
| scanner1 | AA | January, 24 2014 13:00:00+0000 | January, 24 2014 20:00:00+0000 |
| scanner1 | AB | January, 24 2014 20:00:00+0000 | January, 25 2014 02:00:00+0000 |
| scanner1 | A3 | January, 25 2014 02:00:00+0000 | January, 25 2014 07:00:00+0000 |
| scanner1 | A2 | January, 25 2014 07:00:00+0000 | January, 25 2014 14:00:00+0000 |
| scanner1 | B4 | January, 25 2014 14:00:00+0000 | (null) |
| scanner2 | A1 | January, 24 2014 11:00:00+0000 | January, 24 2014 12:00:00+0000 |
| scanner2 | AB | January, 24 2014 12:00:00+0000 | January, 24 2014 17:00:00+0000 |
| scanner2 | A1 | January, 24 2014 17:00:00+0000 | January, 25 2014 01:00:00+0000 |
| scanner2 | AC | January, 25 2014 01:00:00+0000 | January, 25 2014 05:00:00+0000 |
| scanner2 | A2 | January, 25 2014 05:00:00+0000 | January, 25 2014 14:00:00+0000 |
| scanner2 | K4 | January, 25 2014 14:00:00+0000 | (null) |