I have the following result from my SQL query:
EventID P_Num PN_NameCount1 PN_Name
ABC-I-10942683 1089213 1 Company 1
ABC-I-10942683 1326624 8 Company 2
I am still learning this capability in SQL and need some assistance, Pivot's are NOT working in this scenario.
I have tried several different ways in attempting to do this, but was not able to create the desired results:
EventID P_Num1 PNC1 PN_Name PNC_Num2 PNC2 PN_Name
ABC-I-10942683 1089213 1 Company 11326624 8 Company 2
The EventID will change based on the different events from the companies, as the EventID is based on a particular date the event occurred with the company.
This is just a sample of the 500K+ rows of data I am working with. This will go into a temp table to be joined with the other various pieces of data needed.
I have tried this without success:
SELECT Key,
MAX(Col1) AS Col1,
MAX(Col2) AS Col2,
MAX(Col3) AS Col3
FROM table
GROUP BY Key
select EventID
, min(P_Num) as P_Num1, min(PN_NameCount1) PNC1, min(PN_Name) as PN_Name1
, max(P_Num) as P_Num2, max(PN_NameCount1) PNC2, max(PN_Name) as PN_Name2
from table
group by EventID
This is an answer to the stated question. And you stated in a comment you could extend it.
Related
I'm trying to find a specific entry. This entry can appear in only ONE of my two tables and will never repeat in either table.
Here is a scaled-down version example of my tables:
Table 1:
Date Name Room
2020/01/23 John 201
2020/01/22 Rebecca 203
Table 2 (does NOT have the same amount of columns):
Date Name
2020/01/23 Robert
2020/01/22 Sarah
To find this entry, I need to specify a date and a name. You can assume names never repeat.
So let's say I want to find Sarah 2020/01/22
She could appear in either Table 1 or Table 2, and I don't know which one and I need to know which table she's in.
I'm not sure how I would do this in a single SQL query. So far I just have two separate ones:
SELECT date,name from Table1 WHERE name="Sarah" and date='2020/01/22'
and
SELECT date,name from Table2 WHERE name="Sarah" and date='2020/01/22'
Is there a way to do it in a single query that also tells me which table it came from? It could be another field or some indication that I can get. Thanks.
Use union all, and add another column to each resulset, with a literal value that indicates the table name:
select 't1' as which, date, name from table1 where name = 'Sarah' and date = '2020-01-22'
union all
select 't2' as which, date, name from table2 where name = 'Sarah' and date = '2020-01-22'
Apologies if this is answered in SO. I searched and couldn't find an obvious duplicate. I am new to Access and inherited an old Access MDB (2000 File Format). [Note: I am using Office 365.]
Data is in 5 tables. However, only 4 are connected with Relationships. The fifth (unconnected) Table is a mashup of different data in 3 fields: Type, Code, and Description. Type has multiple rows with repeated values. Each Type row has a unique Code and Description. Each unique Type corresponds to a column in Table_1, and the Codes are values found in that column. (There are 3000 rows in Table_5 with over 250 unique Types, each with 1-500+ Codes.)
Here is a simplified version of Table_5:
Type Code Description
Atype A Atype_A_Description
Atype B Atype_B_Description
Atype X1 Atype_X1_Description
...
Class 1 Class_1_Description
Class 2 Class_2_Description
Class 9 Class_9_Description
...
Source A Source_A_Description
Source A1 Source_A1_Description
Source A2 Source_A2_Description
...
To complicate things, there isn't an exact match between the Field names in Table_1 and the Type entries in Table_5. (For example the Atype values in Table_5 correspond to a Field named ACC_TYPE1 in Table_1.)
I'm working on a method to get the Description from Table_5 based on a Field Name and Value from Table_1. I did this for 1 (hardcoded) Type. I created a Query for that Type in Table_5, and connected it to Table_1 with a Relationship.
Here's what I did:
Table_5_Atype_Query (as SQL, let me know if another format is preferred in SO)
SELECT Table_5.[Type], Table_5.[Code], Table_5.[Description]
FROM Table_5
WHERE (((Table_5.[Type])="Atype"));
The Relationship is:
Table/Query: Related Table/Query:
Table_1 Table_5_Atype_Query
ACC_TYPE1 Code
This works perfectly to get the Description field referencing Code from Table_5 based on values of ACC_TYPE1 in Table_1. It is NOT scalable to lookup Descriptions for other Type/Code pairs. (I would need 250 unique queries and relationships.) Put another way, I'd like to create a method to get the Description from Table_5 based on any Column Name and Value from Table_1. Are there better ways to do this?
A UNION query can rearrange fields to a normalized data structure.
UNION query with 5 fields:
SELECT ID, field1 as Code, "f1" as src from table1
UNION SELECT ID, field2, "f2" from table1
UNION SELECT ID, field3, "f3" from table1
UNION SELECT ID, field4, "f4" from table1
UNION SELECT ID, field5, "f5" from table1;
Of course, use your actual field and table names.
Now use that query in another query joining to table5.
There is a limit of 50 SELECT lines. There is no query designer or wizard for UNION - must use SQLView.
I came back here for another question related to my previous ones. A while ago I created a simple web products parser app which helped me to save some prices on different websites and do some comparison but after a while I found a relative big problem. I will explain everything below.
I have a lot of Mysql tables with the following format:
products with id, name, link
products-prices with id, id_prod, price, availability and date
As you can see, in the products-prices table there is a cell with id_prod which links to the id in the products table. When I parsed the link for every product I though they are unique but in reality something happened and for every product I have 3-4 links. For example, let's consider www.example.com/smth, instead of putting it parsed like that (without http/s and / at the final) in DB I put the whole link and for some reason now I have 4 different products (basically the same one) with http://www.example.com/smth, https://www.example.com/smth, http://www.example.com/smth/, https://www.example.com/smth/. Now I want to do a query to repair my database, basically to delete 1 to 3 entries and keep only one product from products and also change the id_prod from every entry in products-prices.
I don't want a direct answer, instead if you can route me to a tutorial/concept of what syntax I need to use I will be more than thankful. Have a good day!
Edit, real world example
https://images2.imgbox.com/f5/a5/0bdvqXcu_o.png
https://images2.imgbox.com/22/e8/BTbPLCzE_o.png
In the first picture, you can see that the only difference between those 3 products is the link, and in the link the only difference is that one of them is http the other ones are https and between those 2 https one has a slash at the final. In the second picture I have a lot (yea I know very inefficient) of entries which I want in this example to point to the product with id 2 from the first picture.
Try a simple grouping to ascertain the scale of the problem:
SELECT (COUNTPRODID) C, PRODID
FROM YOURTABLE
GROUP BY PRODID
HAVING COUNT(PRODID) >1
Once you have identified the scale of the issue, you could create a table to stage 1 of your records with a sequence based on the PRODID as below:
SELECT * INTO TmpTable
FROM
(SELECT
#row_number:=CASE
WHEN #PRODID = PRODID THEN #row_number + 1
ELSE 1
END AS SEQ,
#PRODID :=PRODID as PRODID
FROM
YOURTABLE
ORDER BY PRODID;) dups
WHERE dups.SEQ = 1
You could then delete all rows in you source
DELETE FROM YOURTABLE
WHERE PRODID IN (SELECT PRODID FROM TmpTable)
And then finally write the rows back from your temp table:
INSERT INTO YOURTABLE
SELECT field1, field2 etc. FROM TmpTable
this is my first time posting so forgive any errors please :)
Source File: Fields
Table 1: ID, Client Number
Table 2: ID, Client Number
Table 1 shows the Customer, and table 2 shows the Bill Payer.Both tables use ID as the transaction ID. So one transaction has 1 record in table 1, and 1 in table 2.
Desired Output:
Table 3: ID, Client Number, Customer/Payer.
I am aware that i can do to append table queries to 1 destination table to achieve but if i can do this with ONE SELECT query that would make the "flow" of my database alot smoother, as i am replacing an old query. Any help would be appreciated and thank you.
SELECT ID, Client_Number, 'Customer' AS Type FROM Table 1
UNION ALL
SELECT ID, Client_Number, 'Payer' AS Type FROM Table 2
SELECT ID, ClientNumber, 'Customer' as Kind FROM Customer
UNION ALL
SELECT ID, ClientNumber, 'Player' as Kind FROM Player
I'm new to MySQL so I really need some help with an issue I'm facing:
I have 7 tables in same database with some datas from tests:
The 7 tables have different columns but they all have these columns:
name.
second_name.
status.
In status are added current status of each student (accepted or rejected) and I want to display using select the name, second_name from the 7 tables where status = accepted.
I managed to display from 1 table
SELECT name, second_name FROM test1 WHERE status="accepted";
But I can not figure out how to display from all 7.
It will be a real help for me if somebody could give me a hint.
If you do not mind duplicate student names with multiple accepted tests, you can try doing it with UNION ALL:
(SELECT name, second_name FROM test1 WHERE status='accepted')
UNION ALL
(SELECT name, second_name FROM test2 WHERE status='accepted')
UNION ALL
(SELECT name, second_name FROM test3 WHERE status='accepted')
-- ...and so on
IMHO it's better to normalize database to have all the names, secondnames and statuses in the separate table and do the only select instead of UNION to improve performace.