SQL Computed Column, CountIF 2 Tables - sql

I have two tables
tblData_VendorMasterSSPaymentTerms
tblData_VendorMasterSSPaymentTermsCLM
tblData_VendorMasterSSPaymentTerms contains a field labled VMSSPayTerms_AribaContractID which the values exist in table tblData_VendorMasterSSPaymentTermsCLM
So in table tblData_VendorMasterSSPaymentTermsCLM I want to create a calculated column that counts how many records in tblData_VendorMasterSSPaymentTerms contains the Contract ID for that record.
This is what I have put together so far but it is still coming up with an error
SELECT Count(VMSSPayTerms_AribaContractID)
From tblData_VendorMasterSSPaymentTerms
Where VMSSPayTerms_AribaContractID=VMSSPayTermsCLM_ContractID
Can someone help me identify what I am doing wrong here?

You must join the tables, group by VMSSPayTermsCLM_ContractID and count:
select
c.VMSSPayTermsCLM_ContractID,
count(t.VMSSPayTerms_AribaContractID) counter
from tblData_VendorMasterSSPaymentTermsCLM c inner join tblData_VendorMasterSSPaymentTerms t
on t.VMSSPayTerms_AribaContractID = c.VMSSPayTermsCLM_ContractID
group by c.VMSSPayTermsCLM_ContractID

Related

SQL select with three tables

Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!

Outputting the data from several sql tables without having a common value

I have a select query which combined several tables. PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS and SERVICE_GUARANTY_NEW have common value however STOCKS table does not.
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT,
workcube_test_1.PRODUCTION_ORDERS PR_ORDERS,
workcube_test_1.SERVICE_GUARANTY_NEW SG,
workcube_test_1.STOCKS S
WHERE PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
AND PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
when I run the query, it shows the output as below.
The problem here is there are four data rows returned from PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS, SERVICE_GUARANTY_NEW and once I have added the STOCKS table, arrived_city, montaj_date, transfer_date columns are side by side with STOCKS table's rows, but the columns value should be null, not filled with data.
The way I tried is UNION of STOCKS table, however unioned table values are ignored, can not use them in html blocks.
there needs to be at least one more join condition among tables where there's for STOCKS table, I think there might exist such a column STOCK_ID within a table such as PRODUCTION_ORDER_RESULTS in order to join with STOCKS table. I think this should be the reason for multiple returning rows. If there's no common column, then the returning data will be produced as many as the number of records within STOCK table due to existing CROSS JOIN logic within the current query. So rearrange your query as
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT
JOIN workcube_test_1.PRODUCTION_ORDERS PR_ORDERS
ON PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
JOIN workcube_test_1.SERVICE_GUARANTY_NEW SG
ON PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
JOIN workcube_test_1.STOCKS S
ON PR_ORDER_RESULT.STOCK_ID = S.ID

JOIN of 4 tables, how to restrict SELECT columns to one table only?

I am working on ABAP program - user input is to query column ANLAGE and output is to get all records from table EADZ (and only fields of EADZ) based on ANLAGE.
Statement and joins should work like this:
Input ANLAGE, find in table EASTL, gets LOGIKNR
Input LOGIKNR, find in table EGERR, gets EQUNR
Input EQUNR, find in table ETDZ, gets LOGIKZW
Input LOGIKZW, find in table EADZ, gets all records (this is the final output)
Here is the code I tried:
DATA: gt_cas_rezy TYPE STANDARD TABLE OF eadz,
lv_dummy_eanl LIKE eanl-anlage.
SELECT-OPTIONS: so_anl FOR lv_dummy_eanl NO INTERVALS NO-EXTENSION.
SELECT * FROM eadz
INNER JOIN etdz ON eadz~logikzw EQ etdz~logikzw
INNER JOIN egerr ON etdz~equnr EQ egerr~equnr
INNER JOIN eastl ON egerr~logiknr EQ eastl~logiknr
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE eastl~anlage IN #so_anl.
I got the records from table EADZ except that the date fields are empty (even though, they are filled in database table). I am assuming there is a problem with JOINs since in statement like this I join all the fields of all 4 tables into one "record" and then to corresponding fields of internal table.
How to get the values of date fields?
You can find the answer in the documentation.
If a column name appears multiple times and no alternative column name was granted, the last column listed is assigned.
In your case, at least two tables share the same column name. Therefore the values from the last mentioned table are used in the join.
You can solve this by listing the columns explicitly (or eadz~* in your case), giving an alias if required.
SELECT EADZ~* FROM EADZ INNER JOIN ETDZ ON EADZ~LOGIKZW = ETDZ~LOGIKZW
INNER JOIN EGERR ON ETDZ~EQUNR = EGERR~EQUNR
INNER JOIN EASTL ON EGERR~LOGIKNR = EASTL~LOGIKNR
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE EASTL~ANLAGE IN #SO_ANL.
If you require additional fields, you can add them explicily with e.g. EADZ~*, EASTL~A.

SQL Query to fetch information based on one or more condition. Getting combinations instead of exact number

I have two tables. Table 1 has about 750,000 rows and table 2 has 4 million rows. Table two has an extra ID field in which I am interested, so I want to write a query that will check if the 750,000 table 1 records exist in table 2. For all those rows in table 1 that exist in table 2, I want the respective ID based on same SSN. I tried the following query:
SELECT distinct b.UID, a.*
FROM [Analysis].[dbo].[Table1] A, [Proteus_8_2].dbo.Table2 B
where a.ssn = b.ssn
Instead of getting 750,000 rows in the output, I am getting 5.4 million records. Where am i going wrong?
Please help?
You're requesting all the rows in your select if b.UID is a unique field in column two.
Also if SSN is not unique in table one you can get the higher row count than the total row count for table 2.
You need to consider what you want from table 2 again.
EDIT
You can try this to return distinct combinations of ssn and uid when ssn is found in table 2 provided that ssn and uid have a cardinality of 1:1, i.e., every unique ssn has a single unique uid.
select distinct
a.ssn,b.[UID]
from [Analysis].[dbo].[Table1] a
cross apply
( select top 1 [uid] from [Proteus_8_2].[dbo].[Table2] where ssn = a.ssn ) b
where b.[UID] is not null
Try with LEFT JOIN
SELECT distinct b.UID, a.*
FROM [Analysis].[dbo].[Table1] A LEFT JOIN [Proteus_8_2].dbo.Table2 B
on a.ssn = b.ssn
Since the order detail table is in a one-many relationship to the order table, that is the expected result of any join. If you want something different, you need to define for us the business rule that will tell us how to select only one record from the Order detail table. You cannot effectively write SQL code without understanding the business rules that of what you are trying to achieve. You should never just willy nilly select one record out of the many, you need to understand which one you want.

Inner join sql statement

I have two tables, Invoices and members, connected by PK/FK relationship through the field InvoiceNum. I have created the following sql and it works fine, and pulls 44 records as expected.
SELECT
INVOICES.InvoiceNum,
INVOICES.GroupNum,
INVOICES.DivisionNum,
INVOICES.DateBillFrom,
INVOICES.DateBillTo
FROM INVOICES
INNER JOIN MEMBERS ON INVOICES.InvoiceNum = MEMBERS.InvoiceNum
WHERE MEMBERS.MemberNum = '20032526000'
Now, I want to replace INVOICES.GroupNum and INVOICES.DivisionNum in the above query with GroupName and DivisionName. These values are present in the Groups and Divisions tables which also have the corresponding Group_num and Division_num fields. I have created the following sql. The problem is that it now pulls 528 records instead of 44!
SELECT
INVOICES.InvoiceNum,
INVOICES.DateBillFrom,
INVOICES.DateBillTo,
DIVISIONS.DIVISION_NAME,
GROUPS.GROUP_NAME
FROM INVOICES
INNER JOIN MEMBERS ON INVOICES.InvoiceNum = MEMBERS.InvoiceNum
INNER JOIN GROUPS ON INVOICES.GroupNum = GROUPS.Group_Num
INNER JOIN DIVISIONS ON INVOICES.DivisionNum = DIVISIONS.Division_Num
WHERE MEMBERS.MemberNum = '20032526000'
Any help is greatly appreciated.
You have at least one relation between your tables which is missing in your query. It gives you extra records. Find all common fields. Say, are divisions related to groups?
The statement is fine, as far as the SQL syntax goes.
But the question you have to ask yourself (and answer it):
How many rows in Groups do you get for that given GroupNum?
Ditto for Divisions - how many rows exist for that DivisionNum?
It would appear that those numbers aren't unique - multiple rows exist for each number - therefore you get multiple rows returned