I have this code below, but I keep getting an error that says A_NUM was specified multiple times for A.
SELECT Alleg,
CASE
WHEN ALLEGCU LIKE '%F%'
OR ALLEGCU LIKE'%A%' THEN 'EF'
ELSE ALLEGCU
END,
ALLEGCU
FROM
(SELECT *, CASE
WHEN ISNUMERIC(SUBSTRING(LTRIM(RTRIM(ALL)), 1,1)) = 0
THEN UPPER(LTRIM(RTRIM(SUBSTRING(ALL, CHARINDEX(' ', ALL)+1, LEN(ALL)
- CHARINDEX(' ', ALL)))))
ELSE ALL
END ALLEGCU
FROM ASSI_O
LEFT OUTER JOIN ALLS
ON ASSI_O.A_NUM = ALLS.A_NUM ) A
A_NUM column exists in both the tables ASSI_O AND ALLS and you are doing a SELECT * while doing the join.
Select only the needed columns and also prefix the table name to the column name
Changing SELECT * to SELECT ASSI_O.A_NUM as ASSI_NUM, ALLS.A_NUM as ALLS_ANUM should solve the problem
SELECT ASSI_O.A_NUM as ASSI_NUM, ALLS.A_NUM as ALLS_ANUM ...
Related
Here I have two tables as student_information and exmaination_marks.
examination_marks table have 3 columns for three subjects and include their marks.
I want to select the roll_number and name of the student from the student_information table where sum of the three subject's marks in examination_marks table is less than 100.
Both table has roll_number as primary key.
Here is the query I wrote.
select
si.roll_number,
si.name
from
student_information as si
left outer join examination_marks as em on
si.roll_number = em.roll_number
where
sum(em.subject_one + em.subject_two + em.subject_three) < 100;
But I got an error saying "ERROR 1111 (HY000) at line 1: Invalid use of group function"
Can any one help me with this?
sum(em.subject_one + em.subject_two + em.subject_three)< 100
this is the problem . Try these
Where (SELECT subject_one + subject_two + subject_three FROM examination_marks WHERE em.roll_number = si.roll_number) < 100
SUM is an "aggregate function" which can only be used inside a query which has a GROUP BY clause.
To get the sum of values within the same row you need to use the + operator. If the columns are NULL-able then you'll also need to use COALESCE (or ISNULL) to prevent NULL values invalidating your entire expression.
Like so:
SELECT
si.roll_number,
si.name,
COALESCE( em.subject_one, 0 ) + COALESCE( em.subject_two, 0 ) + COALESCE( em.subject_three, 0 ) AS sum_marks
FROM
student_information AS si
LEFT OUTER JOIN examination_marks AS em ON
si.roll_number = em.roll_number
WHERE
COALESCE( em.subject_one, 0 ) + COALESCE( em.subject_two, 0 ) + COALESCE( em.subject_three, 0 ) < 100;
(If you're wondering why the COALESCE( em.subje... expression is repeated in the SELECT and WHERE clauses, that's because SQL is horribly designed by (obscene profanities) is an unnecessarily verbose language).
I have a question about subqueries and I am uncertain how to google it effectively;
This feels hacky, but it works:
It uses a temporary table (#OrderProcessDates) which I preload with all Ordernr and Processdata data from a LinkedServer MySQL database
SELECT Ordernr
, CASE WHEN OrderStartDate IS NULL THEN (SELECT MIN(x.ProcessDate) from (SELECT * FROM #OrderProcessDates WHERE #OrderProcessDates.Ordernummer = Ordernr) as x)
ELSE OrderStartDate
END as OrderStartDate
, CASE WHEN OrderStopDate IS NULL AND OrderFinished = 1 THEN (SELECT MAX(x.ProcessDate) from (SELECT * FROM #OrderProcessDates WHERE #OrderProcessDates.Ordernummer = Ordernr) as x)
ELSE OrderStopDate
END as OrderStopDate
FROM (
SELECT Ordernummer as Ordernr
, OrderStartDate
, OrderStopDate
, OrderFinished
FROM OPENQUERY([LinkedServer],
'SELECT Ordernummer,Table2.DateCreated as OrderStartDate, Table2.DateFinished as OrderStopDate, Table2.Afgemeld as OrderFinished
FROM Database.Table1
JOIN Database.Table2 as ordernummers ON Table2.idordernummers = Table1.ordernummer '
) as sourcedata
) as fixedata
Is there a better way to use "ordernr" in a subquery; or are you always forced to use a query around the query for the values that are unknown?
Edit: removed earlier question; that contained non-working SQL; this works but need to know if this is "the way"
I was testing a query in SQL in which I need to concatenate values in the form of a comma-separated list, and it works, I just have the problem of duplicate values.
This is the query:
SELECT t0.id_marcas AS CodMarca,
t0.nombremarcas AS NombreMarca,
t0.imagenmarcas,
(SELECT String_agg((t2.name), ', ')
FROM exlcartu_devcit.store_to_cuisine t1
INNER JOIN exlcartu_devcit.cuisine t2
ON t1.cuisine_id = t2.cuisine_id
WHERE store_id = (SELECT TOP 1 store_id
FROM exlcartu_devcit.store
WHERE id_marcas = t0.id_marcas
AND status = 1)) AS Descripcion,
t0.logo,
t0.imagen,
(SELECT TOP 1 preparing_time
FROM exlcartu_devcit.store
WHERE id_marcas = t0.id_marcas
AND status = 1) AS Tiempo,
t0.orden,
(SELECT TOP 1 Avg(minimum_amount)
FROM exlcartu_devcit.store_delivery_zone
WHERE id_marcas = t0.id_marcas) AS MontoMinimo
FROM exlcartu_devcit.[marcas] t0
I thought the solution could be just adding a DISTINCT to the query to avoid repeated values in this way ...
(SELECT STRING_AGG(DISTINCT (t2.name), ', ') AS Descripcion
But apparently the STRING_AGG() function does not support it, any idea how to avoid repeated values?
Simplest way is just select from select, like this:
with dups as (select 1 as one union all select 1 as one)
select string_agg(one, ', ') from (select distinct one from dups) q;
vs original
with dups as (select 1 as one union all select 1 as one)
select string_agg(one, ', ') from dups;
Output to be produced
using this as reference but now with different scenario SQL Server query : get the sum conditionally
explanation:
Item, Sales, and remarks columns are given column from a database, New Sales column is a formulated column where in, it is getting the sum of items with the same keyword remarks except the default n/a remarks.
(regardless the remarks is not fully identical, at least there's a common similarity like what is on the image above - item 5 has "new" in it, still it sums up with item 6 because of their similar keyword found "small")
code used
FIRST OPTION- using partition - This doesn't work because when the remarks is not identical to each other it will not get the sum properly (for item5 and item6)
CASE
WHEN ([remarks] not like '%big%') AND ([remarks] not like '%PAENGS%')
THEN sales
ELSE SUM(sales) OVER(PARTITION BY [remarks])
END as 'New Sales'
SECOND OPTION -using Cross Apply - So it leave me to this, but I was lost as it is not getting the desired output.
CROSS APPLY
SELECT
d.*,
NewSales =
CASE
WHEN ([remarks] not like '%big%') or ([remarks] not like '%small%')
THEN Sales
ELSE x.NewSales
END
FROM #MSRSuperFinal3 d
CROSS APPLY(SELECT NewSales = SUM(Sales)
FROM #MSRSuperFinal3
WHERE ([remarks] like '%big%') or ([remarks] like '%small%')
)x
Any help will be highly appreciated
Using CROSS APPLY
SELECT *
FROM temp t
CROSS APPLY(
SELECT SUM(sales)
FROM temp
WHERE
remarks LIKE '%' + t.remarks + '%'
OR t.remarks LIKE '%' + remarks + '%'
)x(NewSales)
WHERE remarks <> 'n/a'
UNION ALL
SELECT *,
NewSales = sales
FROM temp
WHERE remarks = 'n/a'
ORDER BY item
Based on your comment, this should be your final query:
SELECT *
FROM #MSRSuperFinal3 t
CROSS APPLY(
SELECT
SUM(CurrentMonth)
FROM #MSRSuperFinal3
WHERE
t.misc LIKE '%' + misc + '%'
OR misc LIKE '%' + t.misc + '%'
)x(NewSales)
WHERE
([misc] LIKE '%BIGKAHUNA%')
or ([misc] LIKE '%PAENGS%')
UNION ALL
SELECT *,
NewSales = CurrentMonth
FROM #MSRSuperFinal3
WHERE
([misc] not like '%BIGKAHUNA%')
AND ([misc] not like '%PAENGS%')
AND ([misc] not like '%ROBINSONS%')
ORDER BY location, name
Try Left Join clause instead of Cross Apply:
SELECT a.item,
a.sales,
a.remarks,
CASE
WHEN a.remarks = 'n/a'
THEN a.sales
ELSE SUM( b.sales )
END AS [New Sales]
FROM query_using_cross_apply_to_get_sum_conditionally a
LEFT JOIN query_using_cross_apply_to_get_sum_conditionally b
ON(a.remarks LIKE '%' + b.remarks + '%'
AND b.remarks != 'n/a')
GROUP BY a.item, a.sales, a.remarks;
CASE
WHEN (a.[remarks] <> 'n/a') THEN a.sales
ELSE
(
SELECT SUM(b.sales)
FROM #MSRSuperFinal3 b
WHERE b.[remark] LIKE '%' + a.[remarks] + '%'
)
END as 'New Sales'
Note that i just added a table alias so that the query knows which remark to use where. Otherwise something that might be faster is doing a Common table expression and firstly doing a sum for sales that contain each other, so for each unique remark sum all sales that contain in, and then in your last select you cna merely join on your common table expression table.
I have a big problem with this query in SQL.
select distinct
b.*,
case
when b.Cash > b2.Cash
then ((b.Cash - b2.Cash) / b.Cash) * 100
end as Increased,
('Cash Increased by' + convert(VARCHAR(20), Increased))) as
Case
from
Accounting b
join
(…
In select statement I created column Increased. Then I want to created another column Case with the following value Cash Increased by… (value from Increased column).
My question is how can I do it in one select statement?
You have two options
Use this query as a subquery and do the concatenation in the outer query
You have to copy-paste the CASE..WHEN into the concatenations
Subquery
SELECT
*
, ('Cash Increased by' + convert(VARCHAR(20), Increased))) AS CASE
FROM (
SELECT DISTINCT
b.*
, CASE
WHEN b.Cash > b2.Cash THEN ((b.Cash - b2.Cash) / b.Cash) * 100
END AS Increased
FROM
Accounting b JOIN (...)
) SubQuery
Copy the CASE part
SELECT DISTINCT
b.*
, CASE
WHEN b.Cash > b2.Cash THEN ((b.Cash - b2.Cash) / b.Cash) * 100
END AS Increased
, (
'Cash Increased by' + CONVERT(VARCHAR(20),
CASE
WHEN b.Cash > b2.Cash THEN ((b.Cash - b2.Cash) / b.Cash) * 100
END)
) AS CASE
FROM
Accounting b JOIN (...)
NOTE
Do not forget to escape (or change) the alias for the concatenation. The CASE is a reserved word in most DMBS!
NOTE 2 Next time please mention the DBMS you are using!