SELECT t1.id, t1.[kh] AS '客户', t1.[lrsj] AS '录入时间', t1.[htbh] AS '合同编号', t1.[rq] AS '签订日期', t1.[htqsrq] AS '起始日期', t1.[htjzrq] AS '截止日期', t1.[zjey] AS '总金额', t1.[hjzld] AS '总吨数', t1.[lrsj] AS '创建时间', t1.[chpt] AS '撮合平台', CAST ( t1.[htwj] AS VARCHAR ( MAX ) ) AS '合同文件', STUFF( ( SELECT ',' + CONVERT ( VARCHAR, CASE WHEN ISNUMERIC( REPLACE( t2.ppcs, ',', '' ) ) = 1 THEN CAST ( REPLACE( t2.ppcs, ',', '' ) AS FLOAT ) END ) FROM uf_xsht_dt1 t2 WHERE t1.id = t2.mainid FOR XML PATH ( '' ) ), 1, 1, '' ) AS '品牌', STUFF( ( SELECT ' ' + CONVERT ( VARCHAR, CASE WHEN ISNUMERIC( REPLACE( t2.hsdjyd, ' ', '' ) ) = 1 THEN CAST ( REPLACE( t2.hsdjyd, ' ', '' ) AS FLOAT ) END ) FROM uf_xsht_dt1 t2 WHERE t1.id = t2.mainid FOR XML PATH ( '' ) ), 1, 1, '' ) AS '销售单价', (SELECT SUM ( sjfk ) from uf_xshksj2 WHERE cght=t1.id ) AS '实际已收', (SELECT SUM ( hkje ) from uf_xshksj2 WHERE cght=t1.id ) AS '已收货款', ( SELECT SUM( sjfk ) FROM uf_xshksj2 WHERE kxlx = 0 AND cght = t1.id ) AS '订金' FROM uf_xsht t1 INNER JOIN uf_xsht_dt1 t2 ON t1.id = t2.mainid LEFT JOIN uf_xshksj2 t3 ON t1.id = t3.cght GROUP BY t1.id, t1.[kh], t1.[lrsj], t1.[htbh], t1.[rq], t1.[htqsrq], t1.[htjzrq], t1.[zjey], t1.[hjzld], t1.[chpt], CAST ( t1.[htwj] AS VARCHAR ( MAX ) );中按ID查询计算收款比例‘已收比例’,当订金与已收货款的和小于总金额时,比例公式为已收货款除以总金额;否则为订金与已收货款求和再除以总金额
时间: 2024-01-07 22:03:42 浏览: 175
EL表达式访问内置方法
你可以使用 CASE 表达式来计算收款比例 '已收比例',根据订金与已收货款的和是否小于总金额来选择不同的计算方式。以下是相应的修改后的查询语句:
```sql
SELECT
t1.id,
t1.[kh] AS '客户',
t1.[lrsj] AS '录入时间',
t1.[htbh] AS '合同编号',
t1.[rq] AS '签订日期',
t1.[htqsrq] AS '起始日期',
t1.[htjzrq] AS '截止日期',
t1.[zjey] AS '总金额',
t1.[hjzld] AS '总吨数',
t1.[lrsj] AS '创建时间',
t1.[chpt] AS '撮合平台',
CAST ( t1.[htwj] AS VARCHAR ( MAX ) ) AS '合同文件',
STUFF(
(
SELECT
',' + CONVERT (
VARCHAR,
CASE
WHEN ISNUMERIC( REPLACE( t2.ppcs, ',', '' ) ) = 1 THEN
CAST ( REPLACE( t2.ppcs, ',', '' ) AS FLOAT )
END
)
FROM uf_xsht_dt1 t2
WHERE t1.id = t2.mainid FOR XML PATH ( '' )
),
1,
1,
''
) AS '品牌',
STUFF(
(
SELECT
' ' + CONVERT (
VARCHAR,
CASE
WHEN ISNUMERIC( REPLACE( t2.hsdjyd, ' ', '' ) ) = 1 THEN
CAST ( REPLACE( t2.hsdjyd, ' ', '' ) AS FLOAT )
END
)
FROM uf_xsht_dt1 t2
WHERE t1.id = t2.mainid FOR XML PATH ( '' )
),
1,
1,
''
) AS '销售单价',
(SELECT SUM(sjfk) FROM uf_xshksj2 WHERE cght = t1.id) AS '实际已收',
(SELECT SUM(hkje) FROM uf_xshksj2 WHERE cght = t1.id) AS '已收货款',
(SELECT SUM(sjfk) FROM uf_xshksj2 WHERE kxlx = 0 AND cght = t1.id) AS '订金',
CASE
WHEN ((SELECT SUM(ISNULL(hkje, 0)) FROM uf_xshksj2 WHERE cght = t1.id) + (SELECT SUM(ISNULL(sjfk, 0)) FROM uf_xshksj2 WHERE kxlx = 0 AND cght = t1.id)) < t1.[zjey] THEN
(SELECT SUM(ISNULL(hkje, 0)) FROM uf_xshksj2 WHERE cght = t1.id) / t1.[zjey]
ELSE
((SELECT SUM(ISNULL(hkje, 0)) FROM uf_xshksj2 WHERE cght = t1.id) + (SELECT SUM(ISNULL(sjfk, 0)) FROM uf_xshksj2 WHERE kxlx = 0 AND cght = t1.id)) / t1.[zjey]
END AS '已收比例'
FROM
uf_xsht t1
INNER JOIN uf_xsht_dt1 t2 ON t1.id = t2.mainid
LEFT JOIN uf_xshksj2 t3 ON t1.id = t3.cght
GROUP BY
t1.id,
t1.[kh],
t1.[lrsj],
t1.[htbh],
t1.[rq],
t1.[htqsrq],
t1.[htjzrq],
t1.[zjey],
t1.[hjzld],
t1.[chpt],
CAST ( t1.[htwj] AS VARCHAR ( MAX ) );
```
在上述查询中,我添加了一个 CASE 表达式来计算 '已收比例'。该表达式首先检查订金与已收货款的和是否小于总金额,如果是,则计算已收货款与总金额的比例;否则,计算订金与已收货款的和与总金额的比例。请根据你的实际需求和数据结构进行适当的调整。
阅读全文