I have existing data like this:
Table A
bill type amount
-----------------------------------
10 Food 100
11 Beverage 50
12 Food 200
Table Master GL:
Type D/C GL_CODE
-------------------------
Food D 12345
Food C 56789
Beverage D 88888
Beverage C 99999
I need to select all row in table A, insert this into below Table B
1 row in Table A will insert into 2 row in table B, also will select the GL code from table Master GL, to get the GL code.
D/C GL CODE Amount bill
-----------------------------------------
D 12345 100 10
C 56789 100 10
D 88888 50 11
C 99999 50 11
D 12345 200 12
C 56789 200 12
Can anyone advise me the query?
Thank you
you should join TableA with MasterGL on Type
In SQL Server
INSERT INTO TableB (DC, GL_CODE, Amount , bill)
SELECT M.DC,
M.GL_CODE,
A.Amount ,
A.bill
FROM TableA A
JOIN mastergl M
ON A.type = M.type
Related
I want to get a default rate for activity or an override rate if one exists in another table. How can I write SQL for this?
I have this query but it produces an error "every derived table must have its own alias".
select A.id, rate from (
select
A.id, coalesce(B.rate, A.rate) as rate
from
A
left join B on B.id = A.id
);
Consider the following data
table user
user_id name
1 johnny
2 sam
table activity_types
activity_type_id description rate
1 cook steak $12.00
2 flip burgers $9.00
3 wait tables $8.00
4 wash dishes $8.00
table personal_override_rates
user_id activity_type_id rate
1 1 $18
table activities
activity_id user_id activity_type_id qty
1 1 1 1
2 1 2 1
3 2 1 1
4 2 2 1
desired result:
johnny cook steak 1 $18.00
johnny flip burgers 1 $9.00
sam cook steak 1 $12.00
sam flip burgers 1 $9.00
The error you get is because you have a derived table without an alias. You must provide one and then select the id and rate using the alias.
Like this:
select derived_table.id, derived_table.rate from (
select
A.id, coalesce(B.rate, A.rate) as rate
from
A
left join B on B.id = A.id
) as derived_table;
eg
Names
id | name
1 abc
2 efg
Area
id | areaName
3 area1
4 area2
The query should return
id | name | areaid
1 abc 3
1 abc 4
2 efg 3
2 efg 4
This should give the ecpected result:
select a.id, a.name, b.id from names,area
When you want to join all records from table A to all records from table B (i.e. get a Cartesian product, you can use CROSS JOIN:
SELECT Names.id, Names.name, Area.areaid
FROM Names
CROSS JOIN Area
ORDER BY Names.id, Area.areaid
OK, so in MS Access I am trying to join two tables on two fields (customer ID and product type), have table A use a sum of each product type, and have all the records from table A so I can know what is missing from table B.
In table A, there are multiple records for each customer for each product type by year. But in table B there is only one record per product type. And in table B not all the product types are there.
Example Tables:
Table A:
Cust ID ProdType Year Number
1 A 2014 5
1 A 2013 8
1 B 2014 3
2 A 2014 13
2 C 2014 2
3 B 2014 1
3 C 2014 4
Table B:
Number
Cust ID ProdType Arrived
1 A 5
2 A 13
2 C 2
3 B 1
3 C 2
Final Result should look like:
Sum of Number
Cust ID ProdType Number Arrived
1 A 13 5
1 B 3
2 A 13 13
2 C 2 2
3 A 1 1
3 C 4 2
Try this,
MySQL Syntax
select a.cust_id, a.prodtype, sum(a.number), b.arrived
from table_a a left join table_b b on a.cust_id=b.cust_id and a.prodtype=b.prodtype
group by a.cust_id, a.prodtype
Here is DEMO (MySQL)
Ms-Access
select a.cust_id, a.prodtype, sum(a.number), b.arrived
from table_a a left join table_b b
on a.cust_id=b.cust_id and
on a.prodtype=b.prodtype
group by a.cust_id, a.prodtype
Say I have two tables as below where Table A has columns name and type where each name may appear many times and with different type and Table B has unique code, name and sum.
Table A
John Type1
Mark Type2
John Type1
Mark Type3
John Type4
Paul Type5
Table B
1 John 20
2 Mark 33
3 Paul 22
4 Mark 55
5 John 46
Now what I want is something like this:
Table C
1 John 20 Type1
2 Mark 33 Type2
3 Paul 22 Type5
4 Mark 55 Type2
5 John 46 Type1
Normally Table A should contain unique entries with one type for each name and I could do a right join Table B on name to get what I want. But now if I do right join I get duplicate entries on Table C because name has duplicates types in Table A. How do I solve this?
Try this
WITH TableAA
AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY NAME,TYPE) RN
,name
,type
FROM TableA
)
SELECT
B.*
,(
SELECT type from TableAA A WHERE A.name= B.name AND A.RN=
ISNULL(NULLIF((SELECT COUNT(1) FROM TableB C WHERE C.NAME=B.name
and C.no < B.no),0),1)
) AS Type
FROM
TableB B
SQLFiddle Demo
You can try this :
SELECT A.*,B.TYPE
FROM dbo.TABLE_2 A RIGHT JOIN
(
SELECT DISTINCT(NAME),MIN(TYPE)TYPE
FROM TABLE_1
GROUP BY NAME
) B ON A.NAME=B.NAME
ORDER BY CODE ASC
TABLE_1= TABLE A
TABLE_2= TABLE B
I have a SQL database with two tables
TableA (ID, State, Value)
1 England 20
2 France 50
3 USA 40
4 ........
5 ........
and
TableB (ID, username, age, stateID)
1 John 15 1
2 Adam 20 2
3 Jane 40 3
4 Scott 50 1
5 Edwin 60 2
6 Alex 20 3
7 Olsen 30 1
8 ...........
9 ...........
What I need is to update TableB by setting the age for all users
from England to be 20
and from France to be 50
and so on...
update tableB
set age = (select tableA.value from tableA where tableA.StateID=TableB.id)
I like this form below:
update b set
age = a.value
from tableB b
join tableA a on a.id = b.stateId
because you can write it this way (at last in SQL Server Management Studio):
update b set
age = a.value
--select b.age, a.value, b.*, a.*
from tableB b
join tableA a on a.id = b.stateId
then highlight the part from select ... to the end of query and execute it (F5) to check what you are going to change (value before and after).