我有一个表'Notes'和列名'Note',其值为Hi和Hello 看起来像这样
#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
#include <cstring>
using namespace std;
int main();
{
//Get seed colour
string seedColour = ""; //The empty "" is what "seedColor" will be chaned to after cin.
cout << "Enter Seed Colour (Red/Blue?) \n";
cin >> seedColour; //The user will iunput the seed's colour, which will change the empty "" and we now have (eg) "string seedColour = "red""
//Get Temp
int temp = 0;
cout << "Enter the Temp \n";
cin >> temp;
//Get Soil Moisture
string soilMoisture = "";
cout >> "Is the soil Wet or dry? \n";
cin >> soilMoisture;
//if red seed
if (seedColour == "red")
{
//if temp >= 75
if (temp >= 75)
{
//if soil is wet
if (soilMoisture == "wet")
{
//Output Sunflower
cout << "SUNFLOWER LAR.\n";
}
//if soil dry
if (soilMoisture == "dry")
{
//Dandelion
cout << "Dandelion.\n";
}
//Otherwwise (temp <75)
else
{
//Mushroom
cout << "Mushroom";
}
}
}
//if blue
if (seedColour == "blue")
{
//temp between 60 n 70
if (temp >= 60 && temp <= 70)
{
//wet soil
if (soilMoisture == "wet")
{
//Dandilion
cout << "Dandilion \n";
}
//dry soil
if (soilMoisture == "dry")
{
//Sunflower
cout << "Sunflower";
}
}
//Otherwise
else
{
//mushroom
cout << "Mushroom";
}
}
return 0
}
我想在sql的两列中分隔它 输出将是这样的:
Note
--------
HI
HELLO
我如何在sql查询中执行此操作?
答案 0 :(得分:1)
此?如果有超过2行,它应该可以工作。
WITH NotesWithId
AS (
SELECT ID = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
note
FROM Notes
)
SELECT [note1] = CASE WHEN [ID] % 2 <> 0 THEN [note] ELSE NULL END,
[note2] = CASE WHEN [ID] % 2 = 0 THEN [note] ELSE NULL END
FROM NotesWithId;
答案 1 :(得分:1)
DECLARE
@SQL VARCHAR(1000),
@COLUMN_LIST VARCHAR(200)
SET @COLUMN_LIST=(SELECT STUFF((SELECT ',[' + CONVERT(VARCHAR(5),NOTE ) +'] '
FROM (SELECT DISTINCT NOTE FROM #C)Z
FOR XML PATH('')),1,1,''))
SET @SQL=
'SELECT HI AS NOTE1 ,HELLO NOTE2 FROM
(
SELECT NOTE AS NOTE FROM #C
)B
PIVOT
(
MIN(B.NOTE) FOR B.NOTE IN ('+@COLUMN_LIST+')
)A'
SELECT @SQL
EXEC (@SQL)
答案 2 :(得分:0)
怎么样:
select max(note) as note1, min(note) as note2
from t;