Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query without max()
Message
De
14/09/2019 16:29:20
Walter Meester
HoogkarspelPays-Bas
 
 
À
14/09/2019 02:51:21
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
01670845
Message ID:
01670859
Vues:
79
J'aime (1)
Tariq,

I truly don't understand why you want to eliminate the MAX() or don't want to group on the extra fields. Any other solution is going to be slower than that. It also smells like bad code or database design.

But anyways, as an alternative (and a little more straightforward IMO) to Cetins code:
select x.code, y.Names, y.country, x.Qty
	FROM 
		(SELECT code, sum(Qty) as Qty
			FROM #Table1 T1
			GROUP BY code
		) AS x
	OUTER APPLY (SELECT TOP 1 names, country FROM #Table1 T2 WHERE x.Code = T2.code) as y
>>>Sir,
>>>
>>>While using SQLSERVER 2012, I have these codes
>>>
>>>
>>>USE at18;
>>>IF OBJECT_ID('tempdb.dbo.#Table1', 'U') IS NOT NULL
>>>  DROP TABLE #Table1; 
>>> 
>>>CREATE TABLE #Table1
>>>(
>>>code VARCHAR(50),
>>>	names VARCHAR(50),
>>>	country VARCHAR(50),
>>>	qty int
>>>)
>>>
>>>INSERT INTO #table1 VALUES (1104,'ERIC','Holland',45)
>>>INSERT INTO #table1 VALUES (1105,'Boris','Bulgaria',50)
>>>INSERT INTO #table1 VALUES (1106,'Anil','India',75)
>>>INSERT INTO #table1 VALUES (1107,'Cetin','Turkey',65)
>>>INSERT INTO #table1 VALUES (1105,'Boris','Bulgaria',40)
>>>INSERT INTO #table1 VALUES (1104,'ERIC','Holland',65)
>>>INSERT INTO #table1 VALUES (1107,'Cetin','Turkey',85)
>>>INSERT INTO #table1 VALUES (1106,'Anil','India',25)
>>>
>>>select * from #table1 order by code
>>>
>>>select code,max(names)as names,max(country)as country,sum(qty)as qty 
>>>from #Table1
>>>group by code
>>>order by code
>>>
>>>
>>>The codes work fine like shown in attachment.
>>>
>>>I used max() clause to get all other columns except column that I used in group by.
>>>
>>>Is there any other methods to get above result without using max() clause?
>>>
>>>I mean ;With CTE or Join
>>>
>>>Please help
>>
>>You can extent the group by to eliminate the MAX()
>>
>>
>>GROUP BY Code, Names, Country
>>
>>
>>Using a Join any other technique would be much more costly.
>
>Thanks sir for reply but I want to use only one column in group by clause.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform