Combining with Union Tables in SQL (values populating incorrectly) - sql

I am trying to perform a Union on 2 tables in SQL, both with columns are MultiNational, CompanyDescrpition, GDPRojectID, Company_Code, 2019TTV, 2020TTV and 2021TTV. These two tables populate values into 2019TTV, 2020TTV and 2021TTV when I make the two separate tables, however when I perform the union, it turns into 3 columns all called TTV and then it just fills with the year number instead of the value that should be there.
Any idea why this is happening?
Here is how I am performing the union.
select multinational, companydescription, GDProjectID, company_code, 2021TTV, 2020TTV, 2019TTV FROM #TTV Union select multinational, companydescription, GDProjectID, company_code, 2021TTV, 2020TTV, 2019TTV FROM #TTVUK

If you have column names starting with numbers, you should contain them in square brackets like [2021TTV].
EDIT: To clarify, if column names start with anything besides letters, you should contain the column name in square brackets or quotes.

Related

Combining 2 different value into 1 in SQL

First of all, I apologize for not being able to show my question with code. I have 2 tables. I'm merging these tables and returning a result. My values ​​include values ​​that are the same but written with different letters ( for example, INSTALL and INSTALL). These two values ​​are essentially the same. but it returns 2 different results because they are written with different letters. what I want is to convert the İNSTALL value to the INSTALL value and increase the total INSTALL value to 5. Any idea?
Column1
Values
INSTALL
2
İNSTALL
3
Use UPPER() function available in different DBMS.
select UPPER(Column1),sum(values) from Table group by UPPER(Column1)
UPPER: https://www.w3schools.com/sql/func_sqlserver_upper.asp
Depending on the anticipated overlap of letters, you could go about this a couple of ways.
Assuming the headers are the same across both tables, and only the cases are different (if they're not, use aliases in the CTE):
SELECT
LOWER(column_1) AS column_1,
SUM(Values) AS values_total
FROM (SELECT * FROM table_1 UNION SELECT * FROM table_2) combined_tables
GROUP BY 1
If there is different spelling across both tables, you could use a lower case cast of only the rightmost or leftmost letters.
SELECT
LOWER(RIGHT(column_1),3) AS column_1,
SUM(Values) AS values_total
FROM (SELECT * FROM table_1 UNION SELECT * FROM table_2) combined_tables
GROUP BY 1
If you expect a mix of misspellings, non-text characters, I'd suggest using regex to do fuzzy matching on the column values.

How to have all the columns of a table by doing a disctint on a single one in with SQL Server?

I have a table from my SQL Server database composed of a stylecode column and an image:
stylecode images
CTWAI4-RC https://dwk1ydkfsczz3.cloudfront.net/CTWAI4-3589-1jpg-90da2f9b-d942-49f5-85df-a5fae6a76934.jpg
CTWAI4-RC https://dwk1ydkfsczz3.cloudfront.net/CTWAI4-3589-1jpg-90da2f9b-d942-4785-85df-a5fae6a76934.jpg
WSM6HW-RC https://dwk1ydkfsczz3.cloudfront.net/WSM6HW-0090-1jpg-998a79f1-6ed9-4610-b30d-113745e37c59.jpg
WSM6HW-RC https://dwk1ydkfsczz3.cloudfront.net/WSM6HW-0090-1jpg-998a7781-6ed9-4610-b30d-113745e37c59.jpg
WLY8OW-RC https://dwk1ydkfsczz3.cloudfront.net//WLY8OW-0026-1jpg-b526d225-5ae5-44fc-b78b-d755a3d94ff6.jpg
WLY8OW-RC https://dwk1ydkfsczz3.cloudfront.net/WLY8OW-1651-1jpg-0051b696-d786-4864-9466-75f90b96fa33.jpg
As you can see I have several times the same style, I would like to make a query on my table to have only the same stylecode and the address of the corresponding images.
So I made a :
select distinct stylecode from dbo.Y2_Images;
Which returns all the unique stylecode but I would like to have next to it the values of the corresponding images,
I made a
select * from dbo.Y2_Images group by stylecode;
but my error is:
Column 'dbo.Y2_Images.images' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How to make the two columns have the same values by sorting the unique values of the code style?
There is more that one way to understand your question.
If you want distinct style/images tuples, then:
select distinct stylecode, images from dbo.Y2_images
If you want just one image per style, even if there are several distinct values, use an aggregate function such as max() or min():
select sylecode, max(images) as images from dbo.Y2_images group by sylecode
If you want all distinct images in a single column when there are several, use string aggregation:
select stylecode, stringagg(distinct images, ',') as images from db.Y2_images group by stylecode

I need to combine two results from a SQL select query

I Have two Select queries that get results for two tables with the same column names.
SELECT
labels.langjrd,
labels.id,
labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%'
;
and
SELECT
labels.langjrd,
labels.id,
labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'ENG%'
;
When I run the query I want to put all the results into one table. I read about union query but when I tried it, it didn’t work. I don't want to overwrite all the duplicate data I just want to have the second select results be added to the bottom of the first select results.
With UNION ALL you can combine results from two queries like this:
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%'
UNION ALL
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'ENG%';
You read about UNION which does a similar thing, but it filters out duplicate results. So it's similar as UNION ALL, but not the same.
But since you are actually querying the exact same columns from the same table, you can just put the two conditions in the where clause and split them using or. That way you will get all records that match either of the conditions.
SELECT
labels.langjrd,labels.id,labels.lcphrase
FROM
labels
WHERE
labels.langjrd LIKE 'FRE%' OR
labels.langjrd LIKE 'ENG%';

SQL Distinct limitation

i Have a table that contain coordinates info...like this:
Li_from
----------
36.2090536962569;37.1511090538763
36.2130356969589;37.1443713448309
36.2130876347598;37.1508944771551
36.2093999652314;37.1442425987982
36.2130356969589;37.1443713448309
36.2130356969589;37.1443713448309
and when i want to select distinct values from the column above like this:
SELECT DISTINCT Li_from from table.
the result is the same as above although the last two values are identical ..
so is there limitation for the distinct keyword , and how can i select distinct values??
Are you sure the last two columns are identical? Have you tried SELECT DISTINCT LTRIM(RTRIM(Li_from)) from table?
This will remove leading and trailing space characters.

The used SELECT statements have a different number of columns

For examples I don't know how many rows in each table are and I try to do like this:
SELECT * FROM members
UNION
SELECT * FROM inventory
What can I put to the second SELECT instead of * to remove this error without adding NULL's?
Put the columns names explicitly rather than *, and make sure the number of columns and data types match for the same column in each select.
Update:
I really don't think you want to be UNIONing those tables, based on the tables names. They don't seem to contain related data. If you post your schema and describe what you are trying to achieve it is likely we can provide better help.
you could do
SELECT *
from members
UNION
SELECT inventory.*, 'dummy1' AS membersCol1, 'dummy2' AS membersCol2
from inventory;
Where membersCol1, membersCol12, etc... are the names of columns from members that are not in inventory. That way both queries in the union will have the same columns (Assuming that all the columns in inventory are the same as in members which seems very strange to me... but hey, it's your schema).
UPDATE:
As HLGEM pointed out, this will only work if inventory has columns with the same names as members, and in the same order. Naming all the columns explicitly is the best idea, but since I don't know the names I can't exactly do that. If I did, it might look something like this:
SELECT id, name, member_role, member_type
from members
UNION
SELECT id, name, '(dummy for union)' AS member_role, '(dummy for union)' AS member_type
from inventory;
I don't like using NULL for dummy values because then it's not always clear which part of the union a record came from - using 'dummy' makes it clear that the record is from the part of the union that didn't have that record (though sometimes this might not matter). The very idea of unioning these two tables seems very strange to me because I very much doubt they'd have more than 1 or 2 columns with the same name, but you asked the question in such a way that I imagine in your scenario this somehow makes sense.
Are you sure you don't want a join instead? It is unlikely that UNOIN will give you what you want given the table names.
Try this
(SELECT * FROM members) ;
(SELECT * FROM inventory);
Just add semicolons after both the select statements and don't use union or anything else. This solved my error.
I don't know how many rows in each table
Are you sure this isn't what you want?
SELECT 'members' AS TableName, Count(*) AS Cnt FROM members
UNION ALL
SELECT 'inventory', Count(*) FROM inventory
Each SELECT statement within the MySQL UNION ALL operator must have the same number of fields in the result sets with similar data types
Visit https://www.techonthenet.com/mysql/union_all.php