I have some records that looks like this.
I want to pull out as the following.
Is this possible?
I can only pull out the duplicate values but can't get that I want.
SELECT [COLUMN A] , COUNT([COLUMN A]) FROM [MYTABLE] GROUP BY [COLUMN A] HAVING COUNT([COLUMN A]) >1 ORDER BY [COLUMN A]
Anyone please help me.
Thanks.
You can use the following query to get the desired result:
SELECT
ColumnA
, MAX(ColumnB) ColumnB
, MIN(ColumnC) ColumnC
, MAX(ColumnC) ColumnD
FROM MYTABLE
GROUP BY ColumnA
ORDER BY ColumnA;
Try in the 'Count()' sentence add the id or *
i.e.:
SELECT [COLUMN A] , COUNT(*) FROM [MYTABLE] GROUP BY [COLUMN A] HAVING COUNT(*) >1 ORDER BY [COLUMN A]
or
SELECT [COLUMN A] , COUNT(´id_col_A´) FROM [MYTABLE] GROUP BY [COLUMN A] HAVING COUNT(´id_col_A´) >1 ORDER BY [COLUMN A]
Related
I want to show two result sets in one result set, using UNION
The problem is, I want it to show all of the first table's records, and then go to the next one
But since SQL Server automatically orders the results by the first column, the final results get ordered by ID column, which both tables have
Both tables have a column with the ID of 1, 2, 3 and so on, and therefore the result of the UNION is like this:
[table 1].[record 1]
[table 2].[record 1]
[table 1].[record 2]
[table 2].[record 2]
I want it to show like this:
[table 1].[record 1]
[table 1].[record 2]
[table 1].[record 3]
[table 2].[record 1]
[table 2].[record 2]
[table 2].[record 3]
Add an "ordering column" e.g.
select 0 Orderb, T1.*
from Table1 T1
union all
select 1 Orderb, T2.*
from Table2 T2
order by OrderBy, id; -- whatever columns you wish to order by
I have used lateral flatten logic in Snowflake in the below query. The query works up to alias A.
I'm getting invalid identifier error when I use the join condition
based on the common column. ON B.product_id=A.product_id
SELECT A.ID, INDEX, purchase_list,
CASE WHEN INDEX = 1 and purchase_list NOT IN('121=find-values','122=find_results','123=item_details','',' ')
THEN purchase_list END as item_no
FROM (SELECT ID,index,d.value::string AS purchase_list FROM (
SELECT ID,c.value::string AS purchase_list
FROM table_1,lateral flatten(INPUT=>split(po_purchase_list, '|')) c
),
LATERAL flatten(INPUT=>split(purchase_list, ';')) d
) A -- The query would be correct till here
JOIN
table_2 B -- This is the table I need to join with table_1
ON B.product_id=A.product_id
WHERE date='2022-03-03'
AND b.item_src NOT IN('0','1','3','4')
from the error message it looks like it is not able to find the column product_id in one of the tables or CTE , can you include the column product_id in your SELECT statement for table A, and see if it works.
I have Table1
And I am trying to remove every identical Column A value if one of it's row has "ZX" anywhere in Column B. So if I did it right, it will look like Table2
I did the following:
Select
Column A,
Column B
From
Table1
Where
Column B not like '%ZX%'
However, it only removes rows with ZX and not every identical Column A values and returns this Table instead
I will really appreciate any help on this! Thank you in advance :)
You can use NOT IN
SELECT
ColumnA
, ColumnB
FROM table1
WHERE ColumnA NOT IN (SELECT ColumnA FROM table1 WHERE ColumnB like '%ZX%')
I like not exists for this purpose:
Select t1.*
From Table1 T1
where not exists (select 1 from table1 tt1 where tt1.a = t1.a and tt1.b like '%ZX%');
This can take advantage of an index on table1(a, b).
Use :NOT EXISTS and that should do it:
Select
[Column A],
[Column B]
From Table1 T1
where
NOT EXISTS (
SELECT 1 FROM TABLE1 T2
WHERE T2.[Column B] like '%ZX%'
AND T2.[column a] = t1.[column a]
)
I have searched much about it but I couldn't find any related information about my problem. I have a dataset like this.
Column1 Column2
A B
A B
A C
X B
X B
Y C
Y B
T A
T A
T A
I can distinct Column1 with total number of occurences. But I actually want to remove the constant rows. When I run the query, the result should be like this;
Column1 Column2
A B
A B
A C
Y C
Y B
As we see above A and Y have different values in Column2. How do I query this? I am using Sql Server 2014
You want to count the occurences of the pairings before you do anything
SELECT ColumnA, ColumnB, count(*)
FROM [source]
GROUP BY ColumnA, ColumnB
That will give you a list of each pairing and how often it occurs. Next you want to count how many pairings each value in ColumnA has, and cut out the ones with only one option:
SELECT ColumnA, count(*)
FROM
(
SELECT ColumnA, ColumnB, count(*)
FROM [source]
GROUP BY ColumnA, ColumnB
)
GROUP BY ColumnA
HAVING count(*) > 1
That will give you a list of ColumnA values that you're looking for. From there you want to look for those values of ColumnA in your original data with a WHERE .. IN statement:
SELECT ColumnA, ColumnB
FROM [source]
WHERE ColumnA IN
(
SELECT ColumnA, count(*)
FROM
(
SELECT ColumnA, ColumnB, count(*)
FROM [source]
GROUP BY ColumnA, ColumnB
)
GROUP BY ColumnA
HAVING count(*) > 1
)
COUNT(DISTINCT...) might work with a CTE:
; WITH CTE AS (
SELECT Column1
FROM [my_table]
GROUP BY Column1
HAVING COUNT(DISTINCT Column2) > 1
)
SELECT t.*
FROM [my_table] t
JOIN CTE ON CTE.Column1 = t.Column1;
I need to extract a single column of data from 10+ columns of a single table. Instead of stacking up 10+ union select statements, is there another way of doing this without repeating the union select statements for each column?
I can get it by stacking up 10+ select statements like below:
select 'column_5' from table_a
union
select 'column_6' from table_a
union
select 'column_7' from table_a
union
.
.
.
union
select 'column_18' from table_a
Thanks for your time in advance :)
Using Cross Apply or UnPivot you can avoid multiple UNION statements
select Distinct COL
from table
Cross apply
(
values
(column_1),
(column_2),
..
..
(column_18)
)
CS (COL)
Note : Since you have used UNION i have kept Distinct in select. If you don't want to remove duplicates then remove Distinct from select
A query with UNPIVOT would look something like this....
SELECT *
FROM TableName t
UNPIVOT (Vals FOR N IN (Column1, Column2, Column3,....,Column10))up
Important note all the columns in IN clause must be of the same data type, if they are not use a sub-query to convert them to a uniform data type and then unpivot them something like...
SELECT *
FROM (
SELECT CAST(Column1 AS VARCHAR(200)) AS Column1
,CAST(Column2 AS VARCHAR(200)) AS Column2
,CAST(Column3 AS VARCHAR(200)) AS Column3
,.....
,CAST(Column10 AS VARCHAR(200)) AS Column10
FROM TableName) t
UNPIVOT (Vals FOR N IN (Column1, Column2, Column3,....,Column10))up