ok ..我有2個表格......除了列名之外,它們完全一樣,因爲它們處理2個不同的「公司」,儘管...我想要的數據是在具有相同名稱的列中。很多我想要做的事情...是如果tableA.Col2> 0或tableB.Col2> 0,則爲1 .. else 0 ..他們也需要是獨特的...我可以讓他們作爲單獨的表...但我不能讓他們在一起..我需要他們在1列,因爲它是如何被髮送到C3代碼頁,以及它如何讀取... ...結構已經被預先設置了......並且在這個存儲過程中還有大約5個其他的語句正在執行,就像這樣(我也不是那個設置它的人)。所以,如果f這是可能的..我知道請..我也會粘貼我的確切代碼,如果你想看到現實生活中的代碼..我只是試圖簡化它,使其更容易理解。檢查2個sql列並在1列中顯示結果
DECLARE @id INT;
DECLARE @invest nvarchar(50);
SET @id = '7633';
SET @invest = '';
SELECT 'a' + CONVERT(nvarchar, orderfindings.risk_rating) AS cat, COUNT(DISTINCT orderfindings.prnt_id) AS stat
FROM orderheader, orderaudits, orderfindings
WHERE orderheader.id = orderaudits.orderheader_id AND orderaudits.ID = orderfindings.prnt_id
AND orderheader.id = @id AND orderfindings.risk_rating > 0 AND orderaudits.Investor_Name LIKE '%' + @invest + '%'
GROUP BY orderfindings.risk_rating
UNION ALL
SELECT 'a' + CONVERT(nvarchar, orderagencies.risk_rating) AS cat, COUNT(DISTINCT orderagencies.prnt_id) AS stat
FROM orderheader, orderaudits, orderagencies
WHERE orderheader.id = orderaudits.orderheader_id AND orderaudits.ID = orderagencies.prnt_id
AND orderheader.id = @id AND orderagencies.risk_rating > 0 AND orderaudits.Investor_Name LIKE '%' + @invest + '%'
GROUP BY orderagencies.risk_rating
-------------------
/*
Results from 1st statement
cat | stat
-----------
a1 | 4
-----------
a2 | 5
-----------
Results from 2nd statement
cat | stat
-----------
a1 | 3
-----------
a2 | 2
-----------
I pretty much want
Results from new awesome statement
cat | stat
-----------
a1 | 5
-----------
a2 | 5
-----------
I think those are the numbers that would be accurate
Simple version--- if tableA.Col2 is > 0 or tableB.Col2 is > 0 then 1 else 0
|Table A | Table B | result of query
| col_1 col2 | col_1 col2 |
--------------------------------------------------------------------
1 | 1 5 | 1 3 | 1
2 | 1 45 | 1 0 | 1
3 | 1 0 | 1 0 | 0
4 | 1 0 | 1 3 | 1
so a1 would be 3...4 records in the tables..but only 3 of them matter because row 3 has 0
whatevers in col 2 for both tables..there might be 100 records that have 1 for col 1..there
might only be 1
我希望這會更有意義......這是有點兒奇怪的任務
的東西,這絕對有效......但我覺得我可能已經搞亂了我的邏輯,我提出了它..要在我的腦海中經歷它..儘管我會幫助你將其標記爲答案。可能將不得不做一個新的問題,一旦我想出這 – 2012-04-12 18:08:01
沒問題,樂意幫助。 – 2012-04-12 19:21:29
如果你想給它另一個鏡頭..有新問題http://stackoverflow.com/questions/10130318/checking-2-sql-columns-and-displaying-result-in-1-column-pt2 ..我想我解釋了我需要的更好的工作哈哈..我希望 – 2012-04-12 19:23:32