برگرداندن سطرها در ستونها
با سلام
من یک جدول دارم به فرمت زیر
== ID_Mozayede ==Radif_mozayede== name_sherkatkonande== chequNumber == mablaghpishnahedi
و این جدول دارای تعدادی سطر هست که اطلاعات شرکت کنندگان در مزایده تو اون ثبت شده.
select * from tbl که مینویسم اطلاعات تمام شرکت کنندگان مزایده رو به صورت سطری نشون میده (هر شرکت کننده در یک سطر)
حالا من میخام اطلاعات سه نفر اول مزایده رو در یک سطر برگردونم. ( نام نفر اول- چک - مبلغ- نام نفر دوم- چک مبلغ- نام نفر سوم- چک - مبلغ)
چطوری باید اینکارو انجام بدم.
نقل قول: برگرداندن سطرها در ستونها
چند روش هست
1 - استفاده از 3 select و در نهایت در 1 select join شود
2 - به تعداد declare در بیارید در 1 select نمایش دهید
روش 1 بهتره
نقل قول: برگرداندن سطرها در ستونها
به نظرم به این روش می تونید عمل کنید : اول 3 مشتری مورد نظر را در یک لیست به کمک selectبدست بیاورید و بعد در لایه بالار لیست به دست آمده را Pivot کنید
نقل قول: برگرداندن سطرها در ستونها
نقل قول:
نوشته شده توسط
fakhravari
چند روش هست
1 - استفاده از 3 select و در نهایت در 1 select join شود
2 - به تعداد declare در بیارید در 1 select نمایش دهید
روش 1 بهتره
سلام میتونی یک نمونه کد بدی. نمیدونم چطور باید بنویسم.
نقل قول: برگرداندن سطرها در ستونها
فرض کنیم اطلاعات این سه نفر اول باید در فیلدهای جدا بیاد (و نه در یک STRING) ،
میتونیم اول با یک SELECT هر سه رو جدا کنیم (مثلا در یک TEMP TABLE) و بعد از روی این نتیجه موقت خروجی رو بسازیم.
برای این کار هم بیشتر از راه میشه پیدا کرد.
1 ضمیمه
نقل قول: برگرداندن سطرها در ستونها
بعنوان مثال فرض کنین از جدول GRADES قرار هست اسم و بالاترین نمره های درس ARTS رو کنار هم در یک ROW بیاریم:
ضمیمه 153899
1 ضمیمه
نقل قول: برگرداندن سطرها در ستونها
اول یک جدول موقت (TOP3) میسازیم برای 3 نمره بالاتر و با استفاده از تابع ROW_NUMBER ترتیب رو هم مشخص میکنیم:
SELECT TOP 3
ROW_NUMBER() OVER (ORDER BY [Arts] DESC) AS RN ,
[Student] ,
[Arts]
INTO #TOP3
FROM [dbo].[Grades]
ORDER BY [Arts] DESC
ضمیمه 153901
1 ضمیمه
نقل قول: برگرداندن سطرها در ستونها
مابقی کار راحته:
SELECT
MAX(CASE WHEN RN=1 THEN [Student] END) AS [1ST STUDENT],
MAX(CASE WHEN RN=1 THEN [Arts] END) AS [1ST SCORE],
MAX(CASE WHEN RN=2 THEN [Student] END) AS [2ND STUDENT],
MAX(CASE WHEN RN=2 THEN [Arts] END) AS [2ND SCORE],
MAX(CASE WHEN RN=3 THEN [Student] END) AS [3RD STUDENT],
MAX(CASE WHEN RN=3 THEN [Arts] END) AS [3RD SCORE]
FROM #TOP3
ضمیمه 153903
نقل قول: برگرداندن سطرها در ستونها
اینجوری هم میشد بنویسیم:
DECLARE @ST1 AS NVARCHAR(50)
DECLARE @ST2 AS NVARCHAR(50)
DECLARE @ST3 AS NVARCHAR(50)
DECLARE @SCORE1 AS INT
DECLARE @SCORE2 AS INT
DECLARE @SCORE3 AS INT
SELECT TOP 3
ROW_NUMBER() OVER (ORDER BY ARTS DESC) AS RN ,
[Student] ,
[Arts]
INTO #TOP3
FROM [dbo].[Grades]
ORDER BY [Arts] DESC
SELECT @ST1=[Student], @SCORE1=[Arts] FROM #TOP3 WHERE RN=1
SELECT @ST2=[Student], @SCORE2=[Arts] FROM #TOP3 WHERE RN=2
SELECT @ST3=[Student], @SCORE3=[Arts] FROM #TOP3 WHERE RN=3
DROP TABLE #TOP3
SELECT
@ST1 AS '1ST STUDENT' , @SCORE1 AS '1ST SCORE' ,
@ST2 AS '2ND STUDENT' , @SCORE2 AS '2ND SCORE',
@ST3 AS '3RD STUDENT' , @SCORE3 AS '3RD SCORE'
نقل قول: برگرداندن سطرها در ستونها
نقل قول: برگرداندن سطرها در ستونها
نقل قول:
نوشته شده توسط
SKazemi
به نظرم به این روش می تونید عمل کنید : اول 3 مشتری مورد نظر را در یک لیست به کمک selectبدست بیاورید و بعد در لایه بالار لیست به دست آمده را Pivot کنید
کاش برای تکمیل بحث و مقایسه روشها مطلب رو ادامه میدادین.
روش پیشنهادی شما بنظرم درسته ولی مشکل همین هست که چجوری اون 3 نفر رو (که ثابت نیستن وگرنه راحت بود) در pivot بیاریم.
کدنویسی اش پیچیده میشه.
نقل قول: برگرداندن سطرها در ستونها
نقل قول:
نوشته شده توسط
fakhravari
چند روش هست
1 - استفاده از 3 select و در نهایت در 1 select join شود
2 - به تعداد declare در بیارید در 1 select نمایش دهید
روش 1 بهتره
اگر لطف کنین برای تکمیل بحث و مقایسه یک نمونه کد از روش 1 بگذارین خیلی خوبه.